Git custom command to checkout branch for a specific task
This custom git command is intended to simplify checkout of feature-branch for a specific task from issue tracker like Jira.
- Feature-branch must contain a task ID anywhere within it. The following branchs names are valid for task
ABC-1234
:ABC-1234-awesome-bugfix
,ABC-1234/awesome-bugfix
,feature/ABC-1234-awesome-feature
, and etc.
git checkout-task ABS-1234
If the command name looks long for you just add an alias for it:
git config --global alias.cot checkout-task
- Grep all branches including remote ones with the task ID
- Removes everything before task id thus
remotes/origin/ABC-1234-awesome-bugfix
becomes justABC-1234-awesome-bugfix
orremotes/origin/ABC-1234/awesome-bugfix
becomesABC-1234/awesome-bugfix
- Checkout that branch
C - case, B - behavior
C: You're already on the desired branch
B: No branch checkout. Info message printed
C: There are no branches with the specified prefix
B: No branch checkout. Error message printed
C: There is more than one branch with the specified prefix
B: No branch checkout. The names of clashing branches will be printed
Put the file git-checkout-task
to any folder that is in the PATH and make it executable.
For example:
cd ~ && \
mkdir .gitbin && \
cd .gitbin && \
curl -o git-checkout-task https://raw.githubusercontent.com/int02h/git-checkout-task/master/git-checkout-task && \
chmod +x git-checkout-task
Then add ~/.gitbin
to the PATH. Put the following line in the ~/.bashrc
, or ~/.zshrc
, or in the script for whatever shell you're using:
export PATH=$PATH:~/.gitbin
- Support slash
/
in branch name so bothABC-1234-awesome-bugfix
andABC-1234/awesome-bugfix
are valid
- Task ID can be anywhere within a branch name, not only at the beginning.
Copyright (c) 2023 Daniil Popov
Licensed under the MIT License.