Overview
Sometimes we push things that we shouldn’t. Let’s figure out how we can erase unnecessary commits.
How to do [Risky]?
- Reset your branch to the commit you wish to be the last in history. This will delete all commits after the specified commit locally.
git reset --hard <commit-hash>
2. Force-push the changes to the remote repository. This will update the remote branch to match your local branch, effectively deleting the unwanted commits from the remote repository.
git push --force origin <branch-name>
Well done! But please keep in mind:
• Backup: Before performing a force push, back up any important data. You can create a new branch to save your current state.
• Coordination: If you are working with other people, inform them about this action. They may need to sync their repositories and resolve any potential issues caused by the force push.
• Force Push with Caution: Force pushing can overwrite changes on the remote repository and disrupt other collaborators. Use it carefully.
How to do [Safe]?
If you prefer not to force push, you can create new commits that revert the changes made by the unwanted commits.
- Revert the Commits:
git revert <commit-hash>
This will create new commits that undo the changes made by the specified commit.
2. Push the Reverts:
git push origin <branch-name>
This method does not remove commits from history but effectively undoes the changes introduced by them.