0

I want to remove several commits and merges from Git. For example, I have 10 commits before and 10 commits after the specific commit I want to remove. I want to delete 4 commits within this range. How can I achieve this?

Here is a picture for furder explination.

https://i.stack.imgur.com/XTRNY.jpg

This issue occurred when I was trying to amend the previous commit, which I had mistyped. Git prompted me to perform a merge, and this is what happened.

PoUrYa
  • 1
  • Have you tried rebasing? `git rebase -i ` where `` is a commit you want to keep before the first one you want to remove, and then delete the ones you want to drop. By default, rebase will drop the merge commits too. – joanis Aug 12 '23 at 21:25
  • Does this answer your question? [Remove specific commit](https://stackoverflow.com/questions/2938301/remove-specific-commit) – joanis Aug 12 '23 at 21:26

1 Answers1

0

One way to do this is git rebase interactive.

git log --oneline to see all list of commit

enter image description here

then Run below command

git rebase -i HEAD~x

Lets say I want to remove commit 3 and Commit 4 here., Mark commit 3 and commit 4 as drop.

enter image description here

specify x as the number of last commits which you wish to value as the commit which expect you wish to see and want to edit. If you specify 20, you will list of last 20 commits.

From the list mark which one you want to drop using

Once completed Esc --> :wq --> enter. You may come across conflicts, resolve conflicts and once everything looks ok

git rebase --continue

enter image description here

Hope this helps.. Happy coding.

Hardik
  • 1,716
  • 6
  • 27
  • 41