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 just on

Git
~/tips/tip-git-switch-previous-branch

git switch feature/login

Jumps back to the branch you were on before your last branch switch.

The shortest branch command I know is two words and a dash. I like it a lot.

git switch - jumps back to the branch you were on before your last switch. The git docs say a dash is the same as @{-1}, and that it helps with switching quickly between two branches or for undoing a switch you made by mistake.

git switch feature/login
# poke around, then:
git switch -

Say you are on feature/login and you hop to main to check something. Type git switch - and you are back on feature/login. Do it again and you are on main again. It works like the back button on a remote.

You can go further back with the number form. git switch @{-2} takes you to the branch you were on two switches ago.

One caveat. The dash follows your switch history, not your commit history. If you checked out three other branches in between, the dash only gives you the last one. And as always, if you have uncommitted changes that clash with the target branch, git refuses to switch until you commit or stash them.

Further reading: git switch documentation, On this site: use git worktree to check out two branches at once, On this site: git switch – takes you back to the branch you were on

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