How to Update Your Pull Requested Branch with Original Repository’s Master
Let’s say we have submitted a pull request to a public repository long ago. That repository’s main branch is updated often since then. Now that repository’s maintainer wanted to merge our pull request but our pull request branch is some number of commits behind their master branch. Therefore we need to update our pull request (branch) without any conflicts.
I had a similar experience.
In this article, I will explain you how to update pull request branch.
Let’s dive in!
This is how my branch was looked like before updating it. It says that my branch is 3 commits ahead of my forked master but 7 commits behind with Vonage:master (original repo’s master branch).
Let’s sync our forked master branch
Switch to our master branch.
$ git checkout master
Let’s add original repository as an upstream repository.
$ git remote add upstream [HTTPS]
$ git fetch upstream
git fetch upstream
fetches all of the branches from the original repository. This also downloads all of the required commits and files from the that repository.
Let’s merge upstream’s master with our forked master.
$ git merge upstream/master
Now our forked master branch is up to date with upstream’s master branch (original master branch).
Let’s log the commits to ensure our forked master is up to date with original repository’s master.
As you can see in the above diagram my forked master and original repo’s master is in sync.
NOTE: If you made a pull request from your forked master branch (which is not a best practice) after completing above steps, your pull request will be updated as the above steps helps to sync forked master branch.
Create local branch
I have deleted that branch previously hence I created that branch locally again. My pull requested branch on remote is remove-unnecessary-refs
.
$ git checkout -b remove-unnecessary-refs
Now we need to fetch, download content from that remote repository branch.
$ git pull origin remove-unnecessary-refs
As Atlassian Git Guide says,git pull
executes git fetch
to download the remote content for the active local branch and later executes git merge
to create a merge commit for the new remote content. If you have pending changes in progress this will cause conflicts and kick-off the merge conflict resolution flow (this is what happened to me above).
NOTE: You can consider git fetch
the 'safe' version when downloading content from a remote repo instead of git pull
.
In this scenario, we need to manually merge it by committing it.
$ git status
$ git add .
$ git commit -m "Removing unnecessary refs"
Let’s log to view the commit history.
$ git log --oneline
38c4c29
is the commit id of original repository master branch. Commit id 5b78bb6
is our updated, merged content.
Let’s update this change to remote branch.
$ git push origin remove-unnecessary-refs
As you can see in the above diagram, c2bcd59
is the previous commit id of that remote branch before syncing and commit id 5b78bb6
that contains updated change.
Then I added another commit to this branch (adding myself to the CONTRIBUTORS.md).
Before syncing pull requested remote branch I made 3 commits. At the time of syncing I made a commit (git pull
) and later I made a commit to CONTRIBUTORS.md. Altogether I made 5 commit to that branch.
When reviewing commits to that branch in GitHub, it looked like this:
Latest commit to that branch looked like this:
Now our pull requested branch is up to date. GitHub repository maintainer can merge our pull requests.
Finally, maintainer merged my pull request!
Few Days Later
Few days later remove-unnecessary-refs
was some commit behind original repo’s master.
$ git checkout remove-unnecessary-refs
$ git fetch upstream
Now, let’s merge our branch with upstream/master.
$ git merge upstream/master
Let’s push it.
Let’s see in GitHub.
Success!
Similarly I synced forked master branch.
Takeaway
To update PR branch:
$ cd [name of the repository]
$ git checkout [branch name]
$ git fetch upstream
$ git merge upstream/master
$ git push origin [branch name]
Thank you for reading.
Happy Coding!