Created
March 30, 2026 17:56
-
-
Save harsh183/7d098272e9555dac725fde5a323557a7 to your computer and use it in GitHub Desktop.
Quick command to automatically find the parent branch to stack as a base branch when creating a PR on GitHub
This file contains hidden or 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 | |
| current = `git branch --show-current`.strip | |
| branches = `git for-each-ref --format='%(refname:short)' refs/heads/`.split | |
| branches.reject! { |b| b == current } | |
| base = branches | |
| .map { |b| [b, `git rev-list --count #{b}..#{current}`.strip.to_i] } | |
| .min_by { |_, count| count } | |
| &.first | |
| if base | |
| puts "Base: #{base}" | |
| exec "gh", "pr", "create", "-B", base, *ARGV | |
| else | |
| exec "gh", "pr", "create", *ARGV | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This also works if the base branch is master