Skip to content

Instantly share code, notes, and snippets.

@red-stripe
Created February 13, 2018 04:59
Show Gist options
  • Save red-stripe/c24c139b7cf3a64f4052f8bfd245415c to your computer and use it in GitHub Desktop.
Save red-stripe/c24c139b7cf3a64f4052f8bfd245415c to your computer and use it in GitHub Desktop.
Remove large files from solo projects

Removing large files from git

All commands are run from the root directory of your git project folder. Before you start make a backup of your entire git project elsewhere. My username is chobern, yours will be different. Unless you are future me....

Check the archive size, do you really have a problem? chobern$ du -sh .git

1.61G    .git

Yes we do.

List files in your archive to find which file is causing the problem. You could also use the GUI find which file is causing problems. chobern$ du -sh * */*

1.6G	img
4.0K	index.html
4.0K	readme.md
20K	    img/Kiwi.jpg
1.6G	img/big_kiwi.jpg

In my case img/big_kiwi.jpg is the problem.

Find out when you added the large file to the archive chobern$ git log --diff-filter=A -- img/big_kiwi.jpg

commit 8ce86177b57a9e044419570d60f30cde30c5fb98
Author: Craig H 
Date:   Tue Feb 13 16:47:18 2018 +1300

    Update: giant kiwi bird

Use git log to find the commit id of the commit immediately before the large file was added. chobern$ git log

commit 6b90082319b5d7978e2cf137fae2a7f6edd0f8a2
Author: Craig H 
Date:   Tue Feb 13 16:51:26 2018 +1300

    Update: Fruit title

commit 8aa41f054337e4fa2a6287c1a93dbd8fab4544c7
Author: Craig H 
Date:   Tue Feb 13 16:48:04 2018 +1300

    Add: .gitignore

commit 8ce86177b57a9e044419570d60f30cde30c5fb98
Author: Craig H 
Date:   Tue Feb 13 16:47:18 2018 +1300

    Update: giant kiwi bird

commit fa2e36bb0e391b5f07be8f695ae71027c6589053
Author: Craig H 
Date:   Tue Feb 13 16:45:21 2018 +1300

    Update: giant kiwi fruit

commit 8e54605a632e3870b8749f10e0d87f27a4e961bd
Author: Craig H 
Date:   Tue Feb 13 16:31:54 2018 +1300

    Add: My first website

commit f043461cc3d9692013c8f8c7eae0aa888f16c2f7
Author: Craig H 
Date:   Tue Feb 13 13:57:50 2018 +1300

    Add: Readme

In my example commit fa2e36bb0e391b5f07be8f695ae71027c6589053 happen right before I added the large bird accdently.

Use the rebase command to rewrite history from before the large file. You only need to use the first 7 characters of the commit id. git rebase -i fa2e36b

pick 8ce8617 Update: giant kiwi bird
pick 8aa41f0 Add: .gitignore
pick 6b90082 Update: Fruit title

# Rebase fa2e36b..6b90082 onto fa2e36b (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's lo$
# x, exec = run command (the rest of the line) using she$
#
# These lines can be re-ordered; they are executed from $
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be $
#
# Note that empty commits are commented out

Comment out the commit you didnt want to make with a '#' symbol #pick 8ce8617 Update: giant kiwi bird Then apply the rebase.

Successfully rebased and updated refs/heads/master.

Now force expire all old content

chobern$ rm -rf .git/refs/original/
chobern$ git reflog expire --expire=now --all
chobern$ git gc --aggressive --prune=now

Check that the archive size is back to a reasonable level chobern$ du -sh .git

308K	.git

Force push to the archive to replace the current version of history online git push --force

Tell your instructors, friends and family they will need to re-clone your repo beore using it again.

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