Undo git pull, how to bring repos to old state

GitVersion ControlGit Merge

Git Problem Overview


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 ?

Git Solutions


Solution 1 - Git

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"}

Solution 2 - Git

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 - Git

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 - Git

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 - Git

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

Solution 6 - Git

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.

Solution 7 - Git

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}

Solution 8 - Git

you can do git reset --hard ORIG_HEAD

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

Solution 9 - Git

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.

Solution 10 - Git

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).

Solution 11 - Git

Try run

git reset --keep HEAD@{1}

Solution 12 - Git

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

Solution 13 - Git

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}

Solution 14 - Git

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

Solution 15 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionseg.server.faultView Question on Stackoverflow
Solution 1 - GitjkpView Answer on Stackoverflow
Solution 2 - GitJeffrey SunView Answer on Stackoverflow
Solution 3 - GitMartin GView Answer on Stackoverflow
Solution 4 - GitEzequiel GarcíaView Answer on Stackoverflow
Solution 5 - GitSazzad Hissain KhanView Answer on Stackoverflow
Solution 6 - GitSamuel CarrijoView Answer on Stackoverflow
Solution 7 - GitManish GoswamiView Answer on Stackoverflow
Solution 8 - GitOrlandoView Answer on Stackoverflow
Solution 9 - GitDavideView Answer on Stackoverflow
Solution 10 - GitGolamMazid SajibView Answer on Stackoverflow
Solution 11 - Githenrique_msView Answer on Stackoverflow
Solution 12 - GitSlycreatorView Answer on Stackoverflow
Solution 13 - GitSafwanView Answer on Stackoverflow
Solution 14 - GitbigtheoView Answer on Stackoverflow
Solution 15 - GitAkhil SView Answer on Stackoverflow