Course contentsShow
← Git & GitHub
Lesson 57 of 963. Branching and TeamworkFree lesson

Cleaning Up Stale Remote Tracking Branches

Learners will prune local references to remote branches that have been deleted on GitHub to maintain a clean and accurate repository status.

In this lesson, you will learn how to prune your local repository to remove references to remote branches that have already been deleted from GitHub.

When you work in a team, your colleagues frequently create, merge, and delete branches on GitHub. While your local repository tracks these branches, it doesn't automatically "forget" them when they are deleted on the server. Over time, your git branch -r list becomes cluttered with "stale" tracking branches—references that point to branches no longer existing on the remote.

Keeping these around can lead to confusion, making it difficult to distinguish between active ongoing work and abandoned remnants of old features.

Pruning Stale References

To synchronize your local view with the reality of the remote server, you use the git fetch command with the --prune flag. This command reaches out to the remote, identifies which branches have been deleted on GitHub, and removes the corresponding local tracking references.

Run the following command in your terminal:

git fetch --prune

If you prefer to make this behavior the default for all future fetches, you can configure your Git settings globally:

git config --global fetch.prune true

Once you run this, a quick git branch -r will show a much cleaner list, confirming that your repository is no longer tracking deleted remote work. By performing this regularly, you ensure that your local environment remains an accurate representation of the shared codebase, preventing you from accidentally trying to pull or merge code from branches that have already been retired.

Key Takeaway: Use git fetch --prune to automatically delete local references to remote branches that have been removed from GitHub, keeping your repository state clean and manageable.