![]() |
VOOZH | about |
In Git, the "master" branch traditionally serves as the primary development branch in many repositories. However, there are situations where you might want to replace the contents of the "master" branch with those of another branch. This could be due to a variety of reasons, such as renaming the default branch to something more inclusive or reflecting a new primary development focus. This guide will walk you through the steps to replace the "master" branch with the contents of another branch in Git.
Before making any changes, ensure your working directory is clean. This means committing or stashing any uncommitted changes.
git statusIf there are uncommitted changes, either commit them:
git add .
git commit -m "Save changes before replacing master branch"
Or stash them:
git stashCheckout the branch that you want to use to replace the master branch.
git checkout feature-branchMake sure your branch is up-to-date with the latest changes from the remote repository.
git pull origin feature-branchNext, switch to the branch you want to preserve and delete the local master branch.
git branch -D masterRename the current branch (feature-branch) to master.
git branch -m masterFinally, force push the new master branch to the remote repository. Be very careful with this step as it will overwrite the remote master branch.
git push origin master --forceTo ensure everything went smoothly, you should verify that the remote master branch has been updated.
git fetch origin
git checkout master
git log
Review the commit history to confirm that it matches the branch you replaced it with.
Replacing the master branch with another branch in Git involves careful steps to ensure that the new branch content accurately reflects in the master branch. By following the outlined steps, you can effectively manage significant changes in your project. Always ensure proper communication and backup before performing such operations to maintain the integrity of your codebase and team workflow.