I'm just starting to use git to save my projects to the cloud and wanted to know how I can do a pull after I've made some changes I didn't want to make.
I was modifying a file and I messed it up, I wanted to get the old version from github but when I do git pull
it it tells me alredy up to date , if I do git status
it tells me your branch is ahead of master by 1 commit .
How can I get the files from the remote repository even if they are old?
First some concepts
Git is a distributed version control system , which means that unlike other systems, history is not stored in one place (in the cloud), but is in each of the places you use it. The cloud copy is no different from the copy you have on your own computer. Both contain the full commit history.
The only thing it does
git pull
is bring to your computer the parts of the story that were in the cloud, but not on your computer (typically because you collaborate with other developers and some of them have uploaded new commits to the cloud). I understand that in your case you are the only person working on the repository, so there are no new commits in the cloud that you do not have in your local copy, and therefore the message that "already up to date".Actually you have the opposite case, your local history has one more commit than the history saved in the cloud (hence the message "your branch is ahead of master by 1 commit".
So what you really want is not to pull, but to remove the last commit you made , which in the current situation is easy because that commit is only in your local version. If you had uploaded it with it
push
would be more complicated as you would have to remove it from the "cloud" copy as well (and worse yet, if you collaborated with others and the others had done sopull
you would have to remove it from each of the other local copies of your files as well). collaborators).The solution
To "remove" a commit what you actually have to do is roll back the head of your branch ("HEAD") to the previous commit. This is accomplished with the command:
The de
HEAD~1
means "the commit before the last one made. This not only affects the local history (from which the last commit will disappear), but also your local folder thanks to the option--hard
. With that option, the working folder will be restored also to the state in which it was in the previous commit (which will therefore be the state that you can see in your repository in the cloud, through its web interface).Without the option
--hard
the working folder would not be modified (history wouldgit
go back to the previous commit, but any changes in the folder would still be there, so itgit status
would tell you that you have unsaved changes).In either case, having removed the last commit from your local history, it will now match the one in the cloud, so it will no longer tell you "your branch is ahead of master by 1 commit."