27
Ruby Script to Find Local Branches with Deleted Remotes
In the Forem codebase we use a Squash and Merge. Prior to joining Forem, I was accustomed to the Create a Merge Commit strategy.
With the Create a Merge Commit strategy, I have a script to Prune Branches. That script tidied up branches that git understood to have been merged into the “main” branch.
However, that script doesn’t work with the Squash and Merge strategy. So I needed to come up with something different. (And please if you are aware of something else that’s part of git let me know).
At the bottom of this post is that code. With that code I can do the following: cd /path/to/repo; local-branches-with-missing-remote
That command will print to STDOUT
the list of local branches whose tracking branch is now gone; likely because I merged a pull request which automatically deletes the branch on Github.
Whenever I push up a branch, I always use git push --set-upstream
to create the tracking branch connection.
Then, when I want to tidy up my local branches, I can run the following: cd /path/to/repo; local-branches-with-missing-remote | xargs git remote -D
.
This helps keep my local branch list nice and tidy, which is super important to me as I’ve been issuing lots of tiny pull requests to do some minor refactors to the Forem codebase.
A future me might write an alias to shorten this verbose command title and xargs
add-on, but not today.
I’m using Unix’s xargs command to take each line of output and run the git remote -D
command on that.
The xargs
command is analogous to the anonymous function of a block in Ruby.
# `local-branches-with-missing-remote` "piped" into
["branch-1", "branch-2", "branch-3"].each do |line|
# `xargs git remote -D`
exec("git remote -D #{line}")
end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
############################################################## | |
# | |
# Begin Commentary | |
# | |
############################################################## | |
# | |
# First, if you want to use this, I recommend you change the | |
# the file mode to executable: | |
# | |
# `chmod +x local-branches-with-missing-remote` | |
# | |
# This command writes to STDOUT a list of local branches that | |
# have upstream tracking branches, but who's upstream branches | |
# are now gone. | |
# | |
# Fair warning, this perhaps follows some of my internalized | |
# assumptions about repository setup. | |
# | |
# I would love if there already existed a function that does | |
# this, but part of why I write code is to also practice | |
# algorithms and critical thinking. | |
# | |
# Example to list local branches with missing remotes: | |
# | |
# $ cd ~/git/forem ; local-branches-with-missing-remote | |
# | |
# Example to delete local branches with missing remotes: | |
# | |
# $ local-branches-with-missing-remote | xargs git remote -D | |
# | |
# | |
############################################################## | |
# | |
# Begin Function | |
# | |
############################################################## | |
# | |
# Prune the remote branches that are gone. Also, don't worry | |
# about the output | |
`git remote update origin --prune >/dev/null 2>&1` | |
# The remote branch lines are of the form: | |
# | |
# - space | |
# - origin/branch-name | |
# | |
# Note: by convention, origin is the upstream I'm concerned | |
# about | |
remote_branches = `git branch --remotes` | |
.split("\n") | |
.map(&:strip) | |
# The verbose branch lines are of the form: | |
# | |
# - Branch Name | |
# - many spaces | |
# - SHA of latest commit | |
# - space | |
# - OPTIONAL: "[Remote Name]" | |
# - space | |
# - Subject of latest commit | |
local_verbose_branches = `git branch -vv` | |
.split("\n") | |
.map { |b| b.gsub(/^\*? +/, '').strip } | |
local_verbose_branches.each do |verbose_branch| | |
# Definitely don't want to delete this local branch. | |
next if verbose_branch.start_with?("main") | |
# Though I no longer use "master" for my projects, there are | |
# some outliers that do. | |
next if verbose_branch.start_with?("master") | |
# I might have a local branch that doesn't have a remote | |
# tracking branch. Skip those | |
next unless verbose_branch.include?("[origin") | |
branch_name = verbose_branch.split(/ +/)[0] | |
# Test if local branch has a remote branch | |
next if remote_branches.detect do |rb| | |
rb.include?(branch_name) | |
end | |
# Output the branch name for a local branch with what | |
# appears to be a removed upstream branch (e.g., the | |
# upstream branch was likely merged and deleted) | |
puts branch_name | |
end |
27