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:
Step 2: View Current Branch Content
Then, list the content of the current working branch:
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:
Step 4: View “master” Branch Content
Enter the “ls” command to display the “master” branch’s content:
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:
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:
Step 7: Merge “master” Branch Into “feature” Branch
Next, use the following command to merge the “feature” branch with the “master” branch:
Step 8: Move Back to “master” Branch
Next, switch back to the “master” branch:
Step 9: Merge “feature” Branch Into “master” Branch
After that, merge the “master” branch with the “feature” branch:
Step 10: Push Changes to Remote Branch
Now, push the content of both local merged branches to the remote “master” branch:
Step 11: View Commit History
Then, check the Git log to view the changes:
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.