1415

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 ?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
seg.server.fault
  • 19,118
  • 13
  • 35
  • 31
  • 35
    It 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 done `git reset --hard HEAD^` which resets to one commit before the HEAD. – jkp Aug 03 '09 at 16:57
  • 3
    --hard is necessary if you want to modify files in your working dir – William Pursell Aug 03 '09 at 17:02
  • 3
    @seg.server.fault: if it worked, you can always accept the answer ;) – jkp Aug 03 '09 at 17:29
  • 11
    git reset --hard HEAD^ – funroll Sep 16 '13 at 15:48
  • 8
    `git reflog` will show everything what have been done with git. There is a concern that `git reset --hard [sha1 of something from reflog]` will revert everything what is shown in `reflog`, 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. But `git checkout master` and `git reset --hard [SH1 of commit on master branch just before merge]` will reset only current master branch removing pulled merge from origin. – Vladimir Vukanac Jul 14 '15 at 10:54
  • It's good to get out of the `git pull` habit. Always use `git fetch` and then intelligently decide on what to do next. – Kaz Jul 27 '16 at 18:00
  • Another alternative way for HEAD^ is `git reset --hard HEAD~1` – Zelalem Mar 15 '21 at 11:00

18 Answers18

2016

Running git pull performs the following tasks, in order:

  1. git fetch
  2. 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"}
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
jkp
  • 78,960
  • 28
  • 103
  • 104
  • 380
    An 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 of `master`, `master@{"5 minutes ago"}`, or `master@{14:30}`. Full details on specifying revisions in this way can be found in `man git-rev-parse`, in the section called "specifying revisions". – Cascabel Aug 03 '09 at 17:16
  • 43
    In this case ORIG\_HEAD should also work ("git reset --hard ORIG\_HEAD") – Jakub Narębski Aug 03 '09 at 17:27
  • @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. – jkp Aug 03 '09 at 17:27
  • 1
    When I pulled, it says `Updating d2c90a3..035ac4d`. Here, you can also use `d2c90a3` as a parameter to reset. – Thai Oct 02 '13 at 06:28
  • You 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. – Simon The Cat Mar 31 '14 at 12:06
  • When 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 before `git pull`. Can you give me a better clue as to how to find this merge SHA-1? – Daniel Kaplan Feb 19 '15 at 02:13
  • 1
    **WARNING** : `git reset --hard master@{"10 minutes ago"}` also deletes all the changes that are not staged yet! – Muhammad Jun 18 '20 at 04:46
  • 5
    WARNING ::git reset --hard master@{"10 minutes ago"} made my repo like the first commit, I lost everything – brugobi Jun 12 '21 at 03:09
  • 1
    WARNING: git reset --hard master@{"10 minutes ago"} deleted almost my entire git history – JMess Feb 17 '22 at 16:35
  • WARNING: The comments above are so scary, that's why I checked out to my previous tag instead, hope you will have one. – louielyl Jun 19 '23 at 03:33
522

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.

Jeffrey Sun
  • 7,789
  • 1
  • 24
  • 17
205

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.

Martin G
  • 17,357
  • 9
  • 82
  • 98
  • 33
    `git merge --abort` works DURING a merge, not after `git pull` is finished. So this answer seems irrelevant to the question. – Abhishek Anand Dec 21 '16 at 19:48
  • 3
    `git reset --merge` deletes all changes not staged for commit. Found this out the hard way. – theadriangreen Mar 19 '20 at 23:39
  • `fatal: There is no merge to abort (MERGE_HEAD missing)` Does not work even during a (unsuccessful) merge – Bersan Jan 31 '22 at 10:15
  • 2
    This was actually what i needed as i didn't have the full pull done yet. I know its not related but was still helpful in my searching – jAC Jul 19 '22 at 02:06
89

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}
Ezequiel García
  • 2,616
  • 19
  • 12
  • The actual one liner that you need after you've just done a pull that has a mistake in it and you need to roll back in a hurry. – Daniel Howard Mar 30 '23 at 09:15
58

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

Community
  • 1
  • 1
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
44

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.

Samuel Carrijo
  • 17,449
  • 12
  • 49
  • 59
26

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}

Manish Goswami
  • 442
  • 4
  • 10
  • this response is very useful , because if we do an git pull origin branch wi get something like this ```Updating ffce65bd..e929e884```, the do ```git reset --hard ffce65bd``` – Ernesto Alfonso Mar 24 '20 at 15:00
21

you can do git reset --hard ORIG_HEAD

since "pull" or "merge" set ORIG_HEAD to be the current state before doing those actions.

Orlando
  • 9,374
  • 3
  • 56
  • 53
16

For reverting the last merge to your custom-branch, the easiest method is:

git reset --hard custom-branch@{1}

Example for main branch:

git reset --hard main@{1}
Safwan
  • 3,300
  • 1
  • 28
  • 33
  • This worked great to revert a messy rebase someone else had done. I had pulled it and with this I was able to revert that pull and get my old local branch back. Thanks. – MumiaIrrequieta Aug 24 '23 at 17:15
16

see the logs in your current branch where you performed git pull command

git log

the sample output will be look like this

commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxa3dd0
Author: user <user@gmail.com>
Date:   Tue Nov 23 20:19:58 2021 +0530

    latest changes

commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxd697b
Author: user <user@gmail.com>
Date:   Tue Nov 23 17:45:44 2021 +0530

    latest changes includes account details api

commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxc0e6fa
Author: user <user@gmail.com>
Date:   Tue Nov 23 17:02:39 2021 +0530

    latest changes

copy the last commit id you wish to want. for example if your last commit id is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxc0e6fa(assuming this commit id was the last commit id before you performed git pull) and two commits above this commit id is came after your git pull command use this commit id to get your previous changes

git reset --hard xxxxxxxxxxxxxxxxxxxxxxxxxxxxxc0e6fa

doing this will remove the commits above this commit id. you will get you previous changes after this, just as simple as that.

Akhil S
  • 955
  • 11
  • 16
12

Try run

git reset --keep HEAD@{1}
henrique_ms
  • 191
  • 2
  • 5
11

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 also

or

ii. git reset --hard master@{5.days.ago} (like 10.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).

GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39
10

The first thing I suggest doing is to make a copy of the project.

You can checkout a new branch(git checkout -b NewCopy) so you can have a copy and then return back to the branch where you checked out from.

Run this command to view git reference.

git reflog

It will display your reference log and commit_Id {something like e0371eb} that you can use to go back to a particular reference point.

Run this command to backtrack to a point

git reset --hard 7316f34  //replace that with your commit id

I suggest having 2 terminal open,one to display the log and the other to run the command

Slycreator
  • 1,144
  • 11
  • 18
9

do this to cancel your merge action : git merge --abort.

bigtheo
  • 624
  • 9
  • 16
8

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.

Davide
  • 17,098
  • 11
  • 52
  • 68
0

git reflog This will generate a list that looks like this:

2cae310 (HEAD -> ArunJan, origin/ArunJan) HEAD@{0}: checkout: moving from a590c01f5c2b83adcb36049f3fd590a3244fa745 to ArunJan a590c01 HEAD@{1}: checkout: moving from ArunJan to a590c01
2cae310 (HEAD -> ArunJan, origin/ArunJan) HEAD@{2}: reset: moving to HEAD
2cae310 (HEAD -> ArunJan, origin/ArunJan) HEAD@{3}: checkout: moving from 4a41a0fa05b98b274f1477991c6a25313f38b1b2 to ArunJan 4a41a0f HEAD@{4}: checkout: moving from ArunJan to 4a41a0fa05b98b274f1477991c6a25313f38b1b2
2cae310 (HEAD -> ArunJan, origin/ArunJan) HEAD@{5}: reset: moving to 2cae31078cdf1c8abac9068330b2ccdd3fc424e3
f830534 HEAD@{6}: pull origin ArunJan: Merge made by the 'ort' strategy.
a590c01 HEAD@{7}: commit: projet creation task
4a41a0f HEAD@{8}: commit: create new project
b6cb3c5 HEAD@{9}: checkout: moving from b6cb3c5e82aea4c64ab2a1f0c37e53695d21f194 to ArunJan
b6cb3c5 HEAD@{10}: checkout: moving from dev-server to b6cb3c5e82aea4c64ab2a1f0c37e53695d21f194
5eeaf3c (dev-server) HEAD@{11}: revert: Revert "project created and copy URL"

Select the version you want to revert to. For example, if I wanted to revert to ‘project creation task’, I’d select the ID b0168ee. Next, run the following command to revert your repository to that version:

git reset --hard a590c01

Arun Tyagi
  • 11
  • 2
0

if you have just pulled a branch on your local and want to remove changes caused by 'git pull' or 'git merge' command from your local workspace, then simply do a git reset --hard

this command will remove all of your uncommitted changes from the local workspace and can't be undone. So make sure to backup your important work that has not been committed yet.

navinrangar
  • 904
  • 10
  • 20
0

This should undo git pull

git reset --hard HEAD^
Sagar M
  • 1,168
  • 13
  • 10