-1

I have made some changes in main branch, and pushed them into main, I was supposed to create a new branch and push the changes there.

Since I already pushed the changes into main, I reverted the changes in the commit.

How do I permanently remove the commit from the branch?

Mahijendra
  • 335
  • 5
  • 15

2 Answers2

1

You actually did the right thing by reverting the commits after publishing them to the repository. Because you already pushed the commits, there generally exists the possibility that someone else has already pulled your unwanted commit (and maybe the revert commit too).

You could use git reset --hard HEAD~2, but don't do that. The reason for not doing this is that deleting commits effectively rewrites history. This means that anyone sharing this branch could have problems next time they go to pull. By undoing the commit via a git revert, you guarantee that no one else will have problems.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Okay. Why would it be a problem when they're making pull? could you elaborate this a bit? Also would they only delete local commits? or would they permanently delete from git? – Mahijendra Mar 24 '23 at 06:25
  • When someone else goes to pull, their local Git will think that new commits are appearing in the remote version, on top of the common recent commit which is three commits ago. Then, that person might get merge conflicts. If, on the other hand, you `git revert`, that other person would have no issues when pulling (assuming it made no additional commits). – Tim Biegeleisen Mar 24 '23 at 06:28
  • Got it! So it's better that I don't delete the the revert commit and the push commit permanently? – Mahijendra Mar 24 '23 at 06:32
  • In general, this is best practice. You should only consider hard reset if you're certain that no one else has pulled. If you haven't pushed yet, then hard reset is fine. – Tim Biegeleisen Mar 24 '23 at 06:37
  • I did hard reset in the main branch, when I did that, it is asking me to pull the changes again which consists 2 commits I just deleted now. Is that how it is supposed to work? Or I did it wrong? – Mahijendra Mar 24 '23 at 06:41
  • This site is not for debugging help. If you are trying a different approach, you should ask a new question. – Tim Biegeleisen Mar 24 '23 at 06:42
-1

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

Swapnil Pasarkar
  • 491
  • 5
  • 14
  • I have 2 commits, the latest one is the revert commit one and the one before is obviously the changes I pushed. I want to remove them both permenantly – Mahijendra Mar 24 '23 at 06:09