Git/Rebase

Interactive Rebase / Squash several commits
There may come a time when you would like to squash your commits from your local checkout to send one commit to Gerrit.

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

Make sure master is up to date and then create a new checkout.

Create a new branch
git checkout -b mingle-fr-2012-69

Make changes to code and commit one of your changes:

git commit gateway_common/gateway.adapter.php

[mingle-fr-2012-69 dfcf4ec] Adding method customFilterFunctionGetRiskyCountryCodeScore to GatewayAdapter. 1 files changed, 63 insertions(+), 2 deletions(-)

Make some more changes and commit your work.

git commit donationinterface.php

[mingle-fr-2012-69 5c6df58] Adding $wgDonationInterfaceCustomFiltersFunctionsRiskyCountries to donationinterface.php 1 files changed, 29 insertions(+), 1 deletions(-)

Perform an interactive rebase
You need to tell git how far back you want to pull.

In this case, HEAD~2 means pull the last two commits.

git rebase -i HEAD~2

After you type this command, you will see your commits in reverse order.

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