Blog Post

Deleting Old Local Git Branches–#SQLNewBlogger

,

I had a lot of local branches for a repo (actually a few repos). I know these are old and not used anymore, so how do I delete them? This post shows how to do that on Windows.

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers. You can see all posts on Git as well.

The Problem

As I’ve been making changes for various SQL Saturday events

I saw this SO post, which was a good starting point. I grabbed this code, which I’ll explain below.

git fetch -p && git branch -vv | awk ‘/: gone]/{print $1}’ | xargs git branch -d

The problem is this doesn’t work on Windows.

2024-06-23 10_24_52-cmd

Running This on Windows

I assume most of you installed Git and have Git Bash. The xargs and awk commands are Unix/Linux ones, so you need a bask shell to tun them

The solution for me, was to open a bash shell in the repo with the right click menu on Windows.

2024-06-23 10_33_35-sqlsatwebsite

Then run the code:

2024-06-23 10_24_44-MINGW64__e_Documents_git_sqlsatwebsite

Local branches removed. Well, almost; read the next section.

Additions

Note that in the first execution, I had two errors noting that there were some unmerged branches. When I look at these, I see they were old branches, ones that haven’t been used in years. I’m guessing either I was fixing something for someone, or they fixed something in another branch.

So, I forced delete by re-running the command a capital D.

How this Works

This code uses some Unix based utilities that I haven’t used in a long time. The flow of this is similar to how PowerShell, or even VBScript works, but on a single line. In this case, this code:

  • Gets a list of branches from the remote with git fetch after pruning the references for local branches that don’t exist on te remote.
  • Run the branch command with the verbose output. Could be –verbose as well
  • Take the output if the previous step and pipe that through awk. This command will parse text, looking for “gone” in a line and then printing the branch name.
  • This text is then taking with xargs and passing it to the git branch command with the delete option.

Note this doesn’t force delete branches.

SQL New Blogger

This post took about 20 minutes to write. I spent about 5 minutes checking a few code examples online, and then tried one after I’d killed branches from GitHub. I don’t have a great solution there, but I don’t do this often and I can click a few buttons to manage this.

I then structured this post with a few screenshots and spent 15 minutes working on it. I’d actually sketched it in 5 minutes with the major sections and a sentence in each and realized this would be quick to write, so I just filled it in on a Sunday morning.

You could do this as well and give an interviewer something to ask you in the next interview. This might catch their eye. I’d also suggest (and I will) do a few posts on awk and xargs. Those are good skills to have and you might spent 20 minutes experimenting and having fun with them.

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating