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 -c copies a branch together with its settings

Git
~/tips/tip-git-branch-copy-with-config

git branch -c feature feature-backup

Makes a copy of a branch that keeps its upstream and other branch config.

Before a risky rebase I like a backup of the branch. git branch -c makes one that also keeps the branch’s settings.

git branch -c feature feature-backup

-c copies the branch, its reflog and its configuration. That last part is the difference with a plain git branch feature-backup feature, which only creates a new pointer at the same commit. After -c, the copy has the same upstream:

$ git config --get-regexp '^branch\.feature-backup'
branch.feature-backup.remote origin
branch.feature-backup.merge refs/heads/feature

With only one name, it copies the current branch: git branch -c feature-backup. You stay on the original branch.

Then rebase away. If it goes wrong, git reset --hard feature-backup on the original brings everything back, and git branch -D feature-backup removes the copy when you’re happy. The reflog would save you too, but a named branch is easier to find.

Because the copy tracks the same remote branch, a careless git push from the copy could try to push to feature on the server. With the default push.default=simple, git refuses since the names differ, but I’d still remove the upstream from a backup with git branch --unset-upstream feature-backup.

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