Skip to content

Instantly share code, notes, and snippets.

@ispedals
Created June 23, 2013 21:17
Show Gist options
  • Save ispedals/5846577 to your computer and use it in GitHub Desktop.
Save ispedals/5846577 to your computer and use it in GitHub Desktop.
To remove files from git history after they have been already committed
1. Note that the files will be deleted from the file system as well as from the history, so make a backup of the repository before preceeding and any files that you want then leave untracked in the repository
2. Get a list of tracked files in a tree and save to file (do for each branch)
git ls-tree --full-tree --name-only -r HEAD >> files.txt
3. Review the list of files and remove the files that you want to keep. This is also a good time to edit your .gitignore file so that the files remain untracked if they are added again.
4. Get a list of deleted files in a repository and add the unwanted files to the list of files
git log --diff-filter=D --summary
5. Now delete the unwanted files in the list of files (replace FILES.TXT with a path to the list of files)
* On Windows this needs to be run with git-bash
git filter-branch --force --index-filter \
'cat FILES.TXT | git rm --cached --ignore-unmatch' \
--prune-empty --tag-name-filter cat -- --all
6. rm -rf .git/refs/original/
7. git reflog expire --expire=now --all
8. git gc --prune=now
9. git gc --aggressive --prune=now
10. Add back the files you want to keep untracked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment