brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git switch – takes you back to the branch you were on

Git
~/tips/tip-git-switch-dash

git switch - # back on your feature branch

Jumps back to the previous Git branch without typing its name.

Hopping between a feature branch and main all day is normal, and typing the branch name every time is not fun. Git has a shortcut for it, the same way cd - works in your shell.

git switch - returns to the branch you were on before the last switch:

git switch main
git switch -     # back on your feature branch
git switch -     # and on main again

The dash is shorthand for @{-1}, the previous branch in your reflog. The older git checkout - does the same, but switch is the newer command and only changes branches, which makes it harder to mess up.

The caveat is the usual one: if you have uncommitted changes that conflict with the other branch, Git refuses to switch. Commit them or use git stash first. It does not throw your work away.

Further reading: git switch (Git documentation), On this site: git switch – takes you back to the branch you were just on, On this site: sort your branches by last commit with branch.sort

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link