January 7, 2016

Git – rebasing + squashing process, followed by merging via pull request

Let’s say I have a feature branch called “feature-x” which I want to first rebase onto the “master” branch. Then it is first best practice to squash all my feature-branch’s commits into a single commit and then merge it into the master branch via a pull request.


You can find all my latest posts on medium.

first checkout the develop branch and pull in the latest changes.

$ git checkout master
$ git pull

Now do the rebasing:

$ git checkout feature-x
$ git rebase master 

This does the rebasing without any squashing. Also if this fails to complete because of conflicts, then you try cancelling by running `git rebase –abort` then consider doing git merge instead.

Now we do the squashing, using the “f” option, for each commit, the following will open up the git rebase (i)nteractive mode:

$ git rebase -i master

Now we use the following to create a new overall commit message:

$ git commit -v --amend

Tip: Here are some advice on how to write good commit messages.

Now check everything looks ok:

$ git log -p
$ git log -p -n1 # displays info about only the latest commit.

Now update your git commit’s timestamp:

$ git commit --amend --date="$(date -R)"

Now push your changes to your bitbucket/github server. the following overwrites the entire history of the current branch. Hence it replaces the individual commits with squashed commits.

$ git push --force origin feature-x

# login into your git server using firefox, and then create the pull request.

Merge approach

git checkout master
git pull
git checkout my-branch
git merge master

Some useful links:

The aim of this approach is that it you don’t end up cluttering up your main master branch with lots of commits.