How do I remove commits that are branched off master
?
I have added a large library, along with a very rich commit history (and that predates my code) to a subdirectory via Git subtree. I'd like to retroactively squash that entire history, but still be able to merge in new commits to the library.
I have tried various combinations of git rebase
but I never get the expected result [*].
My repository looks something like:
A---B-----------F---G master
/
... C---D---E
and I'd like for it to look something like:
A---B-----------F'--G' master
/
E'
or:
A---B-------E'--F'--G' master
[*]:
git rebase --onto C E master
git checkout F; git rebase --onto C E master
This is history editing. You will end up in something like
A---B-----------F'---G' master
/
E'
Merging will be a problem after this, because of Git will not be able to find common parents between your history and libraries's history.
To actually do it you need to
--no-commit
.The history will look like
A---B-----------F'---G'
To make the shallow clone of the library, you need to do something like this (Warning: untested):
git format-patch F --stdout > ~/saved_commits.patch
)git remote rm
git reflog expire --expire=now --all
git gc --prune=now
. Now you should see the repository shrank.git fetch --depth=10 libraryremote
git am ~/saved_commits.patch
).To migrate into submodules solution (the best option probably), you need to rollback to the state before merge and set up submodules, then substitute each merge with changed commit-id for submodule. Unlike for the case of splitting out project directory to submodule I don't know the automated solution for this (but it can be implemented the similar way).