bash 中的函数用于在一个命令中提交和推送
我尝试提交并推送一个命令,输入如下内容:
gm "This is my commit message"
它将运行:
git commit -am "This is my commit message" && ; git push
我已经在我的 .bashrc
中尝试过这个函数:
function gm() {
git commit -am $1 && git push
}
它有效,除了我无法输入几个单词作为提交消息这一事实。我可以运行
gm My_Message
并且它工作正常,但是如果我输入几个单词,例如
gm This is my message
它只会运行 git commit -am "This “&& git 推送
。我尝试使用引号,但它返回错误。我应该如何配置这个功能才能工作?
I try to commit and push in one command, typing something like:
gm "This is my commit message"
Which would run:
git commit -am "This is my commit message" && git push
I've tried this function in my .bashrc
:
function gm() {
git commit -am $1 && git push
}
Which works, except for the fact that I can not type several words as commit message. I can run
gm My_Message
and it works fine, but if I type several words, like
gm This is my message
It will only run git commit -am "This" && git push
. I tried using quotation marks but it returns an error. How should I configure this function to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要在调用函数和 git 命令的地方加上引号。
另一种方法是:
但这意味着:(
其中 \t 实际上是一个制表符)
将给出以下提交消息:
因此所有空白区域都已折叠。
另外,如果您想输入更复杂和完整的提交消息,您可能需要
$EDITOR
并完全省略-m
。此外,如果您立即推送每个提交,那么您并没有真正使用分布式版本控制的强大功能。
You need quotes around both the place calling the function and the git command.
An alternative approach is:
But this will mean that:
(Where \t is actually a tab)
will give the commit message of:
So all the white space is collapsed.
Also, if you want to enter a more complicated and complete commit message, you might want to you
$EDITOR
and omit-m
entirely.Also, you aren't really using the power of distributed version control if you immediately push every commit.
新的、扩展的答案
这最接近您的要求:
git commit -am "$*" && git push
$1 具体表示(空格分隔)列表中的第一个参数。
注意。我最初写的是“$@”,但它扩展为“$1”“$2”...,这不是您想要的:“$*”扩展为包含所有参数的单引号字符串。
自己简单地引用字符串可能更明智,特别是如果您关心 shell 删除空格(因此运行
gm "my commit message"
并使用"$1"
道格拉斯描述的语法)如果您真正关心格式设置,那么使用外部编辑器是最好的选择。只需完全省略
-m
参数,确保将$EDITOR
设置为您喜欢使用的内容,然后看看这个 关于 git 提交消息的注释。New, expanded answer
This is closest to what you're asking for:
git commit -am "$*" && git push
$1 specifically means the first argument in a (whitespace-seperated) list.
NB. I originally wrote "$@", but that expands to "$1" "$2" ..., which isn't what you want: "$*" expands to a single quoted string with all arguments.
Simply quoting the string yourself may be more sensible, especially if you care about the shell chomping whitespace (so run
gm "my commit message"
and use the"$1"
syntax Douglas describes)Using an external editor is best if you really care about formatting. Just omit the
-m
argument entirely, make sure$EDITOR
is set to something you like to use, and take a look at this note about git commit messages.恕我直言,更好的方法是:
它将为本机 git 命令设置一个名为
acp
的别名。现在您可以轻松运行
git acp "commit messages here!"
IMHO, a better approach would be:
which will set an alias named
acp
for native git command.Now you can easily run
git acp "commit messages here!"
您需要用引号将单词括起来,例如
gm“这是我的消息”
。You'll have you encapsulate the words with quotes, like
gm "this is my message"
.