Combine the first two commits of a Git repository?

Use git rebase -i --root as of Git version 1.7.12.

In the interactive rebase file, change the second line of commit B to squash and leave the other lines at pick:

pick f4202da A
squash bea708e B
pick a8c6abc C

This will combine the two commits A and B to one commit AB.

Found in this answer.


You tried:

git rebase -i A

It is possible to start like that if you continue with edit rather than squash:

edit e97a17b B
pick asd314f C

then run

git reset --soft HEAD^
git commit --amend
git rebase --continue

Done.


A was the initial commit, but now you want B to be the initial commit. git commits are whole trees, not diffs even if they are normally described and viewed in terms of the diff that they introduce.

This recipe works even if there are multiple commits between A and B, and B and C.

# Go back to the last commit that we want
# to form the initial commit (detach HEAD)
git checkout <sha1_for_B>

# reset the branch pointer to the initial commit,
# but leaving the index and working tree intact.
git reset --soft <sha1_for_A>

# amend the initial tree using the tree from 'B'
git commit --amend

# temporarily tag this new initial commit
# (or you could remember the new commit sha1 manually)
git tag tmp

# go back to the original branch (assume master for this example)
git checkout master

# Replay all the commits after B onto the new initial commit
git rebase --onto tmp <sha1_for_B>

# remove the temporary tag
git tag -d tmp