DEV Community

darkyM
darkyM

Posted on • Edited on

A couple of useful git aliases

We are using YouTrack for project tracking and we are naming our feature branches by their corresponding YouTrack issue ID. The reason behind this is that we have built a lot of automation upon it.

When we are creating a new feature branch we always start with an empty, let's call sentinel commit with the YouTrack ID which allows us to easily identify the branch starting point and also helps a lot with scripting.

In order not to forgot this initial empty commit, we created a small alias which takes care of this for us (please note that these scripts were created in Windows terminal, for Linux you may need to turn off history expansion):

git config --global --replace-all alias.br "!f() { export YTID=$1 && shift && git switch -c $YTID && git commit -m \"$YTID: Starting point \" --allow-empty;};f"
Enter fullscreen mode Exit fullscreen mode

We are always prefixing the commits with the branch name, as this let's us later identify the original branch that contained the commit. For merge and cherry-pick scenarios it could be super-helpful:

git config --global --replace-all alias.cm "!f() { IFS=$'\n' && for L in $(git log --format=oneline --no-decorate); do YTID=`sed -n -E 's/^[0-9abcdef]+.([A-Z]+\-[0-9]+): Starting point.*/\1/p' <<< $L`; [[ -n ${YTID} ]] && break; done; git commit -m \"$YTID: $@\"; }; f"
Enter fullscreen mode Exit fullscreen mode

In practice when you start working on a feature, simply put

git br ISSUE-11
Enter fullscreen mode Exit fullscreen mode

When commiting:

git cm "some meaningful commit message"
Enter fullscreen mode Exit fullscreen mode

After checking with git log:

e819c10 ISSUE-11: Third commit
87c7b4c ISSUE-11: Second commit
78fd4fc ISSUE-11: First real commit
7717e9c ISSUE-11: Starting point
8f6737d (origin/main, main) ISSUE-10 Lastly merged feature with squash commit
Enter fullscreen mode Exit fullscreen mode

Top comments (0)