Git Previous Branch

Earlier this week, I came across this article, which had one tip that I never knew that I imagine will save me a lot of time moving forward.

Similar to how, in the shell, you can use cd - to jump to the last directory before this one, you can do the same with git to jump to the previously checked out branch, by doing git co -.

~/Developer/kmp/sdui main                                                        17:10:55
❯ g co wasm
Switched to branch 'wasm'

~/Developer/kmp/sdui wasm                                                        17:11:02
❯ g co -
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

I alias g to git here since it saves a lot of time. If you don’t already have co as an alias for checkout, you can do this by adding an alias in your .gitconfig file:

[alias]
  st = status
  ci = commit
  co = checkout
  br = branch