How to shrink the .git folder

后端 未结 7 2347
花落未央
花落未央 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: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)
    

提交回复
热议问题