To undo the last commit on GitHub, use the following steps:
- Undo Locally:
- Run
git reset --soft HEAD~1
to undo the commit while keeping changes staged, or - Use
git reset --mixed HEAD~1
to unstage changes but keep them in your working directory.
- Run
- Push Changes to GitHub:
- For a shared repository, use
git push --force
to overwrite the commit history.
- For a shared repository, use
⚠️ Caution: Avoid --force
in shared repositories without team agreement to prevent overwriting others’ work.
For a safer alternative, use git revert <commit_hash>
to create a new commit that undoes the previous one.