Home > Software engineering >  Git alias for commit with message including part of branch name
Git alias for commit with message including part of branch name

Time:08-02

I am trying to write git alias, which would create commit with message including given parameter and part of a branch name. For example I am on the branch feature/my-branch, so the commit message should look like this:

feature: given message

- my-branch

So far I was able to come up with this:

feat = "!myF() { git commit -m \"feature: $1\n - $(git symbolic-ref --short -q HEAD) \" ; }; myF "

What I cannot achieve is to remove feature/ from the branch name (it will always be there). I tried bash replace feature, but it requires variable, which I don't know how to create in git alias syntax, and sed command, but that requires input file.

CodePudding user response:

You can add multiple commands in your alias function.

name="$(git symbolic-ref --short -q HEAD)"
echo ${name#feature/}  # removes any feature/ prefix

So your alias:

feat = "!myF() { name=\"$(git symbolic-ref --short -q HEAD)\"; git commit -m \"feature: $1\n - ${name#feature/} \" ; }; myF "

  •  Tags:  
  • git
  • Related