174

My Git history looks like that :

Git history

I would like to squash the purple commits into a single one. I don't want to see them ever again in my commit log.

I've tried to do a git rebase -i 1, but even though 1 is on the blue branch (cf. picture), I still see every commit on my purple branch.

How can I completely remove the purple branch (from commit log) ?

3
  • Is your repo pushed anywhere that other people have pulled from it?
    – Schleis
    Commented Jul 10, 2013 at 17:49
  • Both branches are purely local. Commented Jul 10, 2013 at 18:00
  • 1
    TIL doing git rebase -i <first commit sha> will get rid of all merge commits :) This may be probably true only for a repository with only merge commits where no conflicts were resolved
    – vivekmore
    Commented Sep 10, 2022 at 13:57

5 Answers 5

154

Do git rebase -i <sha before the branches diverged> this will allow you to remove the merge commit and the log will be one single line as you wanted. You can also delete any commits that you do not want any more. The reason that your rebase wasn't working was that you weren't going back far enough.

WARNING: You are rewriting history doing this. Doing this with changes that have been pushed to a remote repo will cause issues. I recommend only doing this with commits that are local.

10
  • 7
    Even if I choose a sha before the branches have diverged, I see all the commits of the purple branch -_- Commented Jul 10, 2013 at 18:15
  • 1
    Yeah, then you delete them in the editor that comes up with git rebase and they will be removed.
    – Schleis
    Commented Jul 10, 2013 at 18:17
  • 6
    Deleting the commits resulted in losing all the modification of the merge. But I did a soft reset and I've been able to have something quite like I wanted (order do not match initial expectations). Commented Jul 10, 2013 at 18:35
  • 3
    this answer is incomplete in detail, which branch do yu initiate the rebase from?
    – grgry
    Commented Feb 17, 2016 at 9:18
  • 4
    Fairly sure you do not want to 'remove' the commits. 'remove' is not an option in rebase, but delete is. if you delete a commit, you will loose the changes it introduced. You want to squash the commits.
    – thecoshman
    Commented Oct 2, 2017 at 9:55
148

Starting with the repo in the original state

Original repo history

To remove the merge commit and squash the branch into a single commit in the mainline

Squashed commits, no merge commit

Use these commands (replacing 5 and 1 with the SHAs of the corresponding commits):

git checkout 5
git reset --soft 1
git commit --amend -m '1 2 3 4 5'
git rebase HEAD master

To retain a merge commit but squash the branch commits into one:

Squashed commits, retained merge commit

Use these commands (replacing 5, 1 and C with the SHAs of the corresponding commits):

git checkout -b tempbranch 5
git reset --soft 1
git commit --amend -m '1 2 3 4 5'
git checkout C
git merge --no-ff tempbranch
git rebase HEAD master

To remove the merge commit and replace it with individual commits from the branch

Branch moved into mainline, no merge commit

Just do (replacing 5 with the SHA of the corresponding commit):

git rebase 5 master

And finally, to remove the branch entirely

Branch removed entirely

Use this command (replacing C and D with the SHAs of the corresponding commits):

git rebase --onto C D~ master
4
  • 1
    in git rebase 5 master case, why it is not "A B C 1 2 3 4 5 D ..." order?
    – Roy
    Commented Jun 4, 2018 at 15:28
  • 3
    That command takes the commits that master doesn't have in common with 5 and plops them on top of 5. The commit at C isn't part of the lineage of 5, it's the first to be moved on top of 5.
    – Allen Luce
    Commented Jun 4, 2018 at 19:55
  • 3
    If you want to end up with "A B C 1 2 3 4 5 D ..." you can do: git rebase C 5; git rebase 5 master
    – Allen Luce
    Commented Jun 4, 2018 at 19:56
  • 1
    Did exactly like this and worked even a thousand commits back!
    – Michael-O
    Commented Dec 20, 2022 at 14:57
54

To Just Remove a Merge Commit

If all you want to do is to remove a merge commit (2) so that it is like it never happened, the command is simply as follows

git rebase --onto <sha of 1> <sha of 2> <blue branch>

And now the purple branch isn't in the commit log of blue at all and you have two separate branches again. You can then squash the purple independently and do whatever other manipulations you want without the merge commit in the way.

3
  • 3
    Of all the answers, I find this both the most straightforward and the easiest to do. Thank you.
    – Roshambo
    Commented Jul 22, 2020 at 22:34
  • This is the first step to the question, and I agree the easiest that does exactly what was requested. The second step would be git merge --squash <purple branch name>
    – Tony
    Commented Dec 9, 2020 at 16:19
  • best answer, definitely. Thanks. ps: add --rebase-merges if you want to preserve some other merge commits along your blue branch (otherwise rebase will flatten those by default)
    – chris
    Commented Jan 31, 2023 at 9:49
27

There are two ways to tackle this based on what you want:

Solution 1: Remove purple commits, preserving history (incase you want to roll back)

git revert -m 1 <SHA of merge>

-m 1 specifies which parent line to choose

Purple commits will still be there in history but since you have reverted, you will not see code from those commits.


Solution 2: Completely remove purple commits (disruptive change if repo is shared)

git rebase -i <SHA before branching out>

and delete (remove lines) corresponding to purple commits.

This would be less tricky if commits were not made after merge. Additional commits increase the chance of conflicts during revert/rebase.

4
  • This does not sound right. Leaving aside the fact that it's hard to tell what the original poster wanted to do with the purple commits in the first place, if you revert the merge, that's fine, that will do a reverse patch of the changes from that commit and add it as another commit, canceling the purple commits out. It's like 1 - 1 = 0. But if you then rebase out the purple commits, you leave behind the reverted patch, unless you rebase that out too. If you don't, it's like applying -1 to your history, not 0, so you'll leave behind changes you don't want.
    – user456814
    Commented Jul 14, 2013 at 7:04
  • @Cupcake, provided were two separate answers. I have revised answer to be less confusing :P
    – prem
    Commented Jul 21, 2013 at 19:41
  • AFAIK this is not what the OP is asking for. He wants to keep the changes, but remove the noise from his log. For that, he needs to squash the commits down, deleting will remove the changes.
    – thecoshman
    Commented Oct 2, 2017 at 9:57
  • True, looks like the original question has been rephrased. This answer is not for OPs problem.
    – prem
    Commented Oct 29, 2019 at 23:48
11

For full control over the operation and to preserve any merge commits you want to keep along the way, the modern approach is to use

git rebase -i -r

From man git rebase of git version 2.44.2 about -r:

-r, --rebase-merges[=(rebase-cousins|no-rebase-cousins)], --no-rebase-merges
By default, a rebase will simply drop merge commits from the todo list, and put the rebased commits into a single, linear branch. With --rebase-merges, the rebase will instead try
to preserve the branching structure within the commits that are to be rebased, by recreating the merge commits. Any resolved merge conflicts or manual amendments in these merge
commits will have to be resolved/re-applied manually.  --no-rebase-merges can be used to countermand both the rebase.rebaseMerges config option and a previous --rebase-merges.

When rebasing merges, there are two modes: rebase-cousins and no-rebase-cousins. If the mode is not specified, it defaults to no-rebase-cousins. In no-rebase-cousins mode, commits
which do not have <upstream> as direct ancestor will keep their original branch point, i.e. commits that would be excluded by git-log(1)'s --ancestry-path option will keep their
original ancestry by default. In rebase-cousins mode, such commits are instead rebased onto <upstream> (or <onto>, if specified).

It is currently only possible to recreate the merge commits using the ort merge strategy; different merge strategies can be used only via explicit exec git merge -s <strategy>
[...]  commands.

See also REBASING MERGES and INCOMPATIBLE OPTIONS below.
3
  • good to know .. but this is the option I'd never use ... because personally I prefer minimum to no merge commits in my git history :)
    – vivekmore
    Commented Sep 10, 2022 at 13:55
  • I started to do this, but then I had no idea what I was doing, could you elaborate on this answer? Commented Jul 17, 2023 at 15:59
  • The command is still a git rebase -i at it's heart, so make sure you're comfortable with that first. Suggest you pick a simple merge commit to try this out first. Read what's in the todo textfile, try to trace and follow what git is going to do. Once you have a good idea, you can start experimenting by introducing new commits in the merge, move commits to before the merge, etc. If you need more specific help you'll need to provide more details :)
    – squall3d
    Commented Jul 19, 2023 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.