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 “develop” 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 develop branch via a pull request.

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

$ git checkout develop
$ git pull

Now do the rebasing:

$ git checkout feature-x
$ git rebase -i develop 

At this stage just use vim’s “wq” option to save and quit. This does the rebasing without any squashing.

Now we do the squashing, using the “f” option, for each commit:

$ git rebase -i develop

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.

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.