3

I made a mistake and i pulled down changes, and i closed my text editor and did save all. So, all the changes got overwritten. Is there anyway to undo my changes or force a repull and overwrite my changes?

Please?

caf
  • 233,326
  • 40
  • 323
  • 462
Oscar Godson
  • 31,662
  • 41
  • 121
  • 201

3 Answers3

6

You should look into:

git reset --hard

The general idea is to reset your local repository to HEAD. Any tracked files will be replaced with the ones in HEAD.

tnunamak
  • 2,069
  • 3
  • 22
  • 34
  • So, if i do this, it wont erase any changes in my past commit tho, right, just reset to that point? i.e. 1. Commit. 2. pull. 3.**ck up and erase all pulled changes. 4. git reset --hard 5. All files equal to my #1. 6. Pull again, and everything is fixed! Right? – Oscar Godson Dec 31 '10 at 03:20
  • @Oscar It will reset to the commit that is HEAD. In your example, that is probably a commit created at #2, so you won't need to "repull", and you won't lose any local commits. – Jakob Borg Dec 31 '10 at 08:05
  • To be precise though, if you committed 1 but you never *pushed* it, then those changes would be lost. It would be more like 1. Commit. 2. pull and push 3. mess up files 4. git reset --hard Now it would be as if you had just finished step 2. – tnunamak Dec 31 '10 at 14:45
1

If I understand you correctly, most files are now reflecting the HEAD, whereas some random subset (whichever you had open in your editor) is based on commit $OLD_COMMIT plus your changes.

To throw away all your changes and force your working copy to reflect the HEAD, simply do git reset --hard. If you want to salvage your edits, here's what you can do:

First, find out which files got changed by your text editor using git status. Then, run git reflog to figure out the SHA1 of $OLD_COMMIT. Then, run

git diff $OLD_COMMIT -- enumerate.c every/one.c of/your.c changed/files.c > patch

Check that patch looks OK, and run git reset --hard to throw away all changes in your working copy. Then carefully try to apply the patch using the patch command on top the HEAD. Voila!

Jo Liss
  • 30,333
  • 19
  • 121
  • 170
0

See also How do I discard unstaged changes in Git?; use git stash to stash the changes. You can delete the stash if you don't need to keep the changes;

git stash save
git stash drop
Community
  • 1
  • 1
wimh
  • 15,072
  • 6
  • 47
  • 98