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 fetch origin main:main updates main without switching to it

Git
~/tips/tip-git-fetch-update-main-without-switch

git fetch origin main:main

Fast-forwards your local main from the server while you stay on your feature branch.

Before you rebase a feature branch, your local main should be up to date. Switching to main, pulling and switching back is three commands and touches your working tree twice.

git fetch origin main:main

The part after the space is a refspec: take main from the remote and write it to your local main. Git only does that when it’s a fast-forward, so local commits on main are never thrown away. If main has diverged, you get ! [rejected] main -> main (non-fast-forward) and nothing changes.

Then rebase as usual:

git rebase main

I like that your working tree isn’t touched during the fetch, so editors and running dev servers don’t see files jumping around.

This doesn’t work for the branch you’re on. Git refuses with refusing to fetch into branch ... checked out, because updating it would leave your working tree out of sync. For the current branch, use git pull as normal. The same goes for a branch checked out in another worktree.

Never add a plus in front of the refspec here, as in +main:main. The plus forces the update even when it isn’t a fast-forward, and then any local commit on main that isn’t on the server is gone from the branch.

Further reading: git-fetch documentation

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