
After renaming a branch use the Git Push command to push the new branch to the remote repository and then delete the existing remote branch with the old name. If we are working with a remote repository then it is important to push our local changes to the remote. If you would like to rename the current branch in which you are working locally, you need to.
Renaming git branch how to#
The Git Branch command can be used to rename the branch. How To Rename Local Git Branch Rename the current local branch. Each branch is identified with its unique name and there may be cases where we need to rename our branches. $ git push -delete Summaryīranches are very useful when developing new features or experimenting with our project. Use the -delete option with Git Push to do this.
Now, we need to remove the branch with the old name from the remote repository. Renaming Git Branch Start by switching to the local branch which you want to rename: git checkoutThe -u flag is used to set the remote branch as the upstream branch for our local branch. This will create a new remote branch with a new name. Use the Git Push command with the -u flag after renaming the local branch by using the above-mentioned command. It is important to rename the corresponding remote branch when updating the name of the local branch. Pick the branch you need and use a command of the form 'git checkout -b new-branch-name origin/remote-branch-name.'. Having different names for branches in the local and remote repositories may lead to confusion while pushing or pulling. To checkout a branch from a remote repository, use the 'git fetch' command, and then 'git branch -r' to list the remote branches.
Push the new branch to create a new remote branch: git push originIf the branch exists on the remote Git server, continue to the next steps.
Our local branch may have a corresponding branch with the same name in the remote repository. Rename the local branch: git branch -mTo check whether the name was updated run the Git Branch command again.
Renaming git branch update#
Next, we update the name using the -m flag with Git Branch. We first use the Git Branch command to see the current branch name(oldName) in this case. You will need to rename the remote branch and all local branches. Consider the following example where we are trying to change the name of a branch. The idea of renaming the master branch scared the git out of me, but its easy and quick. Use the Git Branch command to check whether the name was updated or not. We just need to mention the old branch name along with the new name.

We can also use the above command to rename a branch without switching to that branch. The following command will rename the currently checked-out branch. This can be done by using the Git Checkout command or the Git Switch command. Remember to first navigate to the branch whose name you wish to change. Let's learn how to rename Git branches.īranches can be renamed by using the Git Branch command with the -m flag. There may be cases when we want to rename an existing branch so that it better describes what the branch is about. They help the developers by providing them an independent environment to build features without worrying about affecting the rest of the project. First we must push the update to the branch’s previous name, and secondly we push our newly named branch with its refs.Branches are an important part of any Git repository. In order for our changes to be applied to the remote branch we must perform two tasks. Push the renamed branch to the remote repository: git push origin <.
git branch -m bad-branch-name feature/add-comments Updates Remote Repository Rename the branch using the git branch command: git branch -mThe following example renames a branch named “bad-branch-name” to “feature/add-comments”. The difference is we must target the branch we want to name. Renaming a non-active branch is similar to renaming the active branch.

Using the long form variant of the move/rename flag git branch -move feature/add-comments Rename a Non-Active Git Branch To rename the current branch we use the move/rename flag, as mentioned earlier.įor example, if our current branch is named “add-comments”, but should have been named “feature/add-comments”, we simply just specify the branch’s new name with the rename flag. The Move/rename flag can be specified a number of ways. When a branch is renamed or moved the reflog is also updated. Renaming a branch falls under the branch command of Git, and it is accomplished by using the Move/rename flag. This tutorial will show you how to do so.

You may find yourself in a position where you need to rename a local Git branch.
