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

2
  • I suggest to use a function. Commented Jul 10, 2019 at 19:54
  • 1
    An alias is fine for that, but you might consider using a hook, it'll allow you to have this feature while keeping the possibility to have specific commit aliases without making it too complex. Commented Jul 10, 2019 at 20:29

1 Answer 1

4

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

cm = "!f() { git commit -m "[$(current_branch)] $@"; }; f"
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.