Using Git Plumbing & Porcelain to clean up local branches

Using Git Plumbing & Porcelain to clean up local branches

πŸΊοΈπŸ‘¨πŸ»β€πŸ”§ Two methods for safely and effectively removing outdated branches from your local git repository that are no longer on remote

Β·

4 min read

What is Git?

Version Control Logo PNG Vectors Free Download

Git is a distributed version control system that allows developers to maintain a version-controlled repository of their project code base. This provides the ability to track and roll back changes, as well as to collaborate with others on the same project.
Git also allows for branch management, enabling developers to create separate branches for different features or bug fixes, and then merge those branches back together when the features are complete.
This allows for efficient development, allowing for feature development to take place in parallel and for easy tracking of changes.

What is the problem?

39 Sync Error Icons - Free in SVG, PNG, ICO - IconScout

On occasion, local branches can become out-of-sync with the remote repository, and no longer appear on the remote.
I will show two methods for safely and effectively removing these branches from your local repository.

Git plumbing and porcelain πŸΊπŸ‘¨πŸ»β€πŸ”§πŸ€”

Plumbing and Porcelain are two method of operation that can be used on Git.

Plumbing refers to the lower-level command-line utilities and APIs of Git, which are used to manipulate the repository directly and create custom workflows, for more details you can check this.
Porcelain, on the other hand, refers to the higher-level commands designed to be user-friendly and provide a simplified interface to the repository, performing common tasks such as committing, branching, and merging.
These two methods of operation are used by both advanced and regular users to work with their Git repositories.

[Plumbing] Use the Git For-Each-Ref Command πŸ‘¨πŸ»β€πŸ”§

This method uses the low-level "plumbing" command git for-each-ref, which works on the Git repository directly. It uses the variable %(upstream:track), which will be "[gone]" when the branch is no longer there. Here is the command to use:

git fetch -p && for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); do git branch -D $branch; done

Command explanation:

  1. Updates the remote references with git fetch -p, which fetches the state of the remote repository and updates the local branches with any new changes.

  2. Retrieves a list of branches with git for-each-ref, which allows you to iterate through all of the local branches and manipulate them.

  3. Searches for branches with awk, which can be used to search through the list of branches and find those which have upstream tracking info of "[gone]".

  4. Removes the "refs/heads/" prefix from the branch names, which is used to identify the branches and make them easier to keep track of.

  5. Deletes these branches with git branch -D, which is used to delete branch references and their associated objects from the local repository.

[Porcelain] Use the Git Branch Command 🏺️

This method uses the "porcelain" command git branch, which works in a more user-friendly and intuitive way

git fetch -p && for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do git branch -D $branch; done

Command explanation:

  1. Updates the remote references with git fetch -p, which fetches all the objects from the remote repository and updates the references in the local repository.

  2. Retrieves a list of branches with git branch -vv, which shows the branch names and their associated commit IDs.

  3. Searches for branches with grep, which searches for lines that match the specified patterns. It looks for branches where the remote status is "gone".

  4. Retrieves the names of the branches with awk, which is a programming language used for pattern scanning and text processing.

  5. Deletes these branches with git branch -D, which deletes the specified branch.

Overall

Removing Git tracking branches is a straightforward process that can help keep your Git repository organized and up-to-date.
To do so, you can use the git for-each-ref or git branch commands. The git for-each-ref command allows you to list all branches on the remote, while the git branch command allows you to list all the local branches and then manually delete those that are no longer on the remote.
Doing this regularly helps ensure that your Git repository stays organized and up-to-date.

Β