Home > database >  What does the dash mean in `git switch -`?
What does the dash mean in `git switch -`?

Time:04-24

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?

CodePudding user response:

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.

  • Related