Benbob
Benbob

Reputation: 14264

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:

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?

Upvotes: 25

Views: 53524

Answers (5)

Matthew Rankin
Matthew Rankin

Reputation: 461237

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.

Upvotes: 0

J-16 SDiZ
J-16 SDiZ

Reputation: 26930

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:

Upvotes: 1

geoffreyd
geoffreyd

Reputation: 1099

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

Recovering added file after doing git reset --hard HEAD^

Upvotes: 13

poke
poke

Reputation: 388403

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

Upvotes: 4

Gintautas Miliauskas
Gintautas Miliauskas

Reputation: 7892

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.

Upvotes: 31

Related Questions