[Git] remote.origin.prune vs fetch.prune

Posted on in programming

cover image for article

What is the difference between git config remote.origin.prune true and git config fetch.prune true?

When you fetch from a remote repository, Git creates a remote-tracking branch for each branch in the remote repository. These remote-tracking branches are used to keep track of the state of the remote branches, so that you can easily see what changes have been made to them since you last fetched.

However, sometimes remote branches are deleted. For example, if a developer deletes a branch locally, it will also be deleted from the remote repository. If you don't prune your remote-tracking branches, they will remain in your local repository, even though they no longer exist in the remote repository. This can lead to confusion, as you may think that a branch still exists when it actually doesn't.

The git config remote.origin.prune true command tells Git to prune remote-tracking branches for the specified remote repository, origin. This means that if a branch is deleted from the origin remote repository, Git will delete the corresponding remote-tracking branch from your local repository.

The git config fetch.prune true command tells Git to prune remote-tracking branches for all remote repositories. This means that if a branch is deleted from any remote repository, Git will delete the corresponding remote-tracking branch from your local repository.

So, which command should you use? If you only work with one remote repository, then you can use git config remote.origin.prune true. However, if you work with multiple remote repositories, then you should use git config fetch.prune true. This will ensure that your remote-tracking branches are always up-to-date.

Why should you prune remote-tracking branches?

There are a few reasons why you should prune remote-tracking branches:

  • To keep your local repository clean. Remote-tracking branches can quickly accumulate, especially if you work with multiple remote repositories. Pruning them can help to keep your local repository clean and organized.
  • To avoid confusion. As mentioned above, if you don't prune remote-tracking branches, they can remain in your local repository even after the corresponding branches have been deleted from the remote repository. This can lead to confusion, as you may think that a branch still exists when it actually doesn't.
  • To improve performance. Pruning remote-tracking branches can improve the performance of your Git operations. This is because Git doesn't have to check for the existence of remote branches that have been deleted.

How to prune remote-tracking branches

There are a few ways to prune remote-tracking branches:

  • Use the git config command. You can use the git config command to configure Git to prune remote-tracking branches automatically. To do this, run the following command:
git config fetch.prune true
  • Use the git fetch command with the --prune option. You can also prune remote-tracking branches manually by using the git fetch command with the --prune option. To do this, run the following command:
git fetch --prune

My Bookshelf

Reading Now

Other Stuff