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
To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.
What Happens If I Delete a Git Branch? When you delete a branch in Git, you don't delete the commits themselves. That's right: The commits are still there, and you might be able to recover them.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With