Oprea.Rocks Logo

Get updates on new articles, and other opportunities

Quick checkout and rebase in Git

Here are two simple versions of the checkout and rebase subcommands I use to perform faster branch updates and merges.

Check out the last visited branch

git checkout -

This command is useful if, let's say, for example, you were on master and you checked out to a new feature branch. You work on the feature and once you're done, you want to switch back to master, get remote changes and then pull them in your feature branch as well, so you have the latest changes and check for any integration conflicts with other people's work.

Think of this command as an ALT+TAB or CMD+TAB for git branches. Running it again and again will move you between two git branches.

Rebase on top of the previous branch

Continuing on the same theme, now that you updated master from the remote, you have to switch back to your feature branch using the same command — git checkout - — and bring in the latest changes from master. You can do this with the command below.

git rebase -

How it works

Just like ~(tilde) is the shortcut for a user's $HOME directory, in *NIX systems, the -(dash) symbolises the previously visited directory. My guess is that behind the scenes, Git mimics the UNIX filesystem, with regards to branches.

That's it! Hope it's useful.