4

In my local GIT repository, I have the following commits:

A --> B --> C --> D

(Where D is the head)

I would like to combine/squash B and C into a single commit. How could I do that?

Thanks.

1 Answers1

9

Use this command:

git rebase -i A ;# where A is A's commit hash

In the interactive editor, change the the lines, which should look something like this:

pick c7787af A
pick f9d9262 B
pick 6c363d0 C
pick 40f657d D

To this:

pick c7787af A
pick f9d9262 B
squash 6c363d0 C
pick 40f657d D

If you'd prefer to abandon C's commit message, but still squash the commit, use this:

pick c7787af A
pick f9d9262 B
fixup 6c363d0 C
pick 40f657d D

Never rebase a branch you've already shared with collaborators. The action changes SHA1 hashes, and results in non-fast-forward updates.

As Jason noted in comments, an alternative syntax is git rebase -i HEAD~4. When you're rebasing recent history to squash commits (i.e. when it's easy to count parents back in time), this avoids the trouble of looking up A's hash.

Christopher
  • 42,720
  • 11
  • 81
  • 99
  • 2
    In addition to @Christopher's answer, you can also use `git rebase -i HEAD~4`, in which 4 is the number of topmost commits you want to change. This saves you from manually entering the hash... – Penghe Geng Aug 22 '12 at 02:20