Delete git branches that do not exist on remote
source link: https://www.wisdomgeek.com/development/delete-git-branches-that-do-not-exist-on-remote/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Delete git branches that do not exist on remote
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:
- Switch to the main branch of the project (master/main)
- Run the command:
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d
BashNote: 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.
Recommend
-
130
Sometimes there are many branches, accumulated over the years in a git repository. And you need to delete it to save some space or make management of your repository easy. After that, you can iterate…
-
4
Erik Smith Posted on Oct 30...
-
3
Danyson Posted on Jan 6...
-
4
Are you feeling overwhelmed with too many git branches? Do you know which branches have been merged and which can be safely deleted? Do you leave your git branches to pile up on your local machine until your git branch command sp...
-
7
Delete all local git branches except the main branch If you’re a bit like me, you like to keep your desk clear. The same goes for all those pesky git branches you accumulate over time. Whether you are using Visual Studio, VSCode, or...
-
4
Delete Stale Branches · GitHub Delete Stale Branches Found if in case of lastCommitTimestamp is NUL...
-
2
How to List Remote Branches in Git, and Why You Need To By Idowu Omisola Published 11 hours ago Remote branch...
-
6
Delete Git Branches That Have Been Merged August 04, 2022
-
5
There are 4 different Git commands you can enter into your command line to list all of the remote branches of a repo. I will show you command line code examples of each of these.For all of these examples, I will use
-
3
August 16, 2022 /
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK