# How to Prune Local Tracking Branches That Do Not Exist on Remote Anymore?

Pruning local tracking branches that no longer exist on the remote is a common task to clean up stale branches and maintain a tidy repository. You can achieve this by using Git commands that help synchronize your local branches with the remote repository. Here’s a step-by-step guide:

### Steps to Prune Local Tracking Branches

1. **Fetch Remote Updates**
    
    Start by fetching updates from the remote repository. This step ensures that your local repository is aware of the latest changes in the remote branches.
    
    ```
    git fetch --prune
    ```
    
    - `-prune`: This option removes any remote-tracking references that no longer exist on the remote. It updates your local knowledge of remote branches, which is useful for subsequent cleanup.
2. **List Local Branches Tracking Remote Branches**
    
    To see which local branches are tracking remote branches, use:
    
    ```
    git branch -vv
    ```
    
    This command lists all local branches along with their upstream tracking branches and indicates if they are behind or ahead of the remote.
    
3. **Prune Local Branches That No Longer Have Remote Counterparts**
    
    Git does not have a built-in command to directly remove local branches that no longer have corresponding remote branches, but you can use a combination of commands and scripting to achieve this.
    
    Here's a one-liner script that prunes local branches that have been deleted from the remote:
    
    ```
    git branch -vv | awk '/: gone]$/ {print $1}' | xargs git branch -d
    ```
    
    - `git branch -vv`: Lists local branches with verbose information.
    - `awk '/: gone]$/ {print $1}'`: Filters branches that are marked as gone (i.e., their remote counterpart no longer exists).
    - `xargs git branch -d`: Deletes each branch listed by `awk`.
    
    **Note**: This script uses `-d` to delete branches. If you want to force delete branches that have not been merged, use `-D` instead.
    
4. **Manual Cleanup**
    
    If you prefer manual cleanup or want to double-check which branches to delete:
    
    - List branches that are tracking a remote but are no longer present:
        
        ```
        for branch in $(git branch -vv | awk '/: gone]$/ {print $1}'); do
          echo $branch
        done
        ```
        
    - Verify the branches and then manually delete them with:
        
        ```
        git branch -d <branch-name>
        ```
        

### Example Workflow

1. **Fetch Updates and Prune Remote References**:
    
    ```
    git fetch --prune
    ```
    
2. **List Local Branches with Tracking Information**:
    
    ```
    git branch -vv
    ```
    
    Example output:
    
    ```
    feature-x          a1b2c3d [origin/feature-x: gone] Some description
    bugfix-y           d4e5f6g [origin/bugfix-y] Another description
    ```
    
    In this example, `feature-x` has been deleted from the remote.
    
3. **Automatically Delete Local Branches That No Longer Exist on Remote**:
    
    ```
    git branch -vv | awk '/: gone]$/ {print $1}' | xargs git branch -d
    ```
    
4. **Verify Branch Deletion**
    
    After running the deletion script, you can verify that the branches have been removed by listing your local branches:
    
    ```
    git branch
    ```
    

### Summary

To prune local tracking branches that no longer exist on the remote:

1. Use `git fetch --prune` to update your local references of remote branches.
2. List local branches and identify which ones are tracking deleted remote branches.
3. Use the provided script or manually delete local branches that no longer have corresponding remote branches.

Regularly cleaning up local branches helps keep your repository organized and ensures you’re not working with outdated references.