Branches are essential to the software development process as they’re pointers to a commit. Git allows you to rename a branch easily through the git branch -m command. However, if you’ve pushed a branch to a remote repository with an incorrect name, there are a few more steps involved to rename the Git branch.
This article explains how you can rename the local and remote Git branch in a few easy steps.
How to rename a local Git branch?
As mentioned above, the git branch -m command makes it a breeze to rename a Git branch. Follow the steps below to rename a local Git branch.
Step 1: Switch to the local branch you want to rename by typing the following.
git checkout <old_name>
Step 2: Rename the local branch by typing the following.
git branch -m <new_name>
This will rename the local Git branch. If you’ve already pushed the wrong branch name to a remote repository, follow the guide below.
Also read: How to remove untracked files in Git?
How to rename a remote Git branch?
While renaming a local Git branch is a two-step process, you can’t directly rename a remote Git branch. You’ll first need to push the renamed local branch and then delete the branch with the incorrect or old name.
That said, the first two steps when renaming a remote Git branch are the same as the guide above for renaming a local Git branch.
Step 1: Switch to the local branch you want to rename by typing the following.
git checkout <old_name>
Step 2: Rename the local branch by typing the following.
git branch -m <new_name>
Step 3: Now push the renamed local branch (<new_name>) and reset the upstream branch.
git push origin -u <new_name>
Step 4: Now, to complete the git branch renaming, you’ll need to delete the remote branch with the <old_name>.
git push origin --delete <old_name>
Both the local and remote Git branch will be renamed.
Also read: How to fix Gitignore not working issue?