5

Suppose I have a mainline branch and a feature branch. I have merged the mainline branch into the feature branch many times, but I have only had a few, very minor merge conflicts. I want to clean up the history so that there is only a single merge at the end. What is the best method for doing this?

Casebash
  • 114,675
  • 90
  • 247
  • 350
  • 1
    When you say clean up the history, you need to be more specific. If you want to remove the merges from mainline into feature, you have to decide what you want the feature branch to look like with those merges removed. For example, if some work in the feature branch depends on changes made in mainline (and subsequently merged into the feature branch), removing the merges will cause your feature branch to no longer compile because it's depending on code that was removed. – Lily Ballard Dec 22 '11 at 01:58
  • @KevinBallard: That is a valid point. In some cases, you may want to then shift the branch up. I'm not too concerned about that though for this question, I just want to ensure that the branch only merges back in once – Casebash Dec 22 '11 at 05:38

3 Answers3

1

Have you looked into git rebase?

git co -b temp_feature feature
git rebase master

This should ignore the merges, but you will have to re-resolve the conflicts. It also creates temp_feature branch for easier go back but same could be achieved with reflog.

(Wrong answer below: this will create single commit, not a single merge :-/)

I think the simplest is to do following:

git co master
git merge --squash feature

This will create single commit from whole feature branch. If you do not want to keep the feature branch do:

git branch -D feature
Martian
  • 282
  • 1
  • 4
1

You can do an interactive rebase to achieve this, but you should only do so if you have not yet shared that history with anybody else. What this means in practice is that you should only do this if you have not done a git push to a shared repo.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Mark
  • 467
  • 3
  • 14
1

Sounds like you want to cherrypick the non-merge commits for the feature branch into clean-feature branch, then finally merge clean-feature branch with the latest master (whatever that is). You can use rebase to speedup part of that cherrypicking, which unfortunately I don't know of a good way to do in a jiffy, since this is not something I do very often. It would help if you have a gui that allows you to select multiple commits to be cherrypicked at once.

Edit: This would be a more concrete solution: How to cherry pick a range of commits and merge into another branch

Community
  • 1
  • 1
prusswan
  • 6,853
  • 4
  • 40
  • 61