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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github