| Scenario | What To Do |
|---|---|
You ran git add . and node_modules got staged |
• git reset node_modules/ • Add node_modules/ to .gitignore • git add .gitignore • git commit -m "Fix: removed node_modules" • git push origin main/master |
You ran git commit and node_modules got committed |
• git rm -r --cached node_modules • Add node_modules/ to .gitignore • git commit -m "Remove node_modules" • git push origin main/master |
You ran git push and node_modules is on GitHub |
• git rm -r --cached node_modules • Add node_modules/ to .gitignore • git commit -m "Remove node_modules" • git push origin main --force |
| Scenario | What To Do |
|---|---|
| 1. Undo last commit (but keep changes) | git reset --soft HEAD~1 |
| 2. Undo last commit (delete the changes) | git reset --hard HEAD~1 |
| 3. Unstage a file accidentally added | git reset fileName |
| 4. Remove a file/folder from commit before push | git rm --cached fileOrFolder git commit -m "Remove file" |
| 5. Remove file/folder from Git after pushing | git rm --cached fileOrFolder git commit -m "Remove file" git push |
| 6. Remove file/folder from entire Git history | git filter-repo or BFG tool (advanced) |
| 7. Delete wrong commit from GitHub | git reset --hard HEAD~1 git push --force |
| 8. Restore a deleted file from history | git checkout HEAD~1 fileName |
| 9. Revert a specific commit without touching others | git revert <commit-hash> |
| 10. Delete node_modules after push | git rm -r --cached node_modules .gitignore git commit git push --force |
| 11. Delete large files that increased repo size | Use git filter-repo to remove them everywhere |