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?
To undo git add before a commit, run git reset <file> or git reset to unstage all changes.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With