How to replace master branch in Git, entirely, from another branch?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In this article, we will explore how to replace the master branch in a Git repository entirely from another branch. This guide is useful when you want to reset the master branch to the state of another branch, for example, due to experimental improvements or to roll back changes.
Understanding Branches in Git
Git is a distributed version control system used widely in software development for source code management. A branch in Git essentially represents a separate line of development. The master branch is traditionally the default main branch in a repository; however, it's important to note that many repositories use main or other names as the default branch now.
Why Replace the master Branch?
- Resetting to a stable state: If a feature branch has been thoroughly tested and deemed more stable, replacing
masterentirely might be a better choice. - Experimental features: You might choose to replace the
masterwith an experimental branch if initial results are promising. - Cleanup after major mistakes: Sometimes, the easiest way to revert mistakes is to replace the
masterbranch with another known good branch.
The Process to Replace master Branch
Given that Git operations can affect the state of your repository significantly, it is critical to ensure you have backups or clone the repository before making any major changes.
Step-by-step Guide
- Ensure Your Work is Pushed to the RemoteMake sure all important changes have been committed and pushed to avoid any loss of data:
- Check Out the Replacement BranchThis is the branch you want to use to replace the
masterbranch:
- Update Local
masterBranch from RemoteFirst, ensure the localmasterbranch is synched with the remote:
- Perform the Branch ReplacementReset the
masterbranch to point at the replacement branch:
Now, the master branch has the same content as replacement-branch but retains its history.
- Force Push to Remote
masterTo update the remotemaster, force a push. Warning: This is a destructive operation and can potentially rewrite history for collaborators:
Considerations
- Backup and Testing: Always back up your local repository before making these changes. Consider testing the replacement on a separate clone first.
- Communication: Notify your team about such a significant change, especially if using force push.
- Access Control: Ensure you have the necessary permissions to perform these operations on the repository.
Conclusion
Replacing the master branch with another branch in Git is a powerful operation that can reset your project's main line in significant ways. Performing such an operation requires considerations for backup, team communication, and permissions.
Summary Table
| Step | Command | Description |
| Backup and Commit Changes | git add .
git commit -m "..." | Ensure all your work is committed and pushed. |
| Checkout Replacement | git checkout replacement-branch | Switch to the branch you want to promote to master. |
Sync Local master | git checkout master
git fetch origin
git reset --hard origin/master | Ensure local master matches the remote. |
Replace master Content | git checkout replacement-branch
git merge --strategy=ours master
git checkout master
git merge replacement-branch | Replace using a merge strategy that retains history. |
| Force Push to Remote | git push origin master --force | Update the remote master to match your local changes. |
These steps will ensure that your master reflects the exact state of your replacement branch, suitable for use cases where the replacement branch encompasses critical fixes or improvements. Always use caution when force pushing and altering mainline branches.

