在 bash 中提交和推送一个命令的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7852148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:59:11  来源:igfitidea点击:

Function in bash to commit and push in one command

gitbash

提问by Douglas Leeder

I try to commit and push in one command, typing something like:

我尝试提交并推送一个命令,键入如下内容:

gm "This is my commit message"

gm "This is my commit message"

Which would run:

哪个会运行:

git commit -am "This is my commit message" && git push

git commit -am "This is my commit message" && git push

I've tried this function in my .bashrc:

我已经在我的.bashrc

function gm() {
     git commit -am  && git push
}

Which works, except for the fact that I can not type several words as commit message. I can run

哪个有效,除了我不能输入几个单词作为提交消息。我可以跑

gm My_Message

gm My_Message

and it works fine, but if I type several words, like

它工作正常,但如果我输入几个单词,比如

gm This is my message

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?

它只会运行git commit -am "This" && git push。我尝试使用引号但它返回一个错误。我应该如何配置此功能才能工作?

回答by Douglas Leeder

You need quotes around both the place calling the function and the git command.

调用函数和 git 命令的地方都需要引号。

function gm() {
     git commit -am "" && git push
}

gm "This is my message"

An alternative approach is:

另一种方法是:

function gm() {
     git commit -am "$*" && git push
}

gm This is my message

But this will mean that:

但这将意味着:

gm This commit  has double  spaced sections.\t And a tab, \
and a new line

(Where \t is actually a tab)
will give the commit message of:

(其中 \t 实际上是一个选项卡)
将给出以下提交消息:

This commit has double spaced sections. And a tab, and a new line

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 $EDITORand omit -mentirely.

此外,如果您想输入更复杂和完整的提交消息,您可能需要完全$EDITOR省略-m

Also, you aren't really using the power of distributed version control if you immediately push every commit.

此外,如果您立即推送每个提交,您并没有真正使用分布式版本控制的强大功能。

回答by Useless

New, expanded answer

新的、扩展的答案

  • This is closest to what you're asking for:

    git commit -am "$*" && git push

  • 这最接近您的要求:

    git commit -am "$*" && git push

$1 specifically means the first argument in a (whitespace-seperated) list.

$1 特指(空格分隔的)列表中的第一个参数。

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.

注意。我最初写的是“$@”,但它扩展为“$1”“$2”...,这不是您想要的:“$*”扩展为带有所有参数的单引号字符串。

  • 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 reallycare about formatting. Just omit the -margument entirely, make sure $EDITORis set to something you like to use, and take a look at this note about git commit messages.

  • 自己简单地引用字符串可能更明智,特别是如果你关心 shell chomping 空格(所以运行gm "my commit message"并使用"$1"Douglas 描述的语法)

  • 如果您真的关心格式,最好使用外部编辑器。只需-m完全省略参数,确保$EDITOR设置为您喜欢使用的内容,并查看有关 git commit messages 的注释

回答by Demonbane

IMHO, a better approach would be:

恕我直言,更好的方法是:

git config --global alias.acp '!f() { git add -A && git commit -m "$@" && git push; }; f'

which will set an alias named acpfor native git command.

这将为acp本地 git 命令设置一个别名。

Now you can easily run git acp "commit messages here!"

现在您可以轻松运行 git acp "commit messages here!"

回答by Griffin

You'll have you encapsulate the words with quotes, like gm "this is my message".

您将需要用引号将单词封装起来,例如gm "this is my message".