Undo a git pull

The questions I’ve seen for undoing a git pull a slightly different to mine.

This is what I’ve done:

  • How can I Remove .DS_Store files from a Git repository?
  • Egit very slow at handling big projects
  • when was -u switch added to git-push?
  • Git warning for new, unstaged files
  • IntelliJ fails to commit changes when attempting to sign commit (GPG)
  • GitLab: Can't push code to server?
  • There is a project in directory A (not a repo). I initialized a repository in it, added the files, but did not commit anything. Then I pulled from repository B, which overwrote a bunch of my staged files.

    I was under the impression I could use git reset --hard to undo the merge. Of course that just checked out the HEAD of the commits I had just pulled in.

    I should have branched and commited something before I did this pull, hindsight is nice. Is there some way I can get my old unstaged files back?

  • How to integrate local changes with changes on the remote repository in git?
  • git init Permission denied - /private/etc/apache2
  • How to insert the commit and branch info from git to the apk when building with gradle?
  • PS1 line with git current branch and colors
  • `composer install` for production deployments
  • Git Server-Side Hook, acces to user doing the pushing, without SSH
  • 5 Solutions collect form web for “Undo a git pull”

    It seems there is an answer to recovering staged files here:

    Recovering added file after doing git reset –hard HEAD^

    A git pull is the same as git fetch + git merge. It is the merge step that overwrote your changes. To revert to the state before the merge, use git log to find your latest commit and then use git reset --hard 1234abcd where 1234abcd is the hash of the desired commit.

    Note that after the reset git pull will merge the changes again. To revert the changes for good, use git revert which will create an additional commit reversing the changes.

    Either just reset to the previous HEAD (i.e. the commit you had in your repository, that wasn’t updated from the remote), or use git reflog. The latter displays a list of actions that were made in your repository, the git pull command should be part of that. Simply git reset --hard X to a state before that command.

    Or just branch off the previous HEAD (you may delete/overwrite the previous branch), do your changes there and git pull again…

    I think you cannot full recover now. The best you can do is git status to check which file(s) are still intact. If the uncommitted files are not here, they are lost.

    Next time, consider using git stash before git pull, or, better yet, use git fetch.


    IF YOU ARE A ADVANCED GIT USER, AND THE FILES ARE REALLY IMPORTANT:

    If the files are staged, you may be able to extract them from the object repository. But these go right into git guts, which I don’t think you should try.

    If you really want, read these:

    Update January 17, 2011, 6:53 PM

    Just reread your question. I overlooked that you were pulling from project b as opposed to copying files into your project. Not sure if my answer still holds given your scenario. I think your best bet is to follow geoffreyd’s answer and checkout the answer to the StackOverflow question Recovering added file after doing git reset –hard HEAD^.

    Original Answer

    Just do a git checkout -- <file>.

    Here’s the example I ran to confirm the above…

    # Create a test directory and some test files
    $ mkdir blah
    $ echo "I've been overwritten" > test1.txt
    $ cd blah
    $ echo "I'm the real test1.txt" > test1.txt
    $ git init .
    Initialized empty Git repository in /Users/matthew/blah/.git/
    $ git add .
    $ cp ~/test1.txt .
    $ git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #   new file:   test1.txt
    #
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #   modified:   test1.txt
    #
    $ cat test1.txt
    I've been overwritten
    $ git checkout -- test1.txt
    $ cat test1.txt
    I'm the real test1.txt
    

    When you perform git status it tells you to use git checkout -- <file> to discard changes in the working directory. If I’m reading your question correctly, that’s what you want to do.

    Git Baby is a git and github fan, let's start git clone.