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

发布于 2024-12-11 06:17:12 字数 562 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巡山小妖精 2024-12-18 06:17:12

您需要在调用函数和 git 命令的地方加上引号。

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

gm "This is my message"

另一种方法是:

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

gm This is my message

但这意味着:(

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

其中 \t 实际上是一个制表符)
将给出以下提交消息:

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

因此所有空白区域都已折叠。

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

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

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

function gm() {
     git commit -am "$1" && 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:

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 $EDITOR and omit -m entirely.

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

狂之美人 2024-12-18 06:17:12

新的、扩展的答案

  • 这最接近您的要求:

    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.

独孤求败 2024-12-18 06:17:12

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

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

它将为本机 git 命令设置一个名为 acp 的别名。

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

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 acp for native git command.

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

梦断已成空 2024-12-18 06:17:12

您需要用引号将单词括起来,例如 gm“这是我的消息”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文