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 branch -u links an existing branch to a remote one, no push needed

Git
~/tips/tip-git-branch-set-upstream

git branch -u origin/main

Connects an existing local branch to a remote branch without pushing anything.

A branch without an upstream gives you There is no tracking information for the current branch on every git pull. You can fix that without pushing.

git branch -u origin/main

-u is short for --set-upstream-to. It sets the upstream of the current branch to origin/main, and git confirms with branch 'fix' set up to track 'origin/main'. From then on, git pull and git status compare with that branch. To set it for another branch, name it at the end: git branch -u origin/develop topic.

I’d use this in two cases. One is when a branch was created from a local commit and later turns out to belong to a remote branch someone else pushed. The other is when I want a local branch to follow a different remote branch than the one it was made from, for example the upstream repository in a fork instead of your own copy.

To remove the link again, there’s git branch --unset-upstream.

The remote branch must already exist in your clone, so fetch first. Setting it doesn’t push or pull anything; it only changes the config. That also means git push may still go elsewhere, since push follows its own rules in push.default. With the default simple, a push is refused when the names don’t match.

Further reading: git-branch 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