I first started with an alias in my .bash_profile
1 2 |
alias b='git branch' alias gco='git checkout' |
The drawback was that although it is shorter, it doesn’t give you suggestions based on existing branches when pressing “tab” (same as unix commands work for folders/files). Git checkout command does.
So I created an alias to checkout develop.
1 |
alias gcd='git checkout develop' |
But I wanted to use it for any branch…
Then I wrote a script (see below) and added another alias
1 |
alias gc='/c/APPS/scripts/git/git-checkout-fuzzy.sh' |
The script does a git checkout for a first branch that matches the first script argument
1 |
gc branchNameOrJustUniquePartOfTheName |
Full example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$ b * develop feature/JIRA-123-show-some-analytics master $ gc ana Running /c/APPS/scripts/git/git-checkout-fuzzy.sh with parameter [ana] Matching branches: - feature/JIRA-123-show-some-analytics Checking out feature/JIRA-123-show-some-analytics Switched to branch 'feature/JIRA-123-show-some-analytics' Your branch is up-to-date with 'origin/feature/JIRA-123-show-some-analytics'. |
And here is the script – git-checkout-fuzzy.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/usr/bin/env bash # git checkout for first branch that matches the first script argument echo "Running $0 with parameter [$1]" PART_OF_BRANCH_NAME=$1 if [ -z "${PART_OF_BRANCH_NAME}" ]; then echo "Please pass 1 parameter with part of a branch name"; exit 1; fi echo "Matching branches: " # added "- " as prefix for the list git for-each-ref --format='- %(refname:short)' refs/heads/ | grep ${PART_OF_BRANCH_NAME} foundBranchName=`git for-each-ref --format='%(refname:short)' refs/heads/ | grep ${PART_OF_BRANCH_NAME} | head -1` if [ -z "${foundBranchName}" ]; then echo "No matching branch found"; exit 2; fi echo "Checking out ${foundBranchName}" git checkout ${foundBranchName} |
Enjoy and please let me now in the comments below if you find it useful and it works for you.
UPDATE: In IntelliJ it is probably better to set a keyboard shortcut for VCS > Git > Branches…