Let's say I deleted
fileA in commit
abc123. Then, when I
git reset, the output of
git diff shows that
fileA was deleted.
fileA is no longer in my working directory, so I can't re-add it.
The default for
git reset is
git reset --mixed. This is described in
man git-reset as:
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated.
What's an elegant solution. One workaround is to checkout the later commit that contains the file, copy the file, and then checkout
abc123 and add it again.
Two solutions below are correct. One shows how to recover the file if you checkout the commit at which it was deleted, and you simply run
git checkout fileA. The other shows how to recover the file in the present commit that's later that the commit at which
fileA was deleted. You run
git checkout abc123~ -- file A (The
-- ensures that the checkout command know that we are trying to roll back a file and not change branches).
I think this is what happened:
- You checked out commit
- You typed
git rm fileA.
- You typed
And now you see something like this:
# On branch master # Changes not staged for commit: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: fileA #
and you can't use
git add to get
fileA back because it's not on the filesystem anymore.
git checkout fileA.
You could also use
git reset --hard if you don't have any other changes you care about in your index or working tree.