Remove git commit between two commits

I added some functions to my project, which took 4 git commits, now the business is asking that the functionality is no longer needed (after more than a month). So I need to remove those specific git commit (s) from my repo that now have 27 more commits after that.

+6
source share
2 answers

Since you are dealing with a published branch that is supposedly being used by someone other than you, I would recommend that you return two commits with git revert . From the branch in question:

 git log 

and find the SHA-1 hashes of the two commits in question. Then return them using:

 git revert abcd1234..foobar12 

where abcd1234 is the hash of the first (oldest) of the two commits, and foobar12 is the later of the two commits.

With git revert , commits will be added that effectively destroy the two commits buried in your branch. An alternative to this approach would be to use the rebase or filter branch. But both of these methods include rewriting the history of your industry. This can cause a headache for anyone else using your branch, as they can no longer pull or push.

+1
source

There are four ways to do this:

  • A clean path when returning, but keep a refund in the log:

     git revert --strategy resolve <commit> 
  • The hard way is to delete just the last message:

     git reset --soft "HEAD^" 
  • Rebase (show the log of the last 5 commits and delete the lines you don’t want, or reorder, or squander several commits in one or do something else that you want, this is a very universal tool)

     git rebase -i HEAD~5 

And if an error is made:

 git rebase --abort 
  • Fast reboot: remove only the specific commit using your id:

     git rebase --onto commit-id^ commit-id 
  • Alternatives: you can also try:

     git cherry-pick commit-id 
  • Another alternative:

     git revert --no-commit 
  • As a last resort, if you need complete freedom to edit history (for example, because git does not allow you to edit what you want), you can use this very quickly open source: reposurgeon .

Note. Of course, all these changes are done locally, after that you have to git push apply the changes to the remote control. And in case your repo does not want to delete the commit (“without fast jump forward”, which happens when you want to delete the commit that you already pressed), you can use git push -f to force the changes.

Note2: if you are working with a branch and you need to force push, you should avoid git push --force , because it can overwrite other branches (if you made changes to them, even if your current check is on another branch). Prefer to always specify the remote branch when you force push : git push --force origin your_branch .

+2
source

Source: https://habr.com/ru/post/1012276/


All Articles