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 ? Thanks :)
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 ?
git pull
will do two things: it does a git fetch
and then a git merge
where it merges branches that have been setup to be merged in your config.
So what you want to do is to undo the merge (undoing the fetch doesn't make a lot of sense and shouldn't be necessary).
To do that you can try using git reset --hard
to reset to a previous state. Use the git-reflog command to find the SHA-1 of the previous state and then reset to it.
Warning : git reset --hard
removes all uncommitted changes.
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.
上一篇: 如何从另一个分支完全替换git中的master分支?
下一篇: 撤消git拉,如何将回购旧状态