The Wayback Machine - https://web.archive.org/web/20240805214908/https://www.geeksforgeeks.org/how-to-replace-master-branch-with-another-branch-in-git/
Open In App

How to Replace Master Branch with Another Branch in GIT?

Last Updated : 04 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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.

Steps to Replace the Master Branch

Step 1: Ensure Your Working Directory is Clean

Before making any changes, ensure your working directory is clean. This means committing or stashing any uncommitted changes.

git status

If there are uncommitted changes, either commit them:

git add .
git commit -m "Save changes before replacing master branch"

Or stash them:

git stash

Step 2: Switch to the Branch You Want to Replace Master With

Checkout the branch that you want to use to replace the master branch.

git checkout feature-branch

Step 3: Update the Branch to Be Up-to-Date

Make sure your branch is up-to-date with the latest changes from the remote repository.

git pull origin feature-branch

Step 4: Delete the Master Branch Locally

Next, switch to the branch you want to preserve and delete the local master branch.

git branch -D master

Step 5: Rename the Current Branch to Master

Rename the current branch (feature-branch) to master.

git branch -m master

Step 6: Force Push the New Master Branch to Remote

Finally, 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 --force

Step 7: Verify the Changes

To 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.

Conclusion

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.


Previous Article
Next Article

Similar Reads

How to Make the Current Git Branch a Master Branch?
Managing branches effectively is a key part of using Git for version control. Occasionally, you might find yourself in a situation where the current branch you're working on should be the new master branch. This could be due to the current branch evolving to become the main line of development, or simply a restructuring of your project's branch str
3 min read
How To Replace Local Branch With Remote Branch in Git?
Managing branches in Git can sometimes require drastic measures, such as replacing a local branch entirely with its remote counterpart. This can be useful in scenarios where the local branch has diverged too much from the remote branch, leading to complexities that are easier to resolve by completely overriding the local branch. This article will g
4 min read
Git - Create a Branch From Another Branch
Branching in Git allows developers to work on isolated features or fixes without affecting the main codebase. However, what if you need to create a new branch based on the changes in another branch? That's where the concept of branching from another branch comes into play. In this guide, we'll see the process of creating a branch from another branc
3 min read
How to Create a Branch In Git from Another Branch?
Branching is a fundamental aspect of version control systems like Git, which helps developers work on multiple features or bug fixes simultaneously without interfering with the main codebase. Creating a branch from another branch is a common practice, especially when you want to build upon existing work or isolate changes for a specific feature. In
3 min read
How to Fix "master branch and 'origin/master' have diverged"?
When working with Git, you may encounter the "master branch and 'origin/master' have diverged" error. This message indicates that the local master branch and the remote origin/master branch have different histories and have gone out of sync. Resolving this divergence involves understanding the state of both branches and merging, rebasing, or resett
3 min read
How to Merge a Git Branch into Master?
If you're working with Git, merging branches is a fundamental task. Whether you're adding new features, fixing bugs, or improving documentation, merging ensures that your changes are integrated into the main codebase. In this guide, we'll walk you through the process of merging a Git branch into the master, which helps your codebase remain up-to-da
3 min read
How to Change Git Default Branch From Master?
Changing the default branch in Git from master to main has become a common practice in many development teams. This shift is part of a broader movement towards more inclusive and descriptive terminology in software development. This article will guide you through the steps required to change your default branch from master to main, covering both lo
4 min read
How To Rebase a Local Branch Onto a Remote Master in Git?
Git, a powerful version control system, offers various methods to manage branches and integrate changes. Rebasing is one such technique used to incorporate changes from one branch onto another, creating a linear history. This article will walk you through the process of rebasing a local branch onto a remote master in Git. Steps To Rebase a Local Br
3 min read
How to Create a Git Branch From Unstaged/Uncommitted Changes on Master?
When working with Git, you might find yourself in a situation where you've made changes to your files but haven't committed them yet. You realize that these changes should be in a new branch rather than the current one, typically master or main. This guide will show you how to create a new branch and move your unstaged or uncommitted changes to it
3 min read
How To Get Changes From Master Into a Branch in Git?
Adding changes from the master branch to a different branch is a typical Git task. This process makes sure that the branch you are working on is up-to-date with all of the main codebase's modifications and updates. This can be accomplished in a variety of ways, each appropriate for a particular scenario and workflow. In order to make the process un
2 min read
Article Tags :
three90RightbarBannerImg