我正在尝试编写一个简单的shell脚本来简化git提交过程.
代替
git add . -A
git commit -m "message"
git push
我想要做 commit.sh "my commit message"
这就是我所拥有的:
#!/bin/bash
commit_message="$1"
git add . -A
git commit -m $commit_message
git push
这有两个问题:
当提交消息包含空格时,比如"我的提交消息",我得到以下输出:
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 .引用我目前在终端的位置?
Emi*_*Sit 17
您必须在脚本中引用该变量.
#!/bin/bash -e
commit_message="$1"
git add . -A
git commit -m "$commit_message"
git push
我还设置了"-e",这样如果有任何错误,脚本将退出而不处理后续命令.
至于你的第二个问题,.脚本应该引用你当前的工作目录.然而,-A它导致它添加已在repo中修改的所有文件.
[alias]
  cap = "!git add . && git commit -m '$1' && git push origin"
with and Alias 我不能把变量放在句子中间,但是你可以创建一个函数并将它放在你的 .bashrc 中,就像这样
commit(){
  git add --all . && git commit -m '$1' && git push origin master
}