Undo git pull, how to bring repos to old state
Solution 1
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"}
Solution 2
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.
Solution 3
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.
Solution 4
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}
Solution 5
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
seg.server.fault
Email : [email protected] Love programming, reading SE books, blogs ... Knowledge of C, C++ (basic, self learning) ... Planning to learn C#, Java ... Learning linux ...
Updated on July 08, 2022Comments
-
seg.server.fault almost 2 years
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 ?-
jkp over 14 yearsIt looks like you only have two things in your history: a clone and a fetch. Just reset to the clone:
git reset --hard 01b34fa
, in this case you could have donegit reset --hard HEAD^
which resets to one commit before the HEAD. -
William Pursell over 14 years--hard is necessary if you want to modify files in your working dir
-
jkp over 14 years@seg.server.fault: if it worked, you can always accept the answer ;)
-
funroll over 10 yearsgit reset --hard HEAD^
-
Vladimir Vukanac almost 9 years
git reflog
will show everything what have been done with git. There is a concern thatgit reset --hard [sha1 of something from reflog]
will revert everything what is shown inreflog
, which sometimes are not goal, eg. you want to revert merge on master branch pulled from origin with bad data (happens), and after that merge you have worked on other branches.reflog
will show every chage on other branches. Butgit checkout master
andgit reset --hard [SH1 of commit on master branch just before merge]
will reset only current master branch removing pulled merge from origin. -
Kaz almost 8 yearsIt's good to get out of the
git pull
habit. Always usegit fetch
and then intelligently decide on what to do next. -
Manish Goswami almost 5 yearsI have adde the latest answer !
-
vsync almost 4 yearsSaw
999
votes so without reading I had to vote up for a round1000
(mild OCD) -
Zelalem about 3 yearsAnother alternative way for HEAD^ is
git reset --hard HEAD~1
-
-
Cascabel over 14 yearsAn excellent way to pick the previous state, instead of using git-reflog and copying hashes, is to use a shortcut like
master@{1}
, which is the previous position ofmaster
,master@{"5 minutes ago"}
, ormaster@{14:30}
. Full details on specifying revisions in this way can be found inman git-rev-parse
, in the section called "specifying revisions". -
Anonigan over 14 yearsIn this case ORIG_HEAD should also work ("git reset --hard ORIG_HEAD")
-
jkp over 14 years@Jelfromi: thanks for that tip, I wasn't aware you could be so verbose about revisions. I knew about picking revisions relative to HEAD, but when the question was posed I didn't knowhow far back in time he wanted to go.
-
Evan Moran over 12 yearsThis is worth running if you haven't seen it. It pops up a crazy GUI.
-
Thai over 10 yearsWhen I pulled, it says
Updating d2c90a3..035ac4d
. Here, you can also used2c90a3
as a parameter to reset. -
Simon The Cat about 10 yearsYou don't need to type hashes when you use reflog. You can also use HEAD@{1} or what ever previous number as defined in the reflog.
-
Rana Ghosh about 9 yearsWhen I type
git reflog
I don't see anything about merges in there. The most recent things are checkouts and before that is stuff I was doing beforegit pull
. Can you give me a better clue as to how to find this merge SHA-1? -
mc9 about 9 yearsWhy can't I just do
git reset HEAD --hard
, for instance? -
xji about 9 years@MikeC This approach allows you to go back several pulls, for instance
-
E. Sundin over 7 yearsI wasn't able to use the left side ids but
git reset --hard HEAD@{n}
worked -
Abhishek Anand over 7 yearsYou should have suggested an edit to jkp's answer instead of almost replicating it here after so many years.
-
Abhishek Anand over 7 years
git merge --abort
works DURING a merge, not aftergit pull
is finished. So this answer seems irrelevant to the question. -
pmiranda about 7 yearsIf I have this on
git reflog
: dab04ec HEAD@{0}, aaaaaaa HEAD@{1} and bbbbbbb HEAD@{2}. If I dogit reset --hard bbbbbbb
I will lost the HEAD 0 and 1 ? -
Jeffrey Sun almost 6 years@pmirnd Yes, you will lose both HEAD 0 and 1.
-
Lola almost 6 yearsi loooove you :)
-
Cory McAboy over 5 yearsYou saved my day.
-
theadriangreen about 4 years
git reset --merge
deletes all changes not staged for commit. Found this out the hard way. -
Ernesto Alfonso about 4 yearsthis response is very useful , because if we do an git pull origin branch wi get something like this
Updating ffce65bd..e929e884
, the dogit reset --hard ffce65bd
-
Pradeep B P about 4 years- git reflog, then check the HEAD, - git reset --hard HEAD@{11} This worked for me
-
Muhammad almost 4 yearsWARNING :
git reset --hard master@{"10 minutes ago"}
also deletes all the changes that are not staged yet! -
brugobi almost 3 yearsWARNING ::git reset --hard master@{"10 minutes ago"} made my repo like the first commit, I lost everything
-
Bersan over 2 years
fatal: There is no merge to abort (MERGE_HEAD missing)
Does not work even during a (unsuccessful) merge -
JMess about 2 yearsWARNING: git reset --hard master@{"10 minutes ago"} deleted almost my entire git history