How to Clean Up Git Repositories With the Git Prune Command
In this article, we will discuss the git prune
command and the purpose it serves. We are aware that Git is extremely careful with our data.
When we delete data like commits, Git does not lose them easily. This leads to a build-up of stale data in our machines.
That is where the git prune
command comes into action.
We can talk of the git prune
command as a housekeeping utility in Git that we use to clean up orphaned or unreachable Git objects. When we talk of unreachable objects, they are the objects in our repository that the present refs cannot access.
A good example is when we roll back to a previous commit with the git reset <Commit ID>
command. Git will store the deleted commit as a dangling object.
We use the git prune
command to eliminate such data.
Use the git prune
Command
The git prune
command has several useful options like below.
$ git prune --dry-run
We run the command above to get the output of the command. It does not execute the prune
.
$ git prune --verbose
The command above will show us all actions and the associated objects.
$ git prune --progress
We use the command above to check the progress of the git prune
.
$ git prune --expire <time>
We use the command above to delete the objects that are older than the specified time (<time>
).
To understand the concept better, let us look at a practical example. Let us run the git log
command to check the commit history in our local repository.
$ git log --oneline
Let us use the git reset
command to roll back by one commit, such that our HEAD
is at the Sixth Update
.
$ git reset --hard 27bd68b
HEAD is now at 27bd68b Sixth Update
Let us try to find the deleted commit.
$ git fsck --lost-found
The deleted commit is the first one; we can confirm with the first seven characters.
Before running the git prune
command, we must run a reflog
that will expire entries older than now
.
$ git reflog expire --expire=now --expire-unreachable=now --all
It is advisable to dry run
the command to see what changes will occur.
$ git prune --dry-run
We can now run the prune
command.
$ git prune --verbose --progress --expire=now
Let us check if the dangling commits still exist in our repository.
$ git fsck --lost-found
Difference Between git prune
, git fetch --prune
, and git remote prune
The git fetch --prune
and git remote prune
commands have similar functions. We use them to delete the refs to branches that are deleted in the remote repository.
It comes in handy when working as a team, and you want to rid yourself of remote branches that are deleted after merging to the main branch.
The git fetch --prune
command is a combination of:
$ git fetch --all && git remote prune
It will fetch from our remote repo before starting the prune
. The basic git prune
command will delete local objects, as discussed earlier.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn