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 with detach lets you look at an old version without a branch

Git
~/tips/tip-git-switch-detach-look-around

git switch --detach v1.4.0

Checks out any tag or commit to test it, without creating or moving a branch.

To run an old release and check whether a bug was already there, you don’t need to make a branch. Detached HEAD mode is made for that.

git switch --detach v1.4.0

Your working tree now matches the tag v1.4.0, and HEAD points at that commit directly instead of at a branch. Run it, test it, read the code. When you’re done, git switch - or git switch main takes you back.

git switch insists on --detach here. Without it, git switch v1.4.0 refuses, because a tag isn’t a branch. That’s on purpose: the older git checkout v1.4.0 detaches silently, and people end up committing on a detached HEAD without realising.

You can make commits while detached, for an experiment. They just don’t belong to any branch. To keep them, give them a branch before you leave:

git switch -c fix-for-1.4

If you forget and switch away, git warns you that you’re leaving commits behind and prints their hash. They’re still recoverable through git reflog for a while, until garbage collection cleans them up. So when an experiment turns into something useful, I’d create the branch straight away.

Further reading: git-switch documentation, git-checkout documentation: detached HEAD

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