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 -m renames a branch, and two pushes rename it on the remote

Git
~/tips/tip-git-branch-rename-local-remote

git branch -m old-name new-name

Renames a branch locally and on the server without losing its commits.

A typo in a branch name, or a feature that turned into something else: renaming the local branch is one command. The remote takes a little more.

git branch -m old-name new-name

-m moves the branch to a new name, with its commits and its reflog. If you’re on the branch, git branch -m new-name with only one name renames the current branch.

A remote has no rename command, so you push the new name and delete the old one:

git push -u origin new-name
git push origin --delete old-name

The -u makes the renamed branch track origin/new-name. Without it, it still tracks the old remote branch, which is about to disappear.

Watch out with open pull requests. On GitHub, deleting the old branch closes a pull request that was opened from it, and you’ll have to open a new one. GitHub’s own rename button in the web interface handles that better: it renames the branch on the server, retargets pull requests and shows other people the commands to update their clones. For a branch that others have checked out, I’d use that and then just run the git branch -m line locally. Also, -m refuses to overwrite an existing branch; -M forces it, and that one does throw the other branch away.

Further reading: git-branch documentation, GitHub: renaming a branch

Going deeper: Moving on from hdtv: how to export your TV show list to Trakt.tv

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