Skip to content

Instantly share code, notes, and snippets.

@gengwg
Created April 23, 2025 18:14
Show Gist options
  • Save gengwg/32610c0a31f6f7eb09061cf19436220f to your computer and use it in GitHub Desktop.
Save gengwg/32610c0a31f6f7eb09061cf19436220f to your computer and use it in GitHub Desktop.

Here's how to undo your last commit (that accidentally included __pycache__) and prevent it from being tracked in the future:


1. Undo the Last Commit (But Keep Changes)

git reset --soft HEAD~1
  • This will:
    • Remove the commit (fd8b15c)
    • Keep all your changes staged
    • Allow you to re-commit without __pycache__

2. Unstage __pycache__ Files

git restore --staged */__pycache__/*

or for all Python cache files:

git restore --staged *.pyc __pycache__/

3. Re-commit Properly

git commit -m "python crash course"  # Now without __pycache__

4. Add __pycache__ to `.gitignore (Prevent Future Tracking)**

echo "__pycache__/" >> .gitignore
echo "*.pyc" >> .gitignore

Then commit the .gitignore:

git add .gitignore
git commit -m "Add .gitignore for Python cache"

Alternative: Completely Remove the Commit (Hard Reset)

If you want to erase the commit and all changes:

git reset --hard HEAD~1

(Warning: This permanently deletes uncommitted work!)


What Happened in Your Attempt?

  • git restore --staged . unstaged all files, but didn't undo the commit.
  • The files remained in Git's history until you ran reset.

Key Takeaways

  1. --soft reset → Best for fixing bad commits.
  2. .gitignore → Essential for Python projects.
  3. Always check git status before committing!

Let me know if you'd like help with anything else! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment