Git

How to Keep a Branch Synchronized/Updated With Master?

When developers work on a team project, it is necessary to keep their branch updated with the master branch due to many reasons. More specifically, keeping a branch synchronized with the master helps in avoiding conflicts, making merging easier, collaborating efficiently, and staying up-to-date. Moreover, it helps you to ensure that the project is consistent, and every team member is working on the latest version of the code.

This write-up will explain the method of keeping a branch synchronized or updated with the master branch.

How to Keep/Maintain a Branch Updated or Synchronized With the Master?

To keep a branch synchronized or updated with the Master branch, follow the provided steps.

  • Navigate to the local repository.
  • Redirect to the “master” branch and pull the content of the remote “master” branch in it.
  • Navigate to the “feature” branch and merge/combine it with the “master” branch.
  • Go back to the “master” branch and combine it with the “feature” branch.
  • Push changes of both fully merged local branches to the remote branch.
  • Verify changes in both branches.

Step 1: Redirect to Local Repository
First, switch to the desired local repository using the below-provided command:

cd "C:\Git\demoRepo"

Step 2: View Current Branch Content
Then, list the content of the current working branch:

ls

It can be observed that the “feature” branch contains “Demo.txt” and “Test.txt” files:

Step 3: Switch to “master” Branch
Now, utilize the following command and navigate to the “master” branch:

git checkout master

Step 4: View “master” Branch Content
Enter the “ls” command to display the “master” branch’s content:

ls

The below output indicates that the “master” branch contains “File1.txt” and “File2.txt” files:

Step 5: Pull Remote Branch Content
Now, pull the content of the remote “master” branch in the current branch:

git pull origin master --allow-unrelated-histories

Here, the “–allow-unrelated-histories” option is utilized so that Git will permit the user to merge the branches of both unrelated local and remote repositories:

Step 6: Switch to “feature” Branch
After that, navigate to the “feature” branch through the provided command:

git checkout feature

Step 7: Merge “master” Branch Into “feature” Branch
Next, use the following command to merge the “feature” branch with the “master” branch:

git merge master

Step 8: Move Back to “master” Branch
Next, switch back to the “master” branch:

git checkout master

Step 9: Merge “feature” Branch Into “master” Branch
After that, merge the “master” branch with the “feature” branch:

git merge feature

Step 10: Push Changes to Remote Branch
Now, push the content of both local merged branches to the remote “master” branch:

git push origin master

Step 11: View Commit History
Then, check the Git log to view the changes:

git log --oneline

In the below screenshot, it can be seen that the HEAD is pointing to the two fully merged local “master” and “feature” branches and also pointing to the target remote “origin/master” branch in which we pushed merged local branches content:

Step 12: Verify Changes
Lastly, check the content of both branches to ensure changes:

As you can see, the content of both the “master” and “feature” branches are the same which indicates that both branches are fully merged.

Conclusion

To keep a branch synchronized or updated with the Master branch, first, navigate to the local repository. Then, redirect to the “master” branch and pull the content of the remote “master” branch in it. After that, switch to the “feature” branch and merge/combine it with the “master” branch. Next, switch back to the “master” branch and merge/combine it with the “feature” branch. Finally, push changes of both fully merged branches to the remote branch and verify. This write-up explained the method to keep a branch synchronized or updated with the master branch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.