Git
Repository Management
Programming
Command Line
Software Development

Rename master branch for both local and remote Git repositories

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In software development, the term "master" has been conventionally used as the default branch in Git, a version control system. However, in recent years, there has been a shift towards more inclusive terminology, leading to the renaming of the "master" branch to names like "main" or others by individuals and communities. Changing a branch name in Git can impact both local environments and repositories on remote servers such as GitHub, GitLab, or Bitbucket. This article provides a detailed guide on how to rename the master branch in both local and remote Git repositories.

Understanding Git Branches

Git branches are effectively pointers to commits in your project's history. The "master" branch has traditionally been the default branch where the stable code lives. When you initialize a new Git repository with git init, Git creates this branch by default. However, this can be changed with minimal steps.

Steps to Rename The Local Branch

  1. Switch to the master branch: You need to be on the branch you want to rename. Make sure you switch to it with:
 
   git checkout master
  1. Rename the branch locally: To rename the branch locally, use the following command:
 
   git branch -m new-branch-name

where new-branch-name is the name that you want to give to your branch.

Steps to Rename The Remote Branch

Renaming the remote branch is a bit more elaborate and needs a few more steps to ensure that everyone collaborating on the repository updates their configuration.

  1. Push the new branch and reset the upstream branch: First, push the renamed local branch to remote and reset the upstream branch:
 
   git push origin --delete master
   git push origin new-branch-name
  1. Reset the upstream branch for your new branch name: To ensure you and others can track the renamed branch when pulling updates, set the new branch as upstream using:
 
   git push --set-upstream origin new-branch-name
  1. Update any cloud-based pull requests and environments: If you use GitHub, GitLab, or similar, go to each open pull request and change the base branch for each. Update any continuous integration/continuous deployment (CI/CD) pipelines that reference the old branch name.
  2. Inform your team: Make sure to inform all stakeholders about the branch name change so they can update their local repositories. Share commands they should run in their local repositories to avoid confusion:
 
1   git branch -m master new-branch-name
2   git fetch origin
3   git branch -u origin/new-branch-name new-branch-name
4   git remote set-head origin -a

Possible Issues and Troubleshooting

  • Local caching of branch names: Some Git clients cache branch names, and manual intervention might be required to clear this data.
  • Permissions: Ensure you have the necessary permissions to make changes to the remote repository.
  • Collaboration disruptions: Coordinate closely with your team to minimize disruptions, especially if your repository has many active contributors or complex workflows.

Summary Table

ActionCommandDescription
Rename local branchgit branch -m new-branch-nameChanges the local branch name.
Delete old remote branchgit push origin --delete masterRemoves the old branch from remote.
Push new branchgit push origin new-branch-nameAdds the new branch to remote.
Set new upstreamgit push --set-upstream origin new-branch-nameSets the new branch as the default for future pulls and pushes.

Conclusion

Renaming a branch in Git involves both local and remote changes. It's crucial for maintaining a seamless workflow in software development projects. With proper coordination and execution of the above steps, teams can transition smoothly to a new branch naming convention, fostering an inclusive environment and aligning with modern development practices.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.