When working with your Git repositories, there will likely come a time when you wish to rename a Git branch that you’re working with. First, we’ll cover renaming a local Git branch and then renaming a remote Git branch.
When you want to rename a Git branch locally, you can do so using the git branch command with the -m option.
If you want to rename the current branch you have checked out, you can simply pass in your desired new name:
git branch -m <new-branch-name>
If you want to rename a different branch than the current branch you have checked out, you can use the same command, but pass in the current name of the branch followed by the new branch name:
git branch -m <branch-name><new-branch-name>
If the branch you are renaming has already been pushed to a remote, the upstream branch won't change just because you renamed your local branch.
You can push the new branch to the remote and update the upstream using the git push command with the -u (or --set-upstream) option:
git push origin -u <new-branch-name>
Now, you will just have to delete the old branch from the remote using the git push command with the -d (or --delete) option:
git push origin -d <branch-name>
Learn more about how to delete a remote branch in Git.
Now that we’ve shown you how to rename Git branches using the CLI, we’ll show you how to quickly do the same things using the GitKraken Git GUI for Windows, Mac, and Linux.
To rename a local branch using GitKraken, simply right-click on the branch and choose the Rename option from the context menu.
Next, type in your desired new branch name and hit Enter. The local branch will be renamed.
To update the remote branch you will have to change the upstream by right-clicking the branch and selecting Set Upstream from the context menu.
Next, enter the new branch name and click Submit. You can then push the new branch up to the remote using the Push toolbar button, or from the context menu.
Lastly, you'll have to delete the old remote branch by right-clicking it and selecting Delete origin/branch-name from the context menu.
The process to rename a Git branch is much faster using an intuitive tool, like the GitKraken Git GUI, and you can avoid making changes to the wrong data by mistake.