bash 用于 git 提交的 Shell 脚本助手
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17930725/
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
Shell script helper for git commits
提问by Snowman
I'm trying to write a simple shell script that simplifies the git commit process.
我正在尝试编写一个简单的 shell 脚本来简化 git 提交过程。
Instead of
代替
git add . -A
git commit -m "message"
git push
I want to do commit.sh "my commit message"
我想要做 commit.sh "my commit message"
Here's what I have:
这是我所拥有的:
#!/bin/bash
commit_message=""
git add . -A
git commit -m $commit_message
git push
There's two problems with this:
这有两个问题:
When the commit message includes spaces, like "my commit message", I get the following output:
error: pathspec 'commit' did not match any file(s) known to git.
error: pathspec 'message' did not match any file(s) known to git.
So the only part of the commit message it uses is the "my" and the other parts "commit message" are left out.
I think
git add .
references the location of the shell script, not the current project directory. How do I make it so thatgit add .
references where I currently am in the terminal?
当提交消息包含空格时,例如“我的提交消息”,我得到以下输出:
error: pathspec 'commit' did not match any file(s) known to git.
error: pathspec 'message' did not match any file(s) known to git.
所以它使用的提交消息的唯一部分是“我的”,其他部分“提交消息”被省略了。
我认为
git add .
引用了 shell 脚本的位置,而不是当前项目目录。我如何才能git add .
引用我当前在终端中的位置?
回答by Emil Sit
You must quote the variable in your script.
您必须在脚本中引用该变量。
#!/bin/bash -e
commit_message=""
git add . -A
git commit -m "$commit_message"
git push
I also set "-e" so that if there are any errors, the script will exit without processing subsequent commands.
我还设置了“-e”,以便如果出现任何错误,脚本将退出而不处理后续命令。
As to your second question, the .
in the script should refer to your current working directory, as you intend. However the -A
is causing it to add all files that have been modiied in the repo.
至于您的第二个问题,.
脚本中的 应该指您当前的工作目录,如您所愿。然而,这-A
导致它添加了在 repo 中修改过的所有文件。
回答by madhead
You can create alias with argument. Something like:
[alias]
cap = "!git add . && git commit -m '' && git push origin"
回答by Fabian Rios
with and Alias I couldn`t put variables in the middle of the sentence, but you can create a function and put it on your .bashrc like this
with and Alias 我不能把变量放在句子中间,但是你可以创建一个函数并将它放在你的 .bashrc 中,就像这样
commit(){
git add --all . && git commit -m '' && git push origin master
}
回答by David W.
Been there, done that: Git Flow.
去过那里,做到了:Git Flow。
You can also create aliasesin the git configuration file too. This is much better than writing shell scripts since these will be extensions of the git
command itself.
您也可以在 git 配置文件中创建别名。这比编写 shell 脚本要好得多,因为它们将是git
命令本身的扩展。
Also, don't forget:
另外,不要忘记:
$ git commit --all
which will commit all files you added or edited with your commit.
这将提交您通过提交添加或编辑的所有文件。
回答by bitwiki
My Solution ,FYI
我的解决方案,仅供参考
#!/bin/sh
comment=
git add ./*
git commit -m $comment
echo " commit finished,push to origin master ? "
read commit
case $commit in
y|Y|YES|yes)
git push
;;
n|NO|N|no)
exit 0
esac
usage
用法
./commit.sh your comment message ,type yes if you want to push to master