Git Prune

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:

 $ cd prune-demo
$ git init
$ echo “Hello JavaTpoint”
$ git commit -m “committed hello” 

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.

 $ echo “Hello JavaTpoint, Let’s learn Git”
$ git commit -m “committed second hello” 

Your second commit will now display something like this:

 $ echo “Hello JavaTpoint”
$ git commit -m “committed second hello”
git-prune-demo $ git commit -m "added another line to hello.txt file"
[master 5178bec] added another line to hello.txt file
1 file changed, 1 insertion(+) 

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

 $ git log
commit 5178becc2ca965e1728554ce1cb8de2f2c2370b1
Author: KundanKumarPathak <[email protected]>
Date:   Wed March 24 14:49:59 2021 -0700
        added another line to hello.txt
commit 994b122045cf4bf0b97139231b4dd52ea2643c7e
Author: KundanKumarPathak <[email protected]>
Date:   Wed Sep 24 09:43:41 2021 -0700 

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.

 $ git reset --hard 994b122045cf4bf0b97139231b4dd52ea2643c7e
HEAD is now at 994b122 added hello.txt 

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:

 git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
      5178bec added another line to hello.txt
If you want to keep it by creating a new branch, this may be a good time
to do so with:
     git branch <new-branch-name> 5178bec
Switched to branch 'master' 

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:

$ git prune --dry-run --verbose

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:

$ git prune --dry-run

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

$ git prune --dry-run --verbose

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

$ git prune --progress

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

$ git prune --expire<time>

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

$ git prune <head>

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