PeanutsMonkey
PeanutsMonkey

Reputation: 7095

Why does git revert delete files on my filesystem?

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.

  1. Why did it delete the file?
  2. I attempted to recover the deleted file by running the command git checkout {hash} -- /path/to/recovery. It did recover the file however am unsure whether it was the way to go about it.
  3. If I run the command git status it shows me that file 1 is being tracked but is not committed. Why is that?

Upvotes: 3

Views: 2536

Answers (2)

Chronial
Chronial

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

cforbish
cforbish

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

Related Questions