Skip to content

Latest commit

 

History

History
35 lines (26 loc) · 1.28 KB

revert_accidental_commit.md

File metadata and controls

35 lines (26 loc) · 1.28 KB

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:

1. Revert the Commit Locally

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.

2. Remove the Unwanted Files

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

3. Commit and Push the Correct Changes

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.