Git Prune: The prune command in Git is an integrated utility tool that is primarily used for cleaning up the unreachable or “orphaned” objects present in Git. The concept of unreachable objects lies in the fact that they cannot be accessed by any means of reference. Just like the commits that cannot be accessed from any branch or tags are considered unreachable.

Git Prune

Git prune also behaves the same. It is evident from the facts that the git prune command is not executed directly. It behaves like a garbage collector that collects uncontrolled or unused data that are no longer accessible or no longer put in use.

To demonstrate the effects of the git prune command, you need to create a scenario where some of your commits become unreachable. To make your commits unreachable you will need to create a demo repository and then commit and it. This can be done by creating a directory and then storing your files in it. Let’s do it through the command:

These command sequences will create a new repository in a directory named as prune-demo. Since you have committed with the message “Hello JavaTpoint” it is now added with the commit message as given above. The next step is to create modifications in the file and then commit it for the second time.

Your second commit will now display something like this:

Now we have two commits history in our repository. We can verify it by the command and observe the changes.

The log command rolls out the two commits and the messages associated with them, and the edits made to the file. The next step is to make one of these commits unreachable and to do that. First, we need to use the Git reset command, which will reset the repository to the first commit associated with the file.

Now, if you check the logs you will find that there is only one commit associated with the prune-demo files that you made in your repository.  Your repository now has a commit that has been detached. The second commit you made is no longer available even if you check logs frequently. It is not said that it is deleted since Git is quite strict. Git also keeps updates of deleted items, and that can be checked using the command:

Not it is the perfect time to use the prune command. Pruning the removed commit will put it into the output log with other files where it is stored in the log history like the commit history is stored. Before pruning, you need to ensure to pass some options like –dry-run and –verbose which will display what items are to be pruned but not actually prune it. It can be using the below command:

You have just successfully pruned the second commit and the output displayed will be mostly empty since there are no items left to the pruned in the commit directory. You can now re-establish the process and continue to whatever you were working on.

Usage

We have come across some of the usages of pruning. Let’s now conclude it with some existing pruning usage commands:

The above command is associated with pruning but actually it does not prune. It just displays the out of what it will do.

The above command is used to display the objects and actions taken into consideration while pruning.

The above command is used to display the output to check the progress happening while pruning.

The above command is used to force expire the past objects.

This command helps in specifying the options that are taken reference from the head of the commit.

Pin It on Pinterest

Share This