Git: Quickly Switch to the Previous Branch

If you’ve been a long-time user of Git CLI and weren’t aware of this cool Git feature, consider yourself fortunate.

Both checkout and switch commands can accept a dash -, which will tell Git to change the current branch to the previous one.

Assuming we have two branches, namely one and two, with the current branch set to two, the following command will switch to the one branch.

➜ git:(two) ✗ git checkout -
Switched to branch 'one'

Running the same command again will switch back to two.

➜ git:(one) ✗ git checkout -
Switched to branch 'two'

The same outcome could be achieved by using the switch command.

➜ git:(two) ✗ git switch -
Switched to branch 'one'

Reducing the need for extensive typing and eliminating the effort of recalling the previous branch name, this Git feature is a productivity booster.


Posted

Tags: