How to do a Git Rebase for a Pull Request

You don’t overcome challenges by making them smaller but by making yourself bigger.

 This would be my first article after a while, I've been busy with a few things ;-)

In this article, I'll be writing about how to do a git rebase especially when you've made alot of commits and you'll like it to be seen as a single commit with all your changes intact, majorly so as to avoid conflict.

For those that contribute to projects on Github/Bitbucket, conflicts can't be avoided especially when alot of contributors are involved.
Performing a Rebase can be the solution when you've made multiple commits and you want to make a Pull Request.

The most common reason why pull requests go stale is due to conflicts: if two pull requests both modify similar lines in the same file, and one pull request gets merged, the unmerged pull request will now have a conflict.

Specifically, you've made some commits overtime, of which you'll be rebasing into a single commit.

What is a rebase?

 git rebase in its simplest form is a command which will merge another branch into the branch where you are currently working, and move all of the local commits that are ahead of the rebased branch to the top of the history on that branch.

How do I rebase?

Let's say you have cloned a fork of project1 repo like so:
$ git clone 

From the main repo, say:

Add the main repo to your clone, this needs to be done only once:

$ git remote add owner
This can be confirmed using: $ git remote -v 

Also fetch the latest version from the main repo with: $ git fetch owner


Squash your changes

To do this, we're going to do an interactive rebase. You can also use interactive rebase to change the wording on commit messages.

If you know the number of commits on your branch that you want to rebase, you can simply run:
$ git rebase -i HEAD~n
where n is the number of commits to rebase.

If you don't know how many commits are on your branch, you'll first need to find the commit that is base of your branch. You can do this by running:
$ git merge-base my-branch owner/master

That command will return a commit hash. Use that commit hash in constructing this next command:
$ git rebase -i ${HASH} 
where ${HASH} is the lastest commit hash.
Once you've run a git rebase -i command, your text editor will open with a file that lists all the commits in your branch, and in front of each commit is the word "pick". It looks something like this:

pick 1fc6c95 do something
pick 6b2481b do something else
pick dd1475d changed some things
pick c619268 fixing typos

For every line except the first, you want to replace the word "pick" with the word "squash". It should end up looking like this:
pick 1fc6c95 do something
squash 6b2481b do something else
squash dd1475d changed some things
squash c619268 fixing typos
Save and close the file, and a moment later a new file should pop up in your editor, combining all the commit messages of all the commits. Reword this commit message as you want, and then save and close that file as well. 
This commit message will be the commit message for the one, big commit that you are squashing all of your larger commits into.  

Git will start replaying your commits onto the latest version of master. You may get conflicts while doing so: if you do, Git will pause and ask you to resolve the conflicts before continuing. 
Finally, you want to run:
git rebase --continue 
to indicate to Git that it should continue replaying your commits. 

Perform a rebase

To rebase your branch atop of the latest version of owner, run this command in your repository:
$ git rebase owner/master

Force-push to update your pull request

When you do a rebase, you are changing the history on your branch.
As a result, if you try to do a normal git push after a rebase, Git will reject it because there isn't a direct path from the commit on the server to the commit on your branch. Instead, you'll need to use the -f or --force flag.

Set your push.default config setting to simple:
$ git config --global push.default simple

Once it's correct, you can just run:
 $ git push -f

Voila!, check your pull request. It should be updated!


Popular Posts