How to shrink the .git folder

后端 未结 7 2320
花落未央
花落未央 2020-12-02 04:16

My current base has a total size of approx. 200MB.

But my .git folder has an amazing size of 5GB (!). Since I push my work to an external server, i don\'t need any b

相关标签:
7条回答
  • 2020-12-02 04:38

    you should not delete all changes older than 30 days (i think it's somehow possible exploiting git, but really not recommended).

    you can call git gc --aggressive --prune, which will perform garbage collection in your repository and prune old objects. do you have a lot of binary files (archives, images, executables) which change often? those usually lead to huge .git folders (remember, git stores snapshots for each revision and binary files compress badly)

    0 讨论(0)
  • 2020-12-02 04:38

    Tried above methods, nothing worked in my case (where I accidently killed the git process during git push) so I finally had to delete the repo and clone it again and now the .git folder is of normal size.

    0 讨论(0)
  • 2020-12-02 04:40

    Shrink a Git Repository by removing some files log history from the .git Folder based on their last updated time.

    I had faced the same issue on my Local Machine. The reason was I have deleted some massive files from my local and committed to Central Repository. But event after git status, git fetch and git pull. My .git folder size is about 3GB. later I ran the following command to reduce the size of the .git folder by considering the files which have changed/expired a month ago.

    Command

    $ git remote prune origin && git repack && git prune-packed && git reflog expire --expire=1.month.ago && git gc --aggressive
    

    Git Commands and their short description:

    • git-prune - Prune all unreachable objects from the object database
    • git-repack - Pack unpacked objects in a repository
    • git-prune-packed - Remove extra objects that are already in pack files.
    • git reflog: Git keeps track of updates to the tip of branches using a mechanism called reference logs, or "reflogs." Reflogs track when Git refs were updated in the local repository. In addition to branch tip reflogs, a special reflog is maintained for the Git stash. Reflogs are stored in directories under the local repository's .git directory. git reflog directories can be found at .git/logs/refs/heads/., .git/logs/HEAD, and also .git/logs/refs/stash if the git stash has been used on the repo. git reflog at a high level on the Rewriting History Page.
      git reflog expire --expire=now --expire-unreachable=now --all
      In addition to preserving history in the reflog, Git has internal expiration dates on when it will prune detached commits. Again, these are all implementation details that git gc handles and git prune should not be used standalone.
    • git gc --aggressive: git-gc - Cleanup unnecessary files and optimize the local repository.
      Behind the scenes git gc actually executes a bundle of other internal subcommands like git prune, git repack, git pack and git rerere. The high-level responsibility of these commands is to identify any Git objects that are outside the threshold levels set from the git gc configuration. Once identified, these objects are then compressed, or pruned accordingly.

    Commonad with Outcome:

    $ git remote prune origin && git repack && git prune-packed && git reflog expire --expire=1.month.ago && git gc --aggressive
    Enumerating objects: 535, done.
    Counting objects: 100% (340/340), done.
    Delta compression using up to 2 threads
    Compressing objects: 100% (263/263), done.
    Writing objects: 100% (340/340), done.
    Total 340 (delta 104), reused 0 (delta 0)
    Enumerating objects: 904, done.
    Counting objects: 100% (904/904), done.
    Delta compression using up to 2 threads
    Compressing objects: 100% (771/771), done.
    Writing objects: 100% (904/904), done.
    Total 904 (delta 343), reused 561 (delta 0)
    
    0 讨论(0)
  • 2020-12-02 04:46

    I tried these but my repository was still very large. The problem was I had accidentally checked in some generated large files. After some searching I found a great tutorial which makes it easy to delete the large generated files. This tutorial allowed me to shrink my repository from 60 MB to < 1 MB.

    Steve Lorek, How to Shrink a Git Repository

    0 讨论(0)
  • 2020-12-02 04:58

    5GB vs 200MB is kind of weird. Try to run git gc.

    But no, unless you split your repository into modules, you can't decrease the size of the .git directory.

    Each clone of a git repo is a full fledged repository that can act as a server. That's the base principle of distributed version control.

    0 讨论(0)
  • 2020-12-02 04:59

    Here is what the creator of git Linus has to say about how to shrink your git repo:

    The equivalent of "git gc --aggressive" - but done *properly* - is to do (overnight) something like

       git repack -a -d --depth=250 --window=250
    

    where that depth thing is just about how deep the delta chains can be (make them longer for old history - it's worth the space overhead), and the window thing is about how big an object window we want each delta candidate to scan.

    And here, you might well want to add the "-f" flag (which is the "drop all old deltas", since you now are actually trying to make sure that this one actually finds good candidates.

    source: http://gcc.gnu.org/ml/gcc/2007-12/msg00165.html

    Will this get rid of binary data that is orphaned in my repo? "git repack" will not git rid of images or binary data that you have checked into your repo and then deleted it. To delete those kind of data permanently from your repo you have to re-write your history. A common example of that is when you accidentally check in your passwords in git. You can go back and delete some files but then you have to re-write your history from then to now and then force push then new repo to your origin.

    0 讨论(0)
提交回复
热议问题