0

Suppose git pull command advances my local repo by several commits to match the remote repo and I want the local repo goes back to the commit id before git pull. In other words, I want to undo everything I get from git pull. Which command should I use?

etang
  • 329
  • 3
  • 10
  • @mkrieger1 My situation is after I did git pull and found the new code has bugs, so I want to undo git pull. – etang Dec 17 '20 at 23:23
  • 2
    Does this answer your question? [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) – mkrieger1 Dec 17 '20 at 23:24
  • I only want to revert the local repo back to the previous commit and completely remove everything I get from git pull. Also this question is not about remote repo. – etang Dec 17 '20 at 23:27
  • In that case, see the "Hard delete unpublished commits" section of that link. – M. Justin Dec 17 '20 at 23:28
  • 1
    Is that section about "remote repo"? The new code I get from git pull was committed by another person. I don't want to touch his commits in the remote repo. I just want to revert my local repo back to where it was before git pull. – etang Dec 17 '20 at 23:29
  • @etang This question is at least somewhat about the remote repo, since using `pull` means you have a remote repo you're pulling from. But it looks like your point is that the changes you're hoping to make are just to the local repo, not the remote one. So I think I understand now. – M. Justin Dec 17 '20 at 23:32

2 Answers2

0

To switch the branch back, simply checkout the Git commit listed in the output of the pull request:

$ git pull
Updating d5c40ea5e..29f1ae2b3
Fast-forward
 README.md                                             | 15 +++++++++++++++
 gradle.properties                                     |  2 +-
 2 files changed, 16 insertions(+), 1 deletions(-)

Note the d5c40ea5e this updated from. Simply check out this branch, and the code will be back to what it was before the pull request.

$ git checkout d5c40ea5e
Note: switching to 'd5c40ea5e'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at d5c40ea5e Update code to be super awesome
M. Justin
  • 14,487
  • 7
  • 91
  • 130
  • Using get checkout , you will still retain the new code downloaded from git pull in your local repo. Is there a CLEAN way to go back to the previous commit for the local repo? – etang Dec 17 '20 at 23:35
  • Is there a reason you don't want the code in your local repo at all? It's not on the current effective branch, so it shouldn't affect you running it (or creating a new local branch for your own changes while you wait for the bug fix). If you're ever planning to sync with the remote in the future, you'll end up getting it (hopefully in a future state with the previously mentioned bugs fixed). – M. Justin Dec 17 '20 at 23:38
  • I don't see any reason to keep the bad code in the local repo if there is a clean way to revert. – etang Dec 17 '20 at 23:40
0

I don't know of a silver bullet way of doing this, but with some minor detective work it is not difficult. First run git log from the local branch in question. You might see a merge commit at the HEAD of the branch. This would be the case if your pull strategy were merge. Or, you might see some number of new commits in the event that your branch were fast-forwarded. In either case, simply find the SHA-1 hash of the commit before the contents of the pull, and then hard reset to it:

# from your local branch
git reset --hard <SHA-1 of earlier commit>

Note that if your Git pull strategy be rebase, you have another problem. Now you can't simply just reset to an earlier commit, because potentially the pull has already rewritten your history. Cleaning up in this case might be a lot more work.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360