How to remove recent commits from both local Git repository and remote GitHub?

I need help removing the last few commits from my Git repository. I want to delete them both locally and from my remote GitHub repo.

I tried using git reset --hard HEAD~3 to remove the last 3 commits from my local branch, and then used git push --force origin main to update the remote repository. However, when I make new changes and commit them, the old commits I thought I deleted keep showing up again.

I also attempted to use git rebase -i HEAD~3 and deleted the commit lines I didn’t want, but the same issue happens. The commits disappear temporarily but come back after I push new changes.

What’s the correct way to permanently remove these commits from both my local repository and GitHub? Am I missing a step in the process?

sounds like ur pulling from upstream or have a backup branch somewhere thats restoring those commits. check git log --oneline --all to see if theyre hiding in other branches. also make sure ur not accidentally merging from a fork or collaborator who still has the old commits.

The issue you’re experiencing suggests you might have multiple branches or the commits exist elsewhere in your repository history. When you reset and force push, but the commits reappear later, it usually means they’re still referenced somewhere. First, check if you have other local branches that still contain these commits using git branch -a. If the commits exist on other branches, they’ll remain in your repository even after removing them from main. Another possibility is that you’re pulling from a different remote or branch that still has these commits. Verify your remote configuration with git remote -v and ensure you’re working with the correct branch. I’ve encountered this exact problem before when I had a feature branch that contained the commits I was trying to remove. Even though I cleaned main, git pull would bring them back through the merge history. You might need to clean up all references to these commits across all branches, or use git reflog expire and git gc to completely purge them from your local repository after ensuring no other branches reference them.