martin's
martin's

Reputation: 3955

How to combine two commits in Git?

I have this

* [cf0149e] (HEAD, branch_2) more editing
* [8fcc106] some edit
|
|  * [59e643e] (branch_2b) branch 2b
| /
|/
|  * [0f4c880] (branch_2_a) branch 2a
| /
|/
*  [a74eb2a] checkout 1
*  [9a8dd6a] added branch_2 line
|
|
| * [bb903de] (branch_3) branch 3
|/
|
| * [674e08c] (branch_1) commit 1
| * [7d3db01] added branch_1 line
|/
* [328454f] (0.0.0) test

I would like to combine these two:

*  [a74eb2a] checkout 1
*  [9a8dd6a] added branch_2 line

Into one

*  [a74eb2a] This is old [a74eb2a] + [9a8dd6a]

I've tried more permutations that I care to mention: reset, rebase, squash, edit. Can't seem to pull it off. Whenever I have succeeded in merging them the merge only exists for one branch. For example, "branch_2" will end-up with a the desired combination located at HEAD~2. However, "branch_2b" and "branch_2_a" still point to the two commits I want to consolidate. In other words, I end-up with a new commit that is the consolidation of the two original commits. What I want is to have all branches coming out of those two commits to now refer to the new consolidated commit.

Upvotes: 1

Views: 203

Answers (1)

Asherah
Asherah

Reputation: 19347

After squashing these two commits, you'll end up with a brand new commit and history since the squash, called something else entirely:

* [cf0149e] (HEAD, branch_2) more editing
* [8fcc106] some edit
|
|  * [59e643e] (branch_2b) branch 2b
| /
|/
|  * [0f4c880] (branch_2_a) branch 2a
| /
|/
*  [a74eb2a] checkout 1
*  [9a8dd6a] added branch_2 line
|
| * [cf0149e'] (HEAD', branch_2') more editing (post-squash rebase)
| * [8fcc106'] some edit (post-squash rebase)
| * [SQUASHED] "checkout 1" + "added branch_2 line"
|/
|
| * [bb903de] (branch_3) branch 3
|/
|
| * [674e08c] (branch_1) commit 1
| * [7d3db01] added branch_1 line
|/
* [328454f] (0.0.0) test

Notice that the other extant branches are unaffected; that's because all you've done is made a new squashed commit—it doesn't change the history of anything else.

Your HEAD is now pointing to cf0149e', which will have a new SHA1 hash, because you've altered its history.

Now, you can rebase the other branches, one by one, on SQUASHED, since that's the point where they branched off, to produce:

* [cf0149e] (HEAD, branch_2) more editing
* [8fcc106] some edit
|
|  * [59e643e] (branch_2b) branch 2b
| /
|/
|  * [0f4c880] (branch_2_a) branch 2a
| /
|/
*  [a74eb2a] checkout 1
*  [9a8dd6a] added branch_2 line
|
| * [cf0149e'] (HEAD', branch_2') more editing (post-squash rebase)
| * [8fcc106'] some edit (post-squash rebase)
| |
| |  * [59e643e] (branch_2b') branch 2b (rebased on SQUASHED)
| | /
| |/
| |  * [0f4c880'] (branch_2_a') branch 2a (rebased on SQUASHED)
| | /
| |/
| * [SQUASHED] "checkout 1" + "added branch_2 line"
|/
|
| * [bb903de] (branch_3) branch 3
|/
|
| * [674e08c] (branch_1) commit 1
| * [7d3db01] added branch_1 line
|/
* [328454f] (0.0.0) test

The old history is now irrelevant, so really we have:

* [cf0149e'] (HEAD', branch_2') more editing (post-squash rebase)
* [8fcc106'] some edit (post-squash rebase)
|
|  * [59e643e] (branch_2b') branch 2b (rebased on SQUASHED)
| /
|/
|  * [0f4c880'] (branch_2_a') branch 2a (rebased on SQUASHED)
| /
|/
* [SQUASHED] "checkout 1" + "added branch_2 line"
|
| * [bb903de] (branch_3) branch 3
|/
|
| * [674e08c] (branch_1) commit 1
| * [7d3db01] added branch_1 line
|/
* [328454f] (0.0.0) test

Upvotes: 2

Related Questions