Switching to the previous Git branch... duh!

realedwintorres

Edwin Torres

Posted on March 6, 2021

Switching to the previous Git branch... duh!

I have been using Git version control for several years. I probably should have mastered it by now, but that has not happened. I know just enough commands and options to be dangerous. Every now and then, I will learn something new and smile.

One example is the git checkout command. I have used this command countless times to switch back and forth between different branches in my Git repos. For example, here is a repo with master and develop branches:

$  git checkout develop
Switched to branch 'develop'
$  git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$  git checkout develop
Switched to branch 'develop'
$  
Enter fullscreen mode Exit fullscreen mode

I have been switching Git branches this way for years. But this week, I learned git checkout -. This version of the command switches to the last branch that you were on:

$  git checkout develop
Switched to branch 'develop'
$  git checkout -
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$  git checkout -
Switched to branch 'develop'
$  git checkout -
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$  
Enter fullscreen mode Exit fullscreen mode

Mind blown! The git checkout - command is a blessing. It is so much easier, especially when your Git branches have long names. This would have saved me lots of typing over the years 😩. Oh well, better late than never!

If you already know about git checkout -, great. If not, I hope it makes your Git life easier.

Thanks for reading. 😃

Follow me on Twitter @realEdwinTorres for more programming tips and help.

💖 💪 🙅 🚩
realedwintorres
Edwin Torres

Posted on March 6, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related