How to prune local tracking branches that do not exist on remote anymore?

 

To prune or delete local tracking branches that no longer exist on the remote repository, you can use the git fetch command with the --prune option. This will update your remote tracking branches and remove references to branches that have been deleted on the remote. Here's how you can do it with an example:

Suppose you have a Git repository and want to prune local tracking branches that no longer exist on the remote.

bash
# First, fetch the latest changes from the remote and prune deleted branches git fetch --prune

This command fetches the latest changes from the remote repository (git fetch) and the --prune option tells Git to remove references to remote branches that no longer exist on the remote.

After running this command, any local tracking branches that correspond to branches deleted on the remote will be removed from your local repository.

For example, if you had a local tracking branch called feature/old-branch that tracked a remote branch that was deleted on the remote, running git fetch --prune would remove the feature/old-branch local tracking branch.

It's a good practice to periodically prune your local tracking branches to keep your Git repository clean and up to date with the remote repository.

Comments