How to Rename Git Local and Remote Branches
When collaborating with a team on a project, sometimes it is necessary to rename local or remote branches in Git. In this tutorial, we are going to show how to rename Git local and remote branches with the steps described below.
To rename a branch in Git we should use the git branch command followed by -m option.
- If you are required to change the current branch’s name in your project, then run the following:
git branch -m new-branch
- If it is needed to rename another branch in your project, then run the following:
git branch -m old-branch new-branch
- After pushing the <old-branch> branch to the remote repository delete it:
git push origin --delete <old-branch>
- Push the <new-branch> local branch and reset the upstream branch with the git push command:
git push origin -u <new_branch>How to rename git local and remote branches Discuss the topic above on StackDev and get paid for that! Get Paid
There also exists a fast way to renaming local branches:
git branch -m old_branch new_branch
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
Git branches are an important part of the everyday workflow. Branches are a pointer to a snapshot of the changes you have made in Git. Branching helps cleaning up the history before merging it. Branches represent an isolated line of development. They are considered as a way to request a new working directory, staging area, and project history. The isolated lines of development for two features in branches make it possible to work on them in parallel and make the master branch free from questionable code. The git branch command creates, lists and deletes branches not allowing to switch between branches or put a forked history back together.
Local and Remote Branches
The local branch is a branch existing on the local machine. It can be seen only by the local user. The remote branch is a branch on a remote location. A remote-tracking branch is a local copy of a remote branch. Assuming a newly-created <NewBranch> is pushed to origin using the git push command and -u option, a remote-tracking branch named <origin/NewBranch> is created on your machine. The remote-tracking branch tracks the remote branch <NewBranch> on the origin. Update and sync the remote-tracking branch with the remote branch using the git fetch or git pull commands. A local tracking branch is a local branch tracking another branch. Local tracking branches mostly track a remote-tracking branch. When pushing a local branch to the origin with git push -u, the local branch <NewBranch> tracks the remote-tracking branch <origin/NewBranch>.
Thanks for your feedback!Do you find this helpful?