How to Make the Development Branch Identical to the Master Branch

John Wachira Feb 02, 2024
How to Make the Development Branch Identical to the Master Branch

This article discusses resetting a development branch so that it is identical to the master branch. Supposing you have cloned a Git repository and created a development branch off the master branch.

After playing around with the code, you realize your branch is messy and want to reset it back to match the master branch. How do you go about this?

Make the Development Branch Identical to the Master Branch

We can employ two methods to reset a development branch to master. The simplest and cleanest is recreating the pointer.

To recreate the pointer for the development branch, switch to the master branch and run the command below.

$ git branch -f development master

This will reset the commit history of the development branch to match the commit history of the master branch.

An alternative for this involves the git reset command. First, switch to the development branch and run the command below.

$ git reset --hard master

This command will have the same effect as the git branch -f command. However, both of these commands will do away with the changes in the development branch that are not present in the master branch.

If you do not want to eliminate these changes, you can instead create a commit that mirrors the current state of your master branch. To do this, follow these steps:

First, switch to the development branch with the git checkout development command. Merge the master branch to the development branch without creating a commit, as shown below.

$ git merge master --no-commit

This command will merge changes but stop the merge before the commit part. Using the command below, you can check out the changes from the master branch from the index.

$ git checkout --theirs master .

Do not forget to include the . at the end of the command. What’s left is committing the changes.

$ git commit -m"Resetting development to master"

This will preserve the changes not present in the master branch and mirror the current state of the master branch.

In a nutshell, you can reset any branch to master by recreating the pointer or with the help of the git reset --hard command. Both ways will do away with the changes not present in the master branch.

If you want to keep these changes, use the last method we discussed.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Reset