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

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…

2 weeks ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

3 weeks ago

Node.js 20.6 adds built-in support for .env files

Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform…

4 weeks ago

Object destructuring in TypeScript

Object destructuring is a powerful ES 6 feature that can help developers write cleaner code.…

1 month ago

Improve git clone performance in a CI pipeline

Have you felt particularly annoyed by the time it takes to clone a large repository,…

2 months ago

Fix: Hydration failed because the initial UI does not match what was rendered on the server

Within a React or Next.js app, if you encounter the error "Hydration failed because the…

2 months ago
Advertisements