How to Update Branch From Master in Git
While working in Git with many developers and analysts working simultaneously on the various branches, we could come across many issues. One common issue is when one team member makes changes in his local branch, while others work on that remote branch and then combine their changes to the remote master branch.
Furthermore, if we push the working local branch and don’t pull the remote master branch, then we have to rewrite other developers’ changes in the remote master branch.
This article is all about the git update master branch
commands, and we will discuss the complete Git update branching model. The feature branching
can be found in most modern and unique version control systems.
In Git, the most important and useable feature is branching which is a part of our daily development process. This article will guide us on updating a Git branch using the below-mentioned methods.
If we want our git feature branch to be updated with the new changes from the master branch, we need to follow either one of the following techniques:
- Merge
- Rebase
Merging creates more commitment while rebasing rewrites history in the repository.
Let’s suppose we are on any feature branch in the repository we created to add the sub-feature in our development process.
The current state of the branch is following.
git branch
* feature branch
And there are new commits available on the origin/master branch of the repository:
git fetch
From git repository
xyz88874..def74125 master -> origin/master
So how would we merge the above commit from the local branch to the master branch? We have two solutions now, the first is using the merge
commands, and the other is the rebase
commands in Git.
Update Master Branch Using the merge
Command in Git
As we have the situation where we want to merge the latest commit from the local branch to the master branch, we can use the below command to merge the commits.
git merge origin/master
When we are going to commit the changes from the local branch to the remote branch, and if we find some conflicts in it, we’ll first merge the conflicts into a single file and create a new merge commit for it. If we don’t find a conflict in the working directory, a new commit will be pushed directly to a remote branch.
Update Master Branch Using the rebase
Command
As we have the situation where we want to rebase the latest commit from the local branch to the master branch, then we can use the below command to rebase the commits.
git rebase origin/master
Rebase shifts up all diverging commits of the feature branch. This indicates that the diverging commits will now consist of \new commit hashes\
because its history will be written again into the master branch.
Additionally, if our feature branch is already pushed to the remote master branch, then we need to force push to get it to update:
git push origin feature --force
However, if other developers have checked out that feature branch, this method is not recommended, better to stick with the merge command for this scenario.
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.
LinkedInRelated Article - Git Update
- How to Update Git on Mac
- How to Git Refresh Remote Branches
- How to Update Local Branch From Remote in Git