Undo git pull, how to bring repos to old state
Is there any way to revert or undo git pull so that my source/repos will come to old state that was before doing git pull ? I want to do this because it merged some files which I didn't want to do so, but only merge other remaining files. So, I want to get those files back, is that possible?
EDIT: I want to undo git merge for clarification. After seeing some answers, I did this
git reflog
bb3139b... HEAD@{0}: pull : Fast forward
01b34fa... HEAD@{1}: clone: from ...name...
Now, what should I do ? Doing git reset --hard
is OK ? I don't want to screw it again, so asking for detailed steps ?
Running git pull
performs the following tasks, in order:
-
git fetch
git merge
The merge step combines branches that have been setup to be merged in your config. You want to undo the merge step, but probably not the fetch (doesn't make a lot of sense and shouldn't be necessary).
To undo the merge, use git reset --hard
to reset the local repository to a previous state; use git-reflog to find the SHA-1 of the previous state and then reset to it.
Warning
The commands listed in this section remove all uncommitted changes, potentially leading to a loss of work:
git reset --hard
Alternatively, reset to a particular point in time, such as:
git reset --hard master@{"10 minutes ago"}
Same as jkp's answer, but here's the full command:
git reset --hard a0d3fe6
where a0d3fe6 is found by doing
git reflog
and looking at the point at which you want to undo to.
A more modern way to undo a merge is:
git merge --abort
And the slightly older way:
git reset --merge
The old-school way described in previous answers (warning: will discard all your local changes):
git reset --hard
But actually, it is worth noticing that git merge --abort
is only equivalent to git reset --merge
given that MERGE_HEAD
is present. This can be read in the git help for merge command.
git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.
After a failed merge, when there is no MERGE_HEAD
, the failed merge can be undone with git reset --merge
but not necessarily with git merge --abort
, so they are not only old and new syntax for the same thing. This is why i find git reset --merge
to be much more useful in everyday work.
it works
first use: git reflog
find your SHA of your previus state and make (HEAD@{1} is an example)
git reset --hard HEAD@{1}
Suppose $COMMIT
was the last commit id before you performed git pull
.
What you need to undo the last pull is
git reset --hard $COMMIT
.
Bonus:
In speaking of pull, I would like to share an interesting trick,
git pull --rebase
This above command is the most useful command in my git life which saved a lots of time.
Before pushing your newly commit to server, try this command and it will automatically sync latest server changes (with a fetch + merge) and will place your commit at the top in git log. No need to worry about manual pull/merge.
Find details at: http://gitolite.com/git-pull--rebase