Last active
September 18, 2025 06:27
-
-
Save ArtMin96/127d009b8aa4cbd35211d94b6adee5e2 to your computer and use it in GitHub Desktop.
Git script to find parent branch of a branch
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
| #!/bin/sh | |
| # | |
| # git-find-parent.sh | |
| # Find the most likely parent branch for a given feature branch. | |
| # | |
| branch="$1" | |
| # If no branch passed, use current branch | |
| if [ -z "$branch" ]; then | |
| branch=$(git rev-parse --abbrev-ref HEAD) | |
| fi | |
| # Search local + remote branches except the branch itself | |
| git for-each-ref --format='%(refname:short)' refs/heads/ refs/remotes/ \ | |
| | grep -v "^$branch$" \ | |
| | while read b; do | |
| base=$(git merge-base "$b" "$branch" 2>/dev/null) || continue | |
| [ -n "$base" ] && echo "$(git show -s --format='%ct %h %s' "$base") $b" | |
| done \ | |
| | sort -n \ | |
| | tail -n1 \ | |
| | awk '{gsub(/^origin\//,"",$NF); print $NF}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment