Skip to content
Related Articles

Related Articles

Git – git-prune

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 14 Feb, 2022

Git prune is a command that deletes all the files that are not reachable from the current branch. The prune command is useful when you have a lot of files in your working directory that you don’t want to keep. The primary use of git prune is to clean up your working directory after you have finished working on a project. What actually git prune does is, suppose you have an object or commit that is no longer reachable from the current branch. Then git prune will delete that object or commit. The basic idea is that git prune is a way to clean up your working directory to make it lighter and easier to work on a project.

Command:

git fetch –prune <remote>

The git fetch –prune <remote> command is a way to delete all the objects that are not reachable from the remote repository. And if you want to only prune the remote repository and not to fetch it, you can use:

git remote prune origin

This will result in the remote repository being pruned. If you want to configure git to prune the remote repository when you fetch it, you can use:

git config –global fetch.prune true

Let’s understand the git prune command in a little more detail.

Consider this scenario, suppose you have a project that you are working on. You have a bunch of files in your working directory, and a commit becomes unreachable from the current branch. Then you want to delete all the objects or commit that are not reachable from the current branch.

1. creating a new repository and initializing it

mkdir git-prune-demo-geeks

cd git-prune-demo-geeks

git init .

echo “Hello World” > hello.txt

git add hello.txt

git commit -m “first commit”

The above command sequence will:

  • create a new repository called git-prune-demo-geeks
  • change the current directory to git-prune-demo-geeks
  • initialize the repository
  • create a file hello.txt with the content “Hello World”
  • add the file hello.txt to the staging area
  • commit the file hello.txt with the message “first commit”

Output:

Output

creating repository

2. Modify the file hello.txt and create a new commit

echo “Hello from the other side” > hello.txt

git add hello.txt

git commit -m “second commit”

Output:

Output

Adding commit

This will add the line “Hello from the other side” to the file hello.txt and commit it with the message “second commit”. To verify the above command sequence, we can use the following command:

git log

Output:

Output

git log output

Here, the git log will display the commit history of the repository.

3. Making commit unreachable from the current branch

git reset –hard HEAD~1

Output:

Output

git reset

Here, HEAD~1 is the commit that is unreachable from the current branch. And now if we try to see the git log output again, we will see that the commit is no longer reachable from the current branch, it will show only the commit that is reachable from the current branch.

git log

Output:

Output

git log

This repository now has a commit that is a detached commit. So if you try to check out the commit, it will not check out the commit but will create a detached branch. The second commit is a detached commit and no longer showing in the git log output.

git checkout HEAD~1

Output:

Output

The HEAD~1 is the commit that is detached from the current branch.

4. Running git prune

After checkout the detached commit, we have to return back to the master via the git checkout command and then run the git prune command. We have to pass certain options to the git prune command so that the output of the command displays what is set to be pruned.

git prune –dry-run –verbose

Output:

Output

git prune

Here, empty output means that nothing is pruned. Because somewhere in the repository, git is keeping the commit reference that is detached from the current branch. So git prune will not delete the commit. And to conclude our scenario, we must have to clear reflog first.

git reflog expire –expire=now –expire-unreachable=now –all

This will forcefully expire the reflog. After removing the reflog we can run the git prune command again.

git prune –dry-run –verbose –expire=now

Output:

Output

Pruned the unreachable commit

This will result in displaying git SHA object references of commit and tree objects that are no longer reachable from the current branch.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!