• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Twitter GitHub
You are here: Home / Computing / Git Previous Branch

Git Previous Branch

Last updated on February 11, 2019 by Sal Ferrarello

When working with Git on the command line, I spend a lot of time switching back and forth between two branches. Even with Git tab completion, it is a lot of typing. However Git has a shortcut for the previous branch, a single dash (-).

Checkout Previous Branch

git checkout -

Example

While on the master branch of your git project, you create a new branch and check it out.

git branch sf/fix-spacebar-overheat-1172
git checkout sf/fix-spacebar-overheat-1172

Now, you are on the new branch sf/fix-spacebar-overheat-1172. The following line checks out your previous branch (master).

git checkout -

If you git checkout - again, you go back to sf/fix-spacebar-overheat-1172.

Merge Previous Branch

I also like to use - when merging.

git branch sf/fix-spacebar-overheat-1172
git checkout sf/fix-spacebar-overheat-1172

# add commits to sf/fix-spacebar-overheat-1172

git checkout -
git merge -

This example is tricky at first but a great workflow when you get the hang of it. Our two instances of - in this example each refer to a different branch.

git branch sf/fix-spacebar-overheat-1172
git checkout sf/fix-spacebar-overheat-1172

# add commits to sf/fix-spacebar-overheat-1172

# switch to our previous branch (master)    
git checkout -

# merge our previous branch (sf/fix-spacebar-overheat-1172)
git merge -

Deleting a Branch Does Not Work

The one thing this shortcut is missing is the ability to delete the previous branch. git branch -d - does not work.

git branch -d -
error: branch '-' not found.

Any Previous Branch @{-n}

We can use a slightly longer version to refer our previous branch, which is @{-1}.

# delete the previous branch
git branch -d @{-1}

Using the @{-n} format can refer to other branches you previously checked out.

  • @{-1} is the previous branch
  • @{-2} is the branch you had checked out before the previous branch
  • @{-3} is the branch you had checked out before before the previous branch
  • etc.

– is a shortcut

When we use -, this is a special shortcut for @{-1}

Both of these will work but are more verbose than using -

git checkout @{-1}
git merge @{-1}
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Computing, Dev Tips, Draft, Recommendations Tagged With: command line, Git

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2022 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in