Reputation: 7095
I committed a file to my local repository as a test to undo my commit I ran the command git revert {hash}
which then logged a revert message in git's history i.e. Revert "Commit of file 1" This reverts commit {hash}
As soon as I ran the command git revert
it also deleted the file from my working tree/space.
git checkout {hash} -- /path/to/recovery
. It did recover the file however am unsure whether it was the way to go about it.git status
it shows me that file 1 is being tracked but is not committed. Why is that?Upvotes: 3
Views: 2536
Reputation: 70683
You are probably looking for git reset HEAD^
. That will “undo” the last commit in the sense that your repo is now in the state it was before you ran git commit
.
git revert
on the other hand will undo all the changes in a commit. So if you revert a commit that created a file, that action is reverted -> the file is deleted.
Upvotes: 4
Reputation: 8819
If the revert deleted the file, it means that {hash}
that you reverted is the same {hash}
that added the file in the first place. The file is not lost, you could always git checkout {hash} {file}
or git checkout -b tmpbranch {hash}
to get the file back. These two commands do different things, for more information refer to git help.
The reason your file is untracked is because the checkout command you ran simply created a new file that is not yet checked in at the revision you are at.
Upvotes: 1