Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a commit's parent, thereby making it the initial commit

Note: I know this is rewriting history, will change all the hashes, and mess everyone else up.


I want to take a commit, and remove its parent. Specifically, the commit should now look like an initial commit. That means its diff will change; it will look as though all the files were created by that commit, at that point in history.

How does one do this? (I could, of course, go edit the commit object, but then the commits wouldn't point to each other.) Also, I would like to do this on a new branch (meaning that there are now two histories: the original one, and another where the commit is the initial commit.)

like image 827
PyRulez Avatar asked Oct 31 '25 16:10

PyRulez


1 Answers

git rebase -i HEAD~

Then in the editor that pops up after the above terminal cmd Change the start of child commit line from pick to squash. Or 'p' and 's' respectively. Write the file and exit the editor that appeared upon the above command

Edit: Assuming the two commits are the most recent. If not then you can check out the child's child commit hash and use git rebase -i HEAD~2 instead

Edit2: or you could just git rebase -i parentcommithash and the top of the list should be the parent

like image 104
bgarcia Avatar answered Nov 02 '25 08:11

bgarcia



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!