git find
If you’re like me you might have tens (or hundreds?) of “draft branches” from various spikes, experiments and aborted attempts at features.
git find
searches through your local branches to find a branch that has the code you’re looking for in it’s diff. Usage:
$ git find
Search through all local branches for a string in the diff. Usage:
git-find <string>
git-find <string> <base_branch>
$ git find vnc
base branch: master
DAT-20177_debug_flaky_specs
DAT-20177_flaky_drafts
To set this up, place this script somewhere in your path. Git will automatically translate git find
to git-find
:
git-find
:
#! /bin/bash
# set the default base branch
DEFAULT_BASE_BRANCH="master"
if [ -z "$1" ]; then
echo -e "\nSearch through all local branches for a string in the diff. Usage:\n"
echo -e "git-find [string]"
echo -e "git-find [string] [base_branch]\n"
exit 1
fi
base_branch="${2:-$DEFAULT_BASE_BRANCH}"
# Check if the base branch exists
if ! git show-ref --verify --quiet "refs/heads/$base_branch"; then
echo "Base branch '$base_branch' does not exist."
exit 1
fi
echo "base branch: $base_branch"
for branch in $(git branch --format='%(refname:short)'); do
diff_output=$(git diff "$(git merge-base "$branch" "$base_branch")" "$branch" | grep "$1")
if [ -n "$diff_output" ]; then
echo "$branch"
fi
done