git checkout -
checks out the last branch you were working on.
Do you find yourself needing to switch between the same two branches in git again and again? There's a shortcut for that in the terminal!
Instead of typing out the branch name manually each time, substitute the name with the hyphen character -
.
Note: In the gif, I'm using the Bash shell in iTerm2 on a Mac.
If you can't see the gif, here's a text representation, where the line with the dollar $
represents the command prompt:
[~test-directory] | first-really-long-branch-name $ git checkout second-super-long-branch-name
Switched to branch 'second-super-long-branch-name'
[~/test-directory] | second-super-long-branch-name $ git checkout -
Switched to branch 'first-really-long-branch-name'
[~/test-directory] | first-really-long-branch-name $ git checkout -
Switched to branch 'second-super-long-branch-name'
[~/test-directory] | second-super-long-branch-name $
This shortcut can save keystrokes when toggling between two branches.
It can be helpful when you're working on branches with long names, or your branches have similar names, making autocomplete not as helpful as it could be (e.g. feature-1123
, feature-1124
).
P.S.
Speaking of autocomplete in git, here's how to install it for the Bash shell, if you don't already have it set up. This provides the ability to type a couple of characters and press tab to autocomplete the rest of the command (or see all possibilities if there's more than one match).
P.P.S.
The same trick can be used to navigate between two directories in the terminal. Try typing cd -
at the command prompt and the current directory will change to the one you were in last:
[~/test-directory] | master $ cd /a/long/subdirectory/path
[~/test-directory/a/long/subdirectory/path] | master $ cd -
[~/test-directory] | master $
Top comments (6)
haha yes! good trick. @maestromac taught me that a few months ago :)
Also look into git worktrees if you want to keep two or more branches checked out at the same time.
That one is very well kept secret though. Used it on various occasions when stashing wasn't interesting.
Noted
This also works for the
cd
command. It takes you to the previous directory.Good Trick!!