[git] What does "fatal: bad revision" mean?

In the context:

git revert HEAD~2 myFile
fatal: bad revision '/Users/rose/gitTest/myFile'

I'm sure HEAD~2 exists.

EDIT Amber is correct. I meant to use reset instead of revert.

This question is related to git

The answer is


If you only want to revert a single file to its state in a given commit, you actually want to use the checkout command:

git checkout HEAD~2 myFile

The revert command is used for reverting entire commits (and it doesn't revert you to that commit; it actually just reverts the changes made by that commit - if you have another commit after the one you specify, the later commit won't be reverted).


in my case I had an inconsistent state where the file in question (with the bad commit hash) was not actually added to Git, this clashed somehow with IntelliJ's state. Manually adding the file using git on the command line fixed the issue for me.


If you want to delete any commit then you might need to use git rebase command

git rebase -i HEAD~2

it will show you last 2 commit messages, if you delete the commit message and save that file deleted commit will automatically disappear...


Git revert only accepts commits

From the docs:

Given one or more existing commits, revert the changes that the related patches introduce ...

myFile is intepretted as a commit - because git revert doesn't accept file paths; only commits

Change one file to match a previous commit

To change one file to match a previous commit - use git checkout

git checkout HEAD~2 myFile

I was getting this error in IntelliJ, and none of these answers helped me. So here's how I solved it.

Somehow one of my sub-modules added a .git directory. All git functionality returned after I deleted it.


git revert doesn't take a filename parameter. Do you want git checkout?


I had a "fatal : bad revision" with Idea / Webstorm because I had a git directory inside another, without using properly submodules or subtrees.

I checked for .git dirs with :

find ./ -name '.git' -print

I had a similar issue with Intellij. The issue was that someone added the file that I am trying to compare in Intellij to .gitignore, without actually deleting the file from Git.


Why are you specifying myFile there?

Git revert reverts the commit(s) that you specify.

git revert HEAD~2

reverts the HEAD~2 commit

git revert HEAD~2 myfile

reverts HEAD~2 AND myFile

I take myFile is a file that you want to revert? In that case use

git checkout HEAD~2 -- myFile