Repository Maintenance & Housekeeping
Learn commands like `git gc` and `git prune` to clean up and optimize your local Git repository.
Repository Maintenance & Housekeeping is a free DevOps Bootcamp lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome to Git Housekeeping
Over time, your local Git repositories can accumulate unnecessary files and become less efficient. Think of it like a messy room!
In this lesson, we'll learn how to clean up and optimize your Git repositories using powerful commands like git gc and git prune. This keeps your projects running smoothly.
Git's Object Store Explained
First, let's understand how Git stores data. Git saves your project's history as a series of 'objects':
- Blobs: File contents
- Trees: Directories and their contents
- Commits: Snapshots of your project at a point in time
- Tags: Pointers to specific commits
These objects can be stored as 'loose objects' or compressed into 'packfiles' for efficiency.
Introducing `git gc` (Garbage Collect)
The git gc command stands for 'garbage collect'. It's Git's built-in tool for cleaning up and optimizing your repository.
Its main tasks include:
- Packing loose objects: Compressing individual objects into single packfiles.
- Pruning unreachable objects: Deleting objects that are no longer referenced by any commit, branch, or tag (after a certain age).
- Removing old packfiles: Getting rid of outdated compressed object files.
This process makes your repository smaller and faster.
Running `git gc` (General Use)
You can run git gc manually at any time. Git also runs it automatically in the background when you perform certain operations, like pushing or fetching.
It's generally safe to run, but it's good practice to ensure you don't have uncommitted work you might need to recover before a major cleanup.
Try running it in your terminal:
git gcUnderstanding `git gc` Output
When you run git gc, you'll see output similar to this:
Counting objects: 1234, done.
Compressing objects: 100% (987/987), done.
Writing objects: 100% (1234/1234), done.
Total 1234 (delta 567), reused 0 (delta 0)
Removing duplicate objects: 100% (256/256), done.
Pruning objects: 100% (123/123), done.This shows Git working to pack objects, write new packfiles, and prune (delete) old, unreachable objects. The numbers will vary based on your repo's state.
Focus on `git prune`
While git gc handles many cleanup tasks, git prune specifically deals with removing 'dangling' or 'unreachable' objects.
- Dangling objects: These are objects that exist in your repository but are no longer referenced by any commit, branch, or tag, and are also not part of a packfile.
- Unreachable objects: Similar to dangling, but a broader term. They can exist for various reasons, like after a rebase or reset that rewrites history.
git prune deletes these objects if they are older than a default expiration time (usually 2 weeks).
Running `git prune` Directly
You usually don't need to run git prune directly, as git gc calls it. However, you can use it for more targeted cleanup.
To see what git prune would do without actually deleting anything, use the --dry-run option:
git prune --dry-runPruning with Expiration
By default, git prune (and git gc's pruning step) only removes objects that are older than two weeks. You can override this using the --expire option.
This is useful if you want to aggressively clean up dangling objects that were created very recently, perhaps after an experimental rebase you're sure you don't need to revert.
For example, to prune objects older than 1 day:
git prune --expire=1.dayDistinguishing from `git reflog expire`
It's important not to confuse git prune with git reflog expire. While related to cleanup, they serve different purposes:
git prune: Deletes actual Git objects (blobs, trees, commits) from the object database.git reflog expire: Cleans up entries in your reflog (a local history of where your HEAD has been), making certain commits harder to find, but it doesn't delete the objects themselves.
Always be careful when modifying history or running cleanup commands!
Housekeeping Check
Which of the following best describes the primary role of git gc?
Recap & Next Steps
Congratulations! You've learned how to keep your Git repositories tidy and efficient.
git gc: The all-in-one command for garbage collection, packing objects, and pruning.git prune: Specifically targets and removes unreachable, dangling objects older than a set expiration.
Regular maintenance helps ensure your Git workflow remains smooth and your repositories don't consume excessive disk space. Keep up the good work!
Frequently asked questions
Is the “Repository Maintenance & Housekeeping” lesson free?
Yes — the full text of “Repository Maintenance & Housekeeping” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Repository Maintenance & Housekeeping”?
Learn commands like `git gc` and `git prune` to clean up and optimize your local Git repository. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Repository Maintenance & Housekeeping” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Git Reflog & Recovering History
- Git Bisect for Debugging
- Repository Maintenance & Housekeeping
- Rewriting History with git filter-repo