If some files got accidentally synced to GitHub, you can undo this in a few simple steps. Here’s how you can revert those changes:
You’ll first need to go back to the state before the unwanted files were committed. Run these commands in the terminal inside VS Code or your Git Bash:
# Undo the last commit but keep changes unstaged
git reset HEAD~1
This command moves the commit history back by one commit without deleting the changes from your local workspace.
You can now selectively stage files or modify .gitignore
if needed. If certain files shouldn’t be committed, either remove them or add them to .gitignore
.
-
To remove specific files from staging:
git restore --staged path/to/file
-
If you want to remove them entirely from your working directory:
rm path/to/file
After making the necessary changes, you can commit the correct files and push again:
git add .
git commit -m "Fix accidental sync"
git push --force origin <branch-name>
Note: The --force
option will overwrite the last commit on GitHub with your corrected version.