I added some functionality in my project which took 4 git commits, now business is asking that the functionality is no more needed(After more than a month). So I need to remove those particular git commit(s) from my repo which now has 27 more commits after that.
-
If you feel comfortable you can try `git rebase -i head~n` where n=exact number of commits you need to go back to those redundant commits and just `drop` them. – R11G Aug 22 '19 at 23:15
2 Answers
There are four ways of doing so:
Clean way, reverting but keep in log the revert:
git revert --strategy resolve <commit>
Harsh way, remove altogether only the last commit:
git reset --soft "HEAD^"
Rebase (show the log of the last 5 commits and delete the lines you don't want, or reorder, or squash multiple commits in one, or do anything else you want, this is a very versatile tool):
git rebase -i HEAD~5
And if a mistake is done:
git rebase --abort
Quick rebase: remove only a specific commit using its id:
git rebase --onto commit-id^ commit-id
Alternatives: you could also try:
git cherry-pick commit-id
Yet another alternative:
git revert --no-commit
As a last resort, if you need full freedom of history editing (eg, because git don't allow you to edit what you want to), you can use this very fast open source application: reposurgeon.
Note: of course, all these changes are done locally, you should git push
afterwards to apply the changes to the remote. And in case your repo doesn't want to remove the commit ("no fast-forward allowed", which happens when you want to remove a commit you already pushed), you can use git push -f
to force push the changes.
Note2: if working on a branch and you need to force push, you should absolutely avoid git push --force
because this may overwrite other branches (if you have done changes in them, even if your current checkout is on another branch). Prefer to always specify the remote branch when you force push: git push --force origin your_branch
.

- 5,782
- 5
- 28
- 71
-
1Most of these methods won't work because the OP has commits buried inside the branch. And rewriting the history of a branch should not be suggested for a shared public branch – Tim Biegeleisen Nov 14 '16 at 23:25
-
1This response is quite literally a copy and paste of an answer to another similar question by another user: https://stackoverflow.com/a/11992215 – Michael H. Jun 19 '20 at 16:55
Because you are dealing with a published branch, which is presumably being used by someone other than you, I would recommend that you revert the two commits using git revert
. From the branch in question type:
git log
and find the SHA-1 hashes of two commits in question. Then revert them using:
git revert abcd1234..foobar12
where abcd1234
is the hash of the first (oldest) of the two commits, and foobar12
is the more recent of the two commits.
Using git revert
will add commits which effectively undo the two commits buried in your branch. The alternative to this approach would be to use rebase or filter branch. But both these methods involve rewriting the history of your branch. This could cause a headache for anyone else using your branch since they would no longer be able to pull or push.

- 502,043
- 27
- 286
- 360