How to Replace Master Branch in Git, Entirely, from Another Branch?

Better Stack Team
Updated on June 24, 2024

To replace the master branch entirely with the contents of another branch, you can use the git checkout and git reset commands. Here's how you can do it:

 
# Checkout the branch you want to replace master with
git checkout <branch-to-replace-master>

# Reset the master branch to the same commit as the branch
git reset --hard HEAD

# Force-push the updated master branch to the remote repository
git push --force origin master

Replace <branch-to-replace-master> with the name of the branch you want to replace master with.

Note:

  • This operation rewrites the history of the master branch, so use it with caution, especially if the master branch is shared with others.
  • After running these commands, the master branch will be identical to the <branch-to-replace-master> branch, including its commit history and contents.
  • Make sure to communicate with other collaborators if they are also working on the master branch to avoid conflicts.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.