Squash old commits

48 views Asked by At

I am trying to do a release with Nx. During the release process, it get an error about an unknown revision in my commit history.

enter image description here

This revision dates back to the initial setup of my repository.

enter image description here

I'm trying to use git rebase to either delete the second commit, or just squash all the commits into one

enter image description here

But whether i squash the commits, drop the second one, or try to adjust the commit message i get stuck in a never ending process of resolving merge conflicts going from root to HEAD.

Is there a simple way too just take revision 9facd74f and squash all the commits between it and a07a5ad3 so that it shows as only one "Initial commit" and I can resolve this issue of unknown revision?

1

There are 1 answers

0
eftshift0 On

The error about the missing commit is probably due to a shallow clone. Is that the case?

Anyway, to your question: create a new orphan branch on top of the commit that you want to use as the root of the new branch and commit it. Say the new branch is called X and the *original" commit from the original branch for it is called original-commit.

Then, take the script from here: https://github.com/eantoranz/git-duplicate

Run it like this, assuming you are on X [1]:

./git-duplicate.py original-commit my-branch

It will print a commit id when it finishes running.... That commit has the history the way you are requesting it.... Place the branch you want on top of it if you like the result.

[1] On 2024/03/09 I adjusted the way the new base is provided (now there is the --onto argument to specify it. It is HEAD by default). Old versions of the script had to be run like this:

./git-duplicate.py X original-commit my-branch