Search code examples
gitgit-mergerebasegit-rebase

Keep handled conflicts when rebasing a repository and the non-linear history


A variation of a question that has been asked multiple times but I haven't been able to determine a fitting answers that covers my case.
In short I want to rebase a repository without specific commits and preserve already done merge conflicts with minimal effort having a longer history.


Why this question is not a simple duplicate disclaimer:

I know there is Remove commit from history but it is not specific enough for my case, they do not address the case of preserving already done merges and conflicts handled at all.

git rebase -i is not sufficient: merges and how they are handled should be preserved keeping a non-linear history.

While my questions is tightly related to single files, I want to clean commits and git-filter-repo and git rm --cached paired with just rebase or reset are too simple and not what is needed, the git history is not trivial.
That single files are involved should just make the process easier but does not make it trivial.


More details on the problem:

We want to remove several commits from our git history as if they have never been made (we do not just want to clean the files but the commits). Prerequisites:

  • The helpful part: the removed commits are standalone files that are never touched afterwards.
  • We want to preserve the git history, keeping the merges and how they were handled for a non-linear history.

The be best thing we found is git rebase -i master --rebase-merges and do a label-onto and drop/remove the commits we do not want. However this forces us to handle all merge conflicts that were solved in the past again. How can this be avoided, i.e. how can we make a copy if the repository without the commits we want to remove?


Solution

  • Keep a list of the files that you want to remove..... so that you can do something like git rm --cached the-list-of-doomed-files at any point. You can do a git rebase interactive --rebase-merges, and remove the commits of the files you want to get rid of.... then, when the rebase starts, and assuming all you want to do is just git rid of those files without additional changes, then you can safely run this every time you find a conflict:

    git checkout REBASE_HEAD -- .
    git rm --cached the-list-of-dommed-files
    GIT_EDITOR=/usr/bin/true git rebase --continue
    

    That should work, at least on paper.