2

I want to have one git alias to commit a message and auto filling the branch name in the commit message. output example: git commit -m "[EX-1234] This is the commit message"

I am looking for a way to only type in the terminal: git cm this is the commit message and it will execute the output example.

Things I have tried

cm = "git commit -m \'[$(current_branch)] ${1}\'"
cm = "!f() { \
         git commit -m '[$(current_branch)] $1'; \
       }; f"
cm = '!sh -c '\''git commit --m  "${1}"'\'' -'

The above examples dont work

user234562
  • 631
  • 9
  • 20
  • I suggest to use a function. – Cyrus Jul 10 '19 at 19:54
  • 1
    An alias is fine for that, but you might consider using a [hook](https://stackoverflow.com/questions/54077959/parse-branch-name-initiate-commit-with-name-in-the-commit-message/54078057#54078057), it'll allow you to have this feature while keeping the possibility to have specific commit aliases without making it too complex. – Romain Valeri Jul 10 '19 at 20:29

1 Answers1

4

Use $@ to propagate all the arguments to the underlying command, as in:

cm = "!f() { git commit -m "[$(current_branch)] $@"; }; f"
IronMan
  • 1,854
  • 10
  • 7
  • Thanks :) I get this error now: `f() { git commit -m [$(current_branch)] $@; }; f: current_branch: command not found` $(current_branch) is a bash command and maybe not possible in alias? – user234562 Jul 10 '19 at 19:58
  • 1
    The idea is good. Maybe just tweak the "branch getting" part with `$(git rev-parse --abbrev-ref HEAD)`. Full syntax to set it : `git config --global alias.cm '!f() { git commit -m "$(git rev-parse --abbrev-ref HEAD) $1"; }; f'` then to use it : `git cm "useful message here"`. Also, notice that I've grouped the arguments into one, just use it with a quoted message as in my example, it'll be taken as a unique argument. – Romain Valeri Jul 10 '19 at 20:35