2

I tried to run the following command:

git checkout HEAD^

and I got the output

More? (I press Enter)
More? (I press Enter)
error: pathspec 'HEAD
' did not match any file(s) known to git.

What does this mean? If I run this command instead:

git checkout HEAD~

the checkout succeeds with no issues.

I did confirm that HEAD currently only has one parent.

Edit: I forgot to specify that I was running this in Command Prompt. This was, in fact, the issue since ^ is the escape character in Command Prompt.

Zoe
  • 27,060
  • 21
  • 118
  • 148
NetherGranite
  • 1,940
  • 1
  • 14
  • 42
  • `HEAD^` means the parent of the HEAD, [see here](https://stackoverflow.com/questions/1955985/what-does-the-caret-character-mean). I have never tried doing this, so I don't know how to interpret the feedback Git is giving you. – Tim Biegeleisen Sep 05 '18 at 02:15
  • 4
    Are you on Windows? I think `^` means something special to Windows shells. – hobbs Sep 05 '18 at 02:17
  • @TimBiegeleisen Yeah, I was trying to go one commit back in time. It turned out that the issue was because `^` is the escape character on Command Prompt as hobbs pointed out. – NetherGranite Sep 05 '18 at 04:44
  • 1
    Possible duplicate of [Using a caret(^) when using Git for Windows in PowerShell](https://stackoverflow.com/questions/9600549/using-a-caret-when-using-git-for-windows-in-powershell) – tripleee Sep 05 '18 at 05:12
  • @tripleee Did you read the contents of both questions before you marked this as duplicate? The actual problems posed and errors received are nothing alike, and they aren't even talking about the same shell. Also, the accepted answer for that question is nowhere near the solution to this question. – NetherGranite Sep 05 '18 at 05:21
  • There were other answers which discussed `cmd` specifically. I agree that it's not a stellar duplicate, but the fact that it actually covers more ground than this one isn't necessarily a bad thing. – tripleee Sep 05 '18 at 05:55
  • @tripleee If that is the case, then those answers don't even answer the question properly. Also, I believe that Tim Biegeleisen's answer below much more thoroughly answers the `cmd` case than any of the answers on that question. Furthermore, it is probably not ideal to redirect users looking for a Command Prompt solution to a question that asks about PowerShell; that's just confusing and bound to throw off anyone who doesn't already know PowerShell, Command Prompt, and how they are related. – NetherGranite Sep 05 '18 at 05:58

1 Answers1

2

From this SO question, HEAD^ semantically means the parent of the current HEAD. There may be more than one parent of the current HEAD should the HEAD be a merge commit.

From the Git Bash, git checkout HEAD^ worked for me with no issues. I am assuming you are doing this from a command shell, and not the Bash. In this case, you may try wrapping with double quotes, e.g.

git checkout "HEAD^"

I have tested the above using a Windows prompt, and I got this:

HEAD is now at 450db90... some commit message here

This means that the above checkout put us into a detached HEAD state, one commit prior to the current HEAD of the branch. If you want to return the original branch after looking around, just checkout the branch name, e.g.

git checkout your_branch
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I should add that `git checkout HEAD^^` is also a solution, but that can get pretty messy pretty fast. – NetherGranite Sep 05 '18 at 05:55
  • 1
    Please expand on _the why_ of sole `^` at the end of line not working in `cmd.exe`. I mean, you present a solution, which is fine, but it worth explaining why it's needed in the first place. – kostix Sep 05 '18 at 10:36