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 ?
This question is related to
git
version-control
git-merge
This is the easiest way to revert you pull changes.
** Warning **
Please backup of your changed files because it will delete the newly created files and folders.
git reset --hard 9573e3e0
Where 9573e3e0
is your {Commit id}
you can do git reset --hard ORIG_HEAD
since "pull" or "merge" set ORIG_HEAD to be the current state before doing those actions.
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
.
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
Try run
git reset --keep HEAD@{1}
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.
If you have gitk (try running "gitk --all from your git command line"), it's simple. Just run it, select the commit you want to rollback to (right-click), and select "Reset master branch to here". If you have no uncommited changes, chose the "hard" option.
git pull do below operation.
i.
git fetch
ii.
git merge
To undo pull do any operation:
i.
git reset --hard
--- its revert all local change alsoor
ii.
git reset --hard master@{5.days.ago}
(like10.minutes.ago
,1.hours.ago
,1.days.ago
..) to get local changes.or
iii.
git reset --hard commitid
Improvement:
Next time use git pull --rebase
instead of git pull
.. its sync server change by doing ( fetch & merge).
If there is a failed merge, which is the most common reason for wanting to undo a git pull
, running git reset --merge
does exactly what one would expect: keep the fetched files, but undo the merge that git pull
attempted to merge. Then one can decide what to do without the clutter that git merge
sometimes generates. And it does not need one to find the exact commit ID which --hard
mentioned in every other answer requires.
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.
Source: Stackoverflow.com