Development

Delete git branches that do not exist on remote

Advertisements

After working on a project for a while, there will come a time when we will end up with a lot of local branches that have been merged on remote but still exist on our local machine. To delete git branches that do not exist on remote, we can perform the following steps:

  1. Switch to the main branch of the project (master/main)
  2. Run the command:
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d
Bash

Note: You need to be on the main/master branch because this command compares against the head and so you want to be on the branch to ensure that it does not get deleted.

Breaking down the command

git fetch -p: fetches all the branches and commits from the remote repository. It updates the local repository, and the -p flag tells git to remove remote-tracking references (i.e., origin/branch-name) that no longer exist on the remote repository.

&&: is a logical operator that executes the second command only if the first one succeeds.

git branch -vv: lists all local and upstream branches in very verbose mode. It displays detailed information about each branch and also specifies “[gone]” next to a branch if the local branch’s upstream branch does not exist.

|awk '/:gone]/{print $1}': pipes the verbose output to awk which is used to process text. awk matches lines containing :gone]. In awk, a line is delimited by a whitespace by default and $1 refers to the first field in that line. So {print $1} prints the first field of the matching lines which is the branch names in this case.

|xargs git branch -d: pipes the output of the first field that we got from the previous part of the command (branch names) to xargs. xargs is used to build and execute commands from input. So it runs git branch -d for every input line that it gets. So it deletes the branch names that it receives line by line for each input.

And thus we delete git branches that do not exist on remote one by one. Let us know in the comments if you have any questions.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria
Tags: git

Recent Posts

Remapping keyboard keys to avoid Carpal Tunnel

I am terrible at optimizing my keyboard layout for anything. But off lately, my little…

4 weeks ago

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

5 months ago

Generating a QR code using Node.js

I was preparing a slide deck for a hackathon and decided to put in a…

6 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

6 months ago

Copy/Pasting output from the terminal

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…

7 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

8 months ago
Advertisements