git commit error: pathspec 'xxx' did not match any file(s) known to git
git commit error: pathspec 'xxx' did not match any file(s) known to git
我写了一个 bash 脚本来通过 git 自动更新我的文件,但是如果我的消息包含错误,我会不断收到 git 提交行的错误。
我已经用引号包裹了我的消息,这里有什么问题吗?
我的 bash 脚本:
##Handle local git update
remote='
cd express-demo-nonbare;
git pull origin master;
';
echo "Process for updating Git begin";
git add . ;
read -p "Message for this commit: " comment;
comment=\"${comment}\";
echo $comment;
git commit --message=$comment;
git push backup master;
实际上,您只需要在将 comment
变量提供给 git commit
时引用它即可。您可以替换:
read -p "Message for this commit: " comment;
comment=\"${comment}\";
echo $comment;
git commit --message=$comment;
和
read -p "Message for this commit: " comment;
echo $comment;
git commit --message="$comment";
引号不会成为提交消息的一部分,而是被 bash
使用,以确保 comment
变量的全部内容作为单个 [=16] 的一部分提交=] 参数 git
,即使它包含空白字符。
你不想,正如你所说:
wrap... my message with quote
(这确实是你在做什么)。相反,您希望 保护 您的消息不被 bash 视为 单词列表 。为此,您需要引号,但位置不同:
remote='
cd express-demo-nonbare;
git pull origin master;
'
echo "Process for updating Git begin"
git add .
read -p "Message for this commit: " comment
echo "$comment"
git commit --message="$comment"
git push backup master
我也删除了所有非必要的分号(bash 将一行的结尾视为命令的结尾,除非像未闭合的括号或大括号这样的东西阻止了这一点)。
我不清楚您将变量 remote
设置为文字字符串 newlinec[ 的目的是什么=57=]dspaceex... ;newline,特别是因为 $remote
不会出现在脚本的后面。但是请注意,由于此字符串确实包含 white-space,因此将其扩展到 outside 引号中,如 $remote
(对比 "$remote"
将其扩展引号内)可以触发进一步的 shell 操作。例如:
foo='this; that'
wc $foo
将让 wc
程序尝试打开名为 this;
和 that
的文件,并在白色 space 处将 $foo
拆分为单独的单词然后传递给 wc
。这个拆分实际上是基于$IFS
:
IFS=+
foo='this+that'
wc $foo
尝试打开名为 this
和 that
的文件。将 IFS 恢复到正常设置:
wc $foo
尝试打开一个名为 this+that
.
的文件
(我有时使用 wc
作为一个程序来帮助显示实际参数是什么,因为它试图将每个参数作为文件名打开,并在任何后续错误消息中吐出实际文件名或计数。)
同样,如果变量的扩展产生 shell glob 元字符,这些将在扩展后计算:
foo='*'
wc $foo
将尝试打开并读取当前目录中的每个文件和目录。同样,双引号可以防止这种情况发生:
wc "$foo"
只会尝试打开和读取一个名为 *
.
的文件
我写了一个 bash 脚本来通过 git 自动更新我的文件,但是如果我的消息包含错误,我会不断收到 git 提交行的错误。
我已经用引号包裹了我的消息,这里有什么问题吗?
我的 bash 脚本:
##Handle local git update
remote='
cd express-demo-nonbare;
git pull origin master;
';
echo "Process for updating Git begin";
git add . ;
read -p "Message for this commit: " comment;
comment=\"${comment}\";
echo $comment;
git commit --message=$comment;
git push backup master;
实际上,您只需要在将 comment
变量提供给 git commit
时引用它即可。您可以替换:
read -p "Message for this commit: " comment;
comment=\"${comment}\";
echo $comment;
git commit --message=$comment;
和
read -p "Message for this commit: " comment;
echo $comment;
git commit --message="$comment";
引号不会成为提交消息的一部分,而是被 bash
使用,以确保 comment
变量的全部内容作为单个 [=16] 的一部分提交=] 参数 git
,即使它包含空白字符。
你不想,正如你所说:
wrap... my message with quote
(这确实是你在做什么)。相反,您希望 保护 您的消息不被 bash 视为 单词列表 。为此,您需要引号,但位置不同:
remote='
cd express-demo-nonbare;
git pull origin master;
'
echo "Process for updating Git begin"
git add .
read -p "Message for this commit: " comment
echo "$comment"
git commit --message="$comment"
git push backup master
我也删除了所有非必要的分号(bash 将一行的结尾视为命令的结尾,除非像未闭合的括号或大括号这样的东西阻止了这一点)。
我不清楚您将变量 remote
设置为文字字符串 newlinec[ 的目的是什么=57=]dspaceex... ;newline,特别是因为 $remote
不会出现在脚本的后面。但是请注意,由于此字符串确实包含 white-space,因此将其扩展到 outside 引号中,如 $remote
(对比 "$remote"
将其扩展引号内)可以触发进一步的 shell 操作。例如:
foo='this; that'
wc $foo
将让 wc
程序尝试打开名为 this;
和 that
的文件,并在白色 space 处将 $foo
拆分为单独的单词然后传递给 wc
。这个拆分实际上是基于$IFS
:
IFS=+
foo='this+that'
wc $foo
尝试打开名为 this
和 that
的文件。将 IFS 恢复到正常设置:
wc $foo
尝试打开一个名为 this+that
.
(我有时使用 wc
作为一个程序来帮助显示实际参数是什么,因为它试图将每个参数作为文件名打开,并在任何后续错误消息中吐出实际文件名或计数。)
同样,如果变量的扩展产生 shell glob 元字符,这些将在扩展后计算:
foo='*'
wc $foo
将尝试打开并读取当前目录中的每个文件和目录。同样,双引号可以防止这种情况发生:
wc "$foo"
只会尝试打开和读取一个名为 *
.