How to Synchronize Branch With Master in Git

Abdul Jabbar Feb 02, 2024
How to Synchronize Branch With Master in Git

Git is a well-organized distributed version control system that manages source code very efficiently within the distributed team of developers. When various developers are working on different product features, and once they are finished with the complete feature, they must merge or sync the master branch with their updated work.

In Git, repositories are categories of our projects introduced on the Internet, private server, or network somewhere.

Remote repositories work as a path through which developers can share their part of the work with their teammates. They can then fetch it in their local environment.

Whenever our local branch is introduced or created, we will need to sync our local repository with the remote repository again and again once they are ready to sync and the feature tasks are completed.

Synchronizing files is the procedure of updating two or more files similarly. These are the branches of the developers’ local environment, and they will sync it to the master branch once they have to deploy whole tasks to the server and give them to the client.

Sync Branch With Master in Git

Developers of a huge project work simultaneously in a team on different tasks. Others may push some of their commits to the master branch when we work on our branch.

So to tackle this situation, we should keep our repository updated with the recent changes by pulling the latest work, which should be updated daily or two or three times a day.

Firstly, we will fetch the recently made changes by checking out the master branch. Before this, we have to commit all the changes to our branch on the remote and then pull it from upstream through the following Git commands:

git checkout master
git pull upstream master

By doing this, our local master branch gets updated with the master branch, but the feature branch remains the same. For updating it, we will also rebase it using the Git command mentioned below:

git checkout my_feature_branch
git rebase master

Though it is a smooth process, if Git isn’t responding to automatic merge, it will lead to a merge conflict. We have to remove this conflict by opening the conflicted file that is present in the text editor or if you have the visual studio code.

More than one conflict may be seen in the same file with the red marker indications. Resolve it manually by editing the file and then adding the file to the index, followed by rebasing, as follows:

git add path/to/file
git rebase --continue

Now that both branches, i.e., the master branch and feature branch, are updated, and the feature branch changes are also ready, we will merge them into master, as follows.

git checkout master
git merge my_feature_branch

This will keep our feature branch in sync with the master branch, and all of our latest code will be merged into the main branch.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn