Git/Rebase

Interactive Rebase to merge several commits into one
After you have created a new branch, made changes to the code, and committed several changes in your branch, there may come a time when you would like to merge several of your commits from your local checkout into one commit to send to Gerrit.

The following example shows how to squash two commits into one commit.

Perform an interactive rebase
The  or   option allows you to change (rewrite) your commit history. For each commit, you can modify and change the commit message, add or remove files, or perform other modifications.

First you need to tell git how far back you want to pull. In this case,  means pull the last two commits:

git rebase -i HEAD~2

After you type this command, you will see your commits in reverse order and a list of available commands:

pick aa8cf1d Adding method customFilterFunctionGetRiskyCountryCodeScore to GatewayAdapter. pick 38828e2 Adding $wgDonationInterfaceCustomFiltersFunctionsRiskyCountries to donationinterface.php


 * 1) Rebase 95ccd28..38828e2 onto 95ccd28
 * 2) Commands:
 * p, pick = use commit
 * r, reword = use commit, but edit the commit message
 * e, edit = use commit, but stop for amending
 * s, squash = use commit, but meld into previous commit
 * f, fixup = like "squash", but discard this commit's log message
 * x, exec = run command (the rest of the line) using shell
 * 1) If you remove a line here THAT COMMIT WILL BE LOST.
 * 2) However, if you remove everything, the rebase will be aborted.
 * 1) If you remove a line here THAT COMMIT WILL BE LOST.
 * 2) However, if you remove everything, the rebase will be aborted.

Since we only want to send one commit to review, we will squash the last commit into the first.

Here we change pick to squash:

pick aa8cf1d Adding method customFilterFunctionGetRiskyCountryCodeScore to GatewayAdapter. squash 38828e2 Adding $wgDonationInterfaceCustomFiltersFunctionsRiskyCountries to donationinterface.php


 * 1) Rebase 95ccd28..38828e2 onto 95ccd28
 * 2) Commands:
 * p, pick = use commit
 * r, reword = use commit, but edit the commit message
 * e, edit = use commit, but stop for amending
 * s, squash = use commit, but meld into previous commit
 * f, fixup = like "squash", but discard this commit's log message
 * x, exec = run command (the rest of the line) using shell
 * 1) If you remove a line here THAT COMMIT WILL BE LOST.
 * 2) However, if you remove everything, the rebase will be aborted.
 * 1) If you remove a line here THAT COMMIT WILL BE LOST.
 * 2) However, if you remove everything, the rebase will be aborted.

When you finish picking and squashing, you get to merge your commit messages.

It is important to leave the Change-Id for each commit in the message, otherwise Gerrit will not know how to handle the patch.

Your messages from your previous commits will automatically be placed in this message.

Remember to put your summary message in the commit.

In this case the summary message is:

(mingle-fr-2012-69) Adding a custom filter for risky countries.

Note: ''In regards to which Change-Id you want to use, squashing a commit into an existing commit (one that's already in Gerrit), you need to pick the Change-Id that belongs to the one you meant to submit a new patchset for (the surviving commit). If your commits are new and are not in Gerrit, it does not matter which Change-Id you choose.''

(mingle-fr-2012-69) Adding a custom filter for risky countries.
 * 1) This is a combination of 2 commits.
 * 2) The first commit's message is:

Adding method customFilterFunctionGetRiskyCountryCodeScore to GatewayAdapter.


 * 1) This is the 2nd commit message:

Adding $wgDonationInterfaceCustomFiltersFunctionsRiskyCountries to donationinterface.php

Change-Id: I33e44ec0d93628d9a53c15e08eb89f352b7d5fe0


 * 1) Please enter the commit message for your changes. Lines starting
 * 2) with '#' will be ignored, and an empty message aborts the commit.
 * 3) Not currently on any branch.
 * 4) Changes to be committed:
 * 5)   (use "git reset HEAD ..." to unstage)
 * 6)       modified:   donationinterface.php
 * 7)       modified:   gateway_common/gateway.adapter.php
 * 1)       modified:   gateway_common/gateway.adapter.php

If all goes well, you should see a successful rebase message:

[detached HEAD 02f5e63] (mingle-fr-2012-69) Adding a custom filter for risky countries. 2 files changed, 92 insertions(+), 3 deletions(-) Successfully rebased and updated refs/heads/mingle-fr-2012-69.

Submit your patch for review
git review

You should see a message like this showing your git review went to Gerrit:

remote: Resolving deltas: 100% (4/4) remote: (W) 02f5e63: commit message lines >70 characters; manually wrap lines remote: remote: New Changes: remote:  https://gerrit.wikimedia.org/r/7187 remote: To ssh://jpostlethwaite@gerrit.wikimedia.org:29418/mediawiki/extensions/DonationInterface.git * [new branch]     HEAD -> refs/for/master/mingle-fr-2012-69

See the change at:

https://gerrit.wikimedia.org/r/7187