8

Consider the following problem:

  • Private Project containing some credentials in the early stages
  • We want to go open source
  • We need to get rid of the credentials in the history
  • credentials are not in single files but in code
  • Complicated history with several merges, pull requests etc.

What I want to do:

Squash all commits from root up to an arbitrary commit with clean state to one big 'Initial commit'.

When I do:

git rebase -i --root

And squash the first commits together:

pick Initial commit \
fixup dirty1        |
fixup dirty2        | Squash these to one, to remove credentials.
fixup dirty3        |
fixup clean1        /
pick clean2
pick clean3
...
...

I have to rebase everything and resolve all merge conflicts again after that.

How can I just squash the first N commits without having to resolve the entire history including the merge conflicts after the N + 1 commit.

MaxNoe
  • 14,470
  • 3
  • 41
  • 46
  • Unfortunately, there is no way around rewriting the entire history. This because when clean2 gets its new parent (the squashed commit), its hash changes. This means that clean3 has a new parent, which changes its hash, and so on and so on. – David Deutsch Jan 28 '16 at 15:45
  • This would be ok. I just would like to not have to resolve every merge conflict manually again. – MaxNoe Jan 28 '16 at 15:46
  • I've upvoted your question but I reckon you'll have to deal with conflict resolutions for each subsequent commit in the history. While I've never used it, I suspect this would be a use case for https://www.kernel.org/pub/software/scm/git/docs/git-rerere.html – Anthony Geoghegan Jan 28 '16 at 16:09
  • Mmh. I read this. And I do not think it's applicable. Working on the rebased branch, the commits will never be already resolved for `rerere`, right? – MaxNoe Jan 28 '16 at 16:16
  • Can you just use merge strategy `theirs` to take the resolution from the original patch? – Useless Jan 28 '16 at 18:28
  • 2
    I found another solution: http://www.davidverhasselt.com/git-how-to-remove-your-password-from-a-repository/ . Im replacing the credentials by placeholders. – MaxNoe Jan 28 '16 at 18:42
  • @MaxNoe yes, that is a good solution. You should provide that as a self-answer to your question – knittl Mar 20 '21 at 11:27

2 Answers2

1

You can use git-rerere to reuse the same conflict resolutions.

The only downside is you'll have to resolve them one more time after enabling rerere before it can automatically reuse the resolutions.

T Percival
  • 8,526
  • 3
  • 43
  • 43
0

You can use git rebase -Xtheirs -i --root. Keep the first commit as pick (as you've done) and change the first word on the second through Nth line to squash.

-Xtheirs will manage the merge issues for you and I guess that's what you're looking for here.