Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git un-remove before commit?

Tags:

git

I accidentally did a

git rm filename.txt -f

now if I do a git commit -m '', it will commit this delete. How do I un-remove it before I do a git commit?

like image 493
John Avatar asked Apr 07 '11 20:04

John


People also ask

How do you undo git add before commit?

To undo git add before a commit, run git reset <file> or git reset to unstage all changes.

How do you undo git add After commit?

If you have modified, added and committed changes to a file, and want to undo those changes, then you can again use git reset HEAD~ to undo your commit.


1 Answers

To get the version from the current commit (HEAD) into both the index (staging area, what you're about to commit) and the work tree:

git checkout HEAD filename.txt 

Note that this isn't just for "unremoval" - it's for getting you back to the version from the commit, whether you've modified it by changing one line or deleting the entire file.

Also, in case others find this looking for a slightly different answer, if you want to get the version from the index back into the work tree, you can use

git checkout filename.txt 

That's handy for when you manage to stage everything for a commit, then do something stupid (like remove a file) - you can save yourself by recovering from the index.

like image 188
Cascabel Avatar answered Nov 07 '22 11:11

Cascabel