Skip to content

Instantly share code, notes, and snippets.

@refactorsaurusrex
Last active April 13, 2024 16:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save refactorsaurusrex/49041407c673079ac69873c6e49c3ae1 to your computer and use it in GitHub Desktop.
Save refactorsaurusrex/49041407c673079ac69873c6e49c3ae1 to your computer and use it in GitHub Desktop.
How to retroactively apply gitignore to a repo
  1. Add or update a .gitignore file
  2. Rebase your repo such that the .gitignore from step one is positioned in history prior to the naughty file(s) being commited.
  3. Run this command, where <commit> is the hash for the commit respresented by step 1: git rebase -ix 'git rm -r --cache . && git add . && git commit --amend --no-edit' <commit>
  4. Resolve conflicts as necessary.

Note that if there are only a few file types or directories or individual files, filter-branch is a simpler route to take. These steps are only necessary for large numbers of files / file types. Also note that there may be better ways still of doing this. :-)

@Nashluffy
Copy link

This is great, thanks!

@HuskyNator
Copy link

I wish i were enough of a terminal user to know how to use this.

@nikita-fuchs
Copy link

good god, this saved the day I think.

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