O'Reilly logo

Git Essentials - Second Edition by Ferdinando Santacroce

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reassembling commits

One of the widest uses of the git rebase command is for reordering or combining commits. For this first approach, imagine you have to combine two different commits.

Suppose we erroneously added half a grape in the shoppingList.txt file, then the other half, but at the end we want to have only one commit for the entire grape; follow me with these steps.

Add a gr to the shopping list file:

[1] ~/grocery (master)
$ echo -n "gr" >> shoppingList.txt

The -n option is for not adding a new line.

Cat the file to be sure:

[2] ~/grocery (master)
$ cat shoppingList.txt
banana
apple
orange
peach
gr

Now perform the first commit:

[3] ~/grocery (master)
$ git commit -am "Add half a grape"
[master edac12c] Add half a grape
 1 file changed, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required