Home > front end >  Git alias for commit with branch name
Git alias for commit with branch name

Time:12-17

I'm trying to create a simple bash alias to commit with my branch name in MacOs. For instance, if my branch if CS-12 I'd usually commit as follows:

git commit /file/location/myfile -m "CS-12 my message goes in here"

So I'm trying to create an alias which will receive only the file name and the message, ie:

gcm /file/location/myfile "my message goes in here"

I've got the following but it's not working:

alias gcm="echo git commit $1 -m \"$(current_branch) - $2\""

where current_branch is the function:

function current_branch() {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || \
  ref=$(git rev-parse --short HEAD 2> /dev/null) || return
  echo ${ref#refs/heads/}
}

which does work.

The output of running my alias:

gcm src/pages/register/Register.js "aasdasd asdasd"

is giving me back:

git commit -m master - src/pages/register/Register.js aasdasd asdasd

any idea what I'm doing wrong? Bash is not my area of expertise. Thanks

CodePudding user response:

The escaped quotes are 'stripped' by alias, so you need to escape them once more:

alias x="echo \\\"foo\\\""
x
"foo"

CodePudding user response:

aliases do not take parameters. Just write a function:

gcm() { git commit "$1" -m  "$(current_branch) - $2"; }

Note that there's really no need for aliases, and you shouldn't use them. Since at least 1996, the bash man page has stated: "For almost every purpose, aliases are superseded by shell functions."

  • Related