DEV Community

Ankur Sheel
Ankur Sheel

Posted on • Originally published at ankursheel.com on

Cleaning Up Unnecessary Remote-Tracking References

If your work involves collaborating on a monolithic project with multiple teams, you might find yourself in a situation where you're tracking numerous branches. However, you might soon realise that most of these branches are unnecessary for your specific work. This situation can lead to an avoidable increase in the time you spend updating your project, especially if you're habitually using git pull.

To streamline our Git workflow and save those precious seconds(or minutes), we should clean up those remote-tracking references.

Identifying the Branches to Keep

Before we start removing stuff, we should identify the branches that we want to keep. Typically, these would include the_main (or master)_ branch and a few select branches we actively work on.

To get their unique commit hashes, execute the following command, ensuring to replace origin/main , * *origin/my-branch-1**, and origin/my-branch-2 with the actual branch names.

git rev-parse --quiet origin/main origin/my-branch-1 origin/my-branch-2
Enter fullscreen mode Exit fullscreen mode

Note: I’m assuming the origin is the name of the upstream

The git rev-parse command translates human-readable branch names into cryptic yet significant SHA-1 hashes, which are essentially commit identifiers.

  • --quiet : Silence is golden, as this flag suppresses output unless there is an error.

When executed, the command will produce an output similar to this:

3896d7ba4b5549e8a362bc5e300a0dd8082f9e94
3896d7ba4b5549e8a362bc5e300a0dd8082f9e94
64abe6831bf1cb04396d1df02dd7c2ad6d999ab3
Enter fullscreen mode Exit fullscreen mode

These are the commit hashes associated with the branches we wish to retain.

Cleaning Up Unnecessary Local References

Now that we have the commit hashes, we can tidy up our local references to remote branches.

This can be done by using a combination of 2 commands - git branch and xargs. xargs is a command-line tool that helps build argument lists for other commands.

git branch -r \ 
--no-contains 3896d7ba4b5549e8a362bc5e300a0dd8082f9e94 \
--no-contains 3896d7ba4b5549e8a362bc5e300a0dd8082f9e94 \
--no-contains 64abe6831bf1cb04396d1df02dd7c2ad6d999ab3 \
| xargs -I{} git branch -rd "{}"
Enter fullscreen mode Exit fullscreen mode

Let's look at the options for the git branch command

  • -r : Lists the remote-tracking branches.
  • --no-contains : Filters the list of remote branches to include only branches that don't contain the specified commits (our identified hashes).

The output of this filtered list is then passed to xargs. xargs is a command that allows us to construct argument lists for other commands using input from standard input (stdin).

  • -I{} : This option tells xargs to replace every occurrence of {} with the input elements pulled from standard input (stdin).
  • git branch -rd : Deletes a remote branch.
    • "{}" : This is the placeholder, which gets replaced by the specific input item read from stdin.

Preventing this buildup in the future

Instead of calling git pull, use git fetch and git rebase instead.

git fetch origin main --prune --prune-tags
git rebase origin/main
Enter fullscreen mode Exit fullscreen mode
  • --prune : Before fetching, remove any remote-tracking references that no longer exist on the remote.
  • --prune-tags : Before fetching, remove any local tags that no longer exist on the remote if --prune is enabled.

Conclusion

Following these steps, we can declutter our Git workflow and keep only the relevant references.

References

Top comments (0)