git1 - git中提示error: pathspec ‘xxx‘x did not match any file(s) known to git

1. 问题概述

最近在git中使用commit,将文件暂存到本地库时,有时候会报下面的错误

$ git commit -v '第一次提交'
error: pathspec '第一次提交' did not match any file(s) known to git

而通过 ‘git status’ 进程状态的查看是没有问题的,可以发现 git add xx 是成功的。

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   test1.c
        new file:   test2.c
        new file:   test3.c
        new file:   test4.c

而我的git版本如下:

$ git version
git version 2.31.1.windows.1

2. 问题解决方法

通过在论坛和百度上进行搜索,都没有得到一个有效的解决方式,但是我出现这个error的原因应该是在于:当前目录下的文件和前一个版本下的文件不同(即删除了一部分上一个版本中的部分文件),此时commit的时候就会报这个错。

通过仔细观察,发现commit命令敲错了,应该是 git commit -m “xxx” ,但是我写成了 git commit -v “xxx”,因此导致出现问题。

因此,重要的事情说三遍,git + 命令的时候一定要主要注意,一定要注意,一定要注意,不要写错

2.1 通过git commit -a -m 'xxx’来解决多个文件的修改、删除

而其中的一个解决方法就是在commit命令后面就 -a -m,来强制进行文件的补齐(告诉命令自动暂存已修改和删除的文件),直接结果如下:

$ git commit -a -m '第一次提交'
[master (root-commit) e0bcad4] 第一次提交
 4 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test1.c
 create mode 100644 test2.c
 create mode 100644 test3.c
 create mode 100644 test4.c
git commit -a -m 中的命令(这里重点接收-a, -m)
-a (-all): Tell the command to automatically stage files 
		   that have been modified and deleted, but new 
		   files you have not told Git about are not affected.
		   (告诉命令自动暂存已修改和删除的文件,但未告知Git的新文件不受影响)

-m <msg> (--message=<msg>): 
	Use the given <msg> as the commit message. 
	If multiple -m options are given, their values 
	are concatenated as separate paragraphs.
	The -m option is mutually exclusive with -c, -C, and -F.
	(使用给定的<msg>作为提交消息。如果给定了多个-m选项,它们的值将作为单独的段落连接起来。
-m选项与-c、-c和-F互斥。)

2.2 通过不加后缀来解决commit到本地库(git commit (-v))

也可以通过git commit 或者 git commit -v 直接来提交,解决error:pathspec xxx的问题
note:此时使用命令git commit -v 后,会进入一个DOS页面,让填写commit的注释信息(必须填写信息,否则无法成功commit)

$ git commit -v
[master 1da4a26] add test7.c
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test7.c

运行上述命令后的提示界面如下所示:
可以发现提示符写了:an empty message aborts the commit
commit信息


参考文献:

  1. Git: error
  2. Git使用之(pathspec master did not match any file(s) known to git)