Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all commits after a specific commit

Tags:

git

I have the following commit history (everything is pushed) where I want to discard all commits in the marked spot e.g all commits from (and including) e603105 and forward.

enter image description here

like image 820
CutePoison Avatar asked Aug 15 '26 21:08

CutePoison


2 Answers

You can hard reset back

git reset --hard ff61726

This is considered re-writing history so then it would require a force push

git push -f
like image 147
Cory Kramer Avatar answered Aug 18 '26 17:08

Cory Kramer


You cannot directly delete commits from git, but you can stop a branch pointing at them, and they will eventually be deleted by a "garbage collection" routine.

To move a branch pointer, you can use git reset.

Specifically, if "some-branch" currently points at ac033d7, and you run this:

git checkout some-branch
git reset --hard ff61726

Now "some-branch" points at commit ff61726, but commit ac033d7 still exists, and will only be garbage collected once nothing points to it any more.

One of the things that will probably still point to it is any remote copy of the "some-branch" branch, e.g. on Github/BitBucket/GitLab. To move that branch, you need to perform a "force push":

git push --force-with-lease
like image 22
IMSoP Avatar answered Aug 18 '26 15:08

IMSoP



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!