Hitomi90
Hitomi90

Reputation: 33

Adding an alias for git commit so that no quotes are needed

In git, when I commit a change I use:

git commit -m "my commit message"

I find this time consuming to type out each time, especially since I have a habit of making many small commits over time

I want an alias that makes this easier, for example

gm commit message

I don't like having to type the quotes before and after my commit message, and "gm" is a lot quicker than "git commit -m"

what is the best way to make this alias in my .zshrc file?

Upvotes: 0

Views: 645

Answers (2)

HappyFace
HappyFace

Reputation: 4093

gm() { git commit -m "$*" }
gm this works
gm but you need to quote special chars\!

Upvotes: 1

user10678532
user10678532

Reputation:

In zsh:

setopt interactive_comments
preexec(){ _lc=$1; }
alias gm='git commit -m "${_lc#gm }" #'

then:

gm ** you can use ", ) or any other char's in the commit message

Notice that other of your aliases, etc. may use the preexec function; you may have to modify it instead of just overriding it.


In bash or ksh93, there's no preexec, but you can get the current command from the history:

alias gm='_lc=$(fc -nl -0); git commit -m "${_lc#*gm }" #' 

Also, bash and ksh93 recognize comments by default in interactive scripts.

Upvotes: 2

Related Questions