0

When doing checkout, I got this message:

$ git checkout HEAD~2
Note: switching to 'HEAD~2'.

You are in a 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits that you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

What does the dash mean in git switch -? Is it always the previous branch?

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
  • 2
    As the documentation says here: https://git-scm.com/docs/git-switch/2.23.0 `-` is equivalent to `@{-1}` which is the last position of `HEAD` – mousetail Apr 24 '22 at 10:57
  • @mousetail Please remove that comment, `-` is the last branch/commit switched to using the "git switch" or "git checkout" operation, not the last position of `HEAD`. The last position of `HEAD` is actually `HEAD@{1}`. – Wenfang Du May 08 '23 at 07:33

1 Answers1

3

The documentation of git switch states the following:

You can use the @{-N} syntax to refer to the N-th last branch/commit switched to using "git switch" or "git checkout" operation. You may also specify - which is synonymous to @{-1}. This is often used to switch quickly between two branches, or to undo a branch switch by mistake.

Thus, - is equivalent to @{-1} which represents the last commit selected with git switch or git checkout.

mousetail
  • 7,009
  • 4
  • 25
  • 45
  • 3
    There is precedent for this option. In Bash (and possibly other command shells) it's possible to use `cd -` to change to the last directory location you were at, before the current one. – Drew Noakes Apr 25 '22 at 01:46