Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save tkersey/322505 to your computer and use it in GitHub Desktop.
Save tkersey/322505 to your computer and use it in GitHub Desktop.
Git: cleanup repo
Remove file from git repository (history)
SOLUTION: This is the shortest way to get rid of the files:
1. check .git/packed-refs - my problem was that I had there a refs/remotes/origin/master line for a remote repository, delete it, otherwise git won't remove those files
2. (optional) git verify-pack -v .git/objects/pack/#{pack-name}.idx | sort -k 3 -n | tail -5 - to check for the largest files
3. (optional) git rev-list --objects --all | grep #{SHA_FROM_#_3} - to check what files those are
4. git filter-branch --index-filter 'git rm --cached --ignore-unmatch file_names' - to remove the file from all revisions
5. rm -rf .git/refs/original/ - to remove git's backup
6. git reflog expire --all --expire='0 days' - to expire all the loose objects
7. (optional) git fsck --full --unreachable - to check if there are any loose objects
8. git repack -A -d - repacking the pack
9. git prune - to finally remove those objects
Completely remove a file from all revisions
git filter-branch -f --index-filter 'git update-index --remove filename' <introduction-revision-sha1>..HEAD
git push --force --verbose --dry-run
git push --force
Where introduction-revision-sha1 is the SHA1 that the file was first committed to the repository.
How to remove a git submodule
Let’s say that you have a submodule in your project called ’submodule1′ and it’s in the following path: ‘vendors/submodule1′. In git there are 3 traces of this this submodule:
1) .gitmodules
2) .git/config
3) the submodule entry in the index/commit itself.
To remove the first two, is really simple, you just edit those files and remove the lines that specify the submdoule. In order to delete the third and last trace of the submodule in git, you need to type the following command:
git rm --cached path/to/submodule
Note: Do not put a trailing slash at the end of path. If you put a trailing slash at the end of the command, it will fail.
In the example above, we would type (do not include trailing slash after submodule1):
git rm --cached vendors/submodule1
Removing the history from a new branch
git symbolic-ref HEAD refs/heads/newbranch
rm .git/index
git clean -fdx
<do work>
git add your files
git commit -m 'Initial commit'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment