What does "fatal: bad revision" mean?

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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).

Solution 2 - Git

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.

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

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

Solution 6 - Git

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.

Solution 7 - 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

Solution 8 - Git

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...

Solution 9 - Git

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.

Solution 10 - Git

I tried all of this, and git was complaining that the pathspec of the file I was trying to checkout was unknown.

The easiest fix for me was to delete the already versioned file locally and revert to the versioned version.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRose PerroneView Question on Stackoverflow
Solution 1 - GitAmberView Answer on Stackoverflow
Solution 2 - GitBenRView Answer on Stackoverflow
Solution 3 - GitCarl NorumView Answer on Stackoverflow
Solution 4 - GitNicolas ZozolView Answer on Stackoverflow
Solution 5 - GitAD7sixView Answer on Stackoverflow
Solution 6 - GitSuneelView Answer on Stackoverflow
Solution 7 - GitmanojldsView Answer on Stackoverflow
Solution 8 - GitmrutyunjayView Answer on Stackoverflow
Solution 9 - GitGregorView Answer on Stackoverflow
Solution 10 - GitBrennan NunamakerView Answer on Stackoverflow