Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change parent of commit without doing git rebase

Tags:

git

I need to shrink my long living GIT repository by removing old commits history.

Transform sth like this:

A - B - D - E - ... 
 \ /         
  C 
    

Into:

       D' - E - ...

the commits history after commit E is quite complex, contains a lot of branches, tags, merges etc.

I don't care about other users because this is a migrated repo that has not yet been pushed to any remote repository. It is a bare repository on my local machine. I can transform it to a "normal" one if needed.

To create D' and print its hash I could use:

git commit-tree D^{tree}

But the question is how switch the parent of commit E from commit D to commit D'?

At this stage, most of the tips I've already found suggest doing a rebase master branch onto D', but it's not so easy when you have a complex history with commits for over a decade with a lot of multiple branches, tags, merges etc.

Is there any low-level GIT command to simply switch the pointer to the parent commit without doing a git rebase when I'm 100% sure that the new parent commit contains all content of the current one?

like image 381
Gilmor Avatar asked Feb 06 '26 05:02

Gilmor


1 Answers

You can use git filter-repo (project page) :

excerpt from the doc :
Parent rewriting

To replace $commit_A with $commit_B (e.g. make all commits which had $commit_A as a parent instead have $commit_B for that parent), and rewrite history to make it permanent:

git replace $commit_A $commit_B
git filter-repo --force
like image 90
LeGEC Avatar answered Feb 09 '26 08:02

LeGEC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!