In this article, we will discuss the various pruning methods used in git. The following methods are available in git
- The git prune command
- The git prune option
We will discuss each of these in the following paragraphs.
The git prune command
Let’s dive into the definition of the git prune command. It can remove all unreachable objects from the git object database. As per the git documentation users will not need to call this command directly, users can use the git gc command which will call the git prune command.
Let us understand this command with an example. Suppose we have a repository and to test this command, we need to make the commit unreachable.
First, we will create a folder named gitprune-demo.
- Add a file readme.txt with content “Nolowiz tutorials”
- Initialize git repository
- Add file to repository and commit changes.
mkdir gitprune-demo cd gitprune-demo echo "Nolowiz tutorials" > readme.txt git init git add readme.txt git commit -m "First commit,added readme"
The next step is to update the readme file and add a second commit.
echo "Git prune command" >> readme.txt git commit -am "Updated readme"
Git log will output
After that, make one of the gits commit unreachable we can use the git reset command to which will reset the repository to the first commit.
If we check the git log after running the git reset command we can see only the first commit. Now the repository has one commit in a detached state.
Git is very strict in keeping history we can check if the second commit is still available using the git checkout command. From the below output we can see the second commit is still available.
After that, if we run the git log we can see two commits. Next, we will checkout master.
Before pruning we can run the “git prune –dry-run –verbose” command which will display what items are to be pruned but not actually prune it.
Thus from the above output, the prune command produced empty output which means that the prune will not actually delete anything because the second commit is most likely not fully detached. Somewhere Git is still maintaining a reference to it.
Git internally stores the HEAD and branch references in a log called reflog (To see these logs we can run the git reflog command). It retains logs for a default period of 90 days. The below command is dangerous never run this on an actual repository; for testing purposes, we will be using it.
The above command will force expire all entries to the reflog that are older than now. Finally, we can execute the prune command
The prune git option
Branches are an important part of git. If the number of branches increases it will become difficult to manage it, but git provides a prune option to delete outdated branches.
Suppose we are working on a repository, pushed changes in a feature branch, and merged with the master; GitHub/GitLab provides options to delete feature branch after the merge, but our local repository still holds the deleted branch. To solve this problem we can use the following commands.
git fetch –prune
This command is recommended for deleting outdated branches, it fetches the latest remote data which may contain recovered remote branch refs.
git remote prune origin
This command deletes local branches with reference to the remote branches.
Finally, to delete outdated branches automatically on every fetch request we can set git config as
git config --global fetch.prune true
In conclusion, a normal user does not need to use the git prune command and it is a part of git garbage collection. To delete outdated branches we can use the git prune option.