42 Doing Some Git Housekeeping

One of Git’s key advantages is its speed. Keeping it fast requires some tuning, however. The git gc command provides the tuning tool.

git gc performs several housecleaning tasks. First, it removes any “loose objects” in the repository over a certain age. All of those commits you remove via git rebase, git reset, and so on, are still tracked by Git; they’re just orphaned.

Second, it recalculates the deltas. Deltas are the differences between two pieces of content in the repository. Many times the first pass at creating the delta is not the most efficient. Recalculating the deltas with the help of hindsight allows Git to combine like deltas to reduce the size of the repository and decrease the time Git spends looking for information in the repository.

Periodic running of git gc helps keep the repository in top shape. Providing it with the --aggressive parameter tells Git to focus on making the repository as efficient as possible, instead of trying to run the command as quickly as possible.

Git is configured by default to remove loose objects that are older than the gc.pruneExpire values. The default value is two weeks, but you can change this using git config. You can also change it by providing a value to git gc via the --prune=<some value> parameter. This takes the standard time values.

Many of the commands that cause the repository history to become unruly automatically call git gc for you. The most notable is git svn rebase. Older versions of Git required that you run git gc manually to keep the repository size in check. It’s still a good idea to run git gc from time to time, though, to make sure your repository is in top shape.

What To Do...
  • Run garbage collection.
     
    prompt>​ git gc
     
    Counting objects: 3048, done.
     
    Delta compression using up to 2 threads.
     
    Compressing objects: 100% (2564/2564), done.
     
    Writing objects: 100% (3048/3048), done.
     
    Total 3048 (delta 2041), reused 667 (delta 476)
     
    Removing duplicate objects: 100% (256/256), done.
  • Run garbage collection in the most size-optimized way.
     
    prompt>​ git gc --aggressive
     
    Counting objects: 3048, done.
     
    Delta compression using up to 2 threads.
     
    Compressing objects: 100% (2564/2564), done.
     
    Writing objects: 100% (3048/3048), done.
     
    Total 3048 (delta 2041), reused 667 (delta 476)
     
    Removing duplicate objects: 100% (256/256), done.
  • Remove “loose objects” that are older than a week.
     
    prompt>​ git gc --prune="1 week"
     
    Counting objects: 244, done.
     
    Delta compression using up to 2 threads.
     
    Compressing objects: 100% (228/228), done.
     
    Writing objects: 100% (244/244), done.
     
    Total 244 (delta 109), reused 0 (delta 0)

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.164.24