为git commit添加别名,因此不需要引号 [英] Adding an alias for git commit so that no quotes are needed
问题描述
在git中,当我提交更改时,我使用:
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
我不喜欢在提交消息前后输入引号,而"gm"比"git commit -m"
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"
在我的.zshrc文件中创建此别名的最佳方法是什么?
what is the best way to make this alias in my .zshrc file?
推荐答案
在 zsh 中:
setopt interactive_comments
preexec(){ _lc=$1; }
alias gm='git commit -m "${_lc#gm }" #'
然后:
gm ** you can use ", ) or any other char's in the commit message
请注意,您的其他别名等也可以使用 preexec
函数;您可能需要修改它,而不仅仅是覆盖它.
Notice that other of your aliases, etc. may use the preexec
function; you may have to modify it instead of just overriding it.
在 bash 或 ksh93 中,没有 preexec
,但是您可以从历史记录中获取当前命令:
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 }" #'
此外,默认情况下,bash和ksh93会在交互式脚本中识别注释.
Also, bash and ksh93 recognize comments by default in interactive scripts.
这篇关于为git commit添加别名,因此不需要引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!