Combine the first two commits of a Git repository? Combine the first two commits of a Git repository? git git

Combine the first two commits of a Git repository?


Use git rebase -i --rootas 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 Asquash bea708e Bpick 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 Bpick asd314f C

then run

git reset --soft HEAD^git commit --amendgit 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 commitgit rebase --onto tmp <sha1_for_B># remove the temporary taggit tag -d tmp