git prune
- Git course
- Expert
- Lesson 18/32
- 4 min
Learn unreachable object cleanup with git prune.
What is git prune?
git prune is used to remove objects that cannot be reached through any reference in a Git repository. These objects may no longer be pointed to by a branch, tag, or another reference.
git prune --dry-runYou can use the --dry-run option to see which objects could be removed before cleanup. Prune should be used carefully because the operation may not be reversible.
Check before cleanup
A prune operation can permanently delete objects. Therefore, it is safer to first check which objects would be removed using the --dry-run option.
git prune --dry-run
git pruneThe first command only displays objects that could be removed. The second command actually cleans up eligible objects. Git's protection and reachability rules mean that not every old object is necessarily deleted immediately.
Practice Run a prune preview
Preview the removal of unreachable objects in the repository. Do not perform the actual deletion. Print a message indicating that the preview is complete.
Starter code
# Silme önizlemesini çalıştır
# Önizleme mesajını yazdırSample solution
git prune --dry-run
echo 'Prune önizlemesi tamamlandı'Expected output
Prune önizlemesi tamamlandı
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which command is used to remove Git objects that are not referenced by a branch or another reference?
- git log --all
- git gc --auto
- git prune Correct answer
- git fsck --full
Explanation: <code>git prune</code> is used to remove unreachable objects from the repository.
2 Preview before deletion
git prune ____
git prune --dry-run
Explanation: <code>--dry-run</code> lets you preview which objects the prune operation would remove.
3 Unreachable objects
| Term | Meaning |
|---|---|
git prune |
Removes unreachable objects |
Unreachable |
Cannot be reached from a reference |
--dry-run |
Previews objects to be removed |
Git nesnesi |
Stores commit or file data |