Git Stash上的Git PathSpec问题

4
当我运行新版本2.13.0.windows.1的新命令stash -p -- {pathspec}时,如下所示: git stash -p -- AB.Dir1/Dir2/DestinationHierarchyCreator.cs 它报告了错误:

error: pathspec 'AB.Dir1/Dir2/DestinationHierarchyCreator.cs' did not match any file(s) known to git.

然而,当我执行git status时,我从中复制文件的位置,它会报告:
Your branch is up-to-date with 'origin/project/develop'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

modified:  AB.Dir1/Dir2/DestinationHierarchyCreator.cs

如果我进入该文件所在的目录并执行 git stash -p -- DestinationHierarchyCreator.cs,它会失败并显示相同的错误。
如果我运行命令 git stash -p -- *.cs,那么我就可以将片段保存到stash中。
所以,是我的 git stash -p选项的理解有误,还是我针对单个文件的路径规范处理有误或其他原因导致的呢?

2个回答

2
刚遇到了同样的问题。看起来 git stash 的路径规范必须相对于仓库根目录(即顶层),而不是当前工作目录。
由于 git status(“与许多其他 Git 命令不同”)会显示“相对于当前目录的路径,如果你在子目录中工作”,这似乎容易引起混淆。
因此,如果你当前的工作目录是 ~/dev/git_project_root/subdir,你需要使用:
git stash -p -- subdir/AB.Dir1/Dir2/DestinationHierarchyCreator.cs

通配符git stash -p -- *.cs有效是因为它匹配了根目录下所有修改过的*.cs文件,而你很可能只有那一个。

我只是想说我也遇到了这个问题(在Linux上)。你知道这是否是一个已知的问题,还是一个有意的“特性”吗? - bow
我猜这可能是有意为之或者是一个疏忽。但是这并不完全清晰。pathspec文档说,“对于每个命令路径是相对于当前目录还是顶层目录,请参阅每个命令的文档。”但git stash push 文档没有说明该命令如何处理 pathspecs。 - medmunds

1

我刚刚在Windows上试过了,它可以在没有'.'的普通文件夹上运行。

C:\Users\vonc\data\git\git>git st
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   Documentation/blame-options.txt

C:\Users\vonc\data\git\git>git stash -- Documentation\blame-options.txt
Saved working directory and index state WIP on master: b14f27f91 Tenth batch for 2.13

即使在bash会话中,并且使用了-p选项,它仍然可以工作。
vonc@bvonc MINGW64 ~/data/git/git (master)
$ git stash -p -- Documentation/blame-options.txt
diff --git a/Documentation/blame-options.txt b/Documentation/blame-options.txt
index dc41957af..96a5b1b4a 100644
--- a/Documentation/blame-options.txt
+++ b/Documentation/blame-options.txt
@@ -1,7 +1,7 @@
 -b::
        Show blank SHA-1 for boundary commits.  This can also
        be controlled via the `blame.blankboundary` config option.
-
+sss
 --root::
        Do not treat root commits as boundaries.  This can also be
        controlled via the `blame.showRoot` config option.
Stash this hunk [y,n,q,a,d,/,e,?]? y

Saved working directory and index state WIP on master: b14f27f91 Tenth batch for 2.13

在一个包含“.”的文件夹中:
vonc@bvonc MINGW64 ~/data/git/git (master)
$ git stash -p -- a.b/c
error: pathspec 'a.b/c' did not match any file(s) known to git.
Did you forget to 'git add'?

所以那可能是一个潜在的问题。


注意,从Git 2.29(2020年第四季度)开始,“显示边界提交的空SHA-1”功能将不再提供:一些面向最终用户的消息已更新为哈希算法无关。
请参见提交4279000(2020年8月13日),作者是Junio C Hamano(gitster
(由Junio C Hamano -- gitster --于2020年8月19日合并至提交2a978f8

messages: 避免在面向最终用户的消息中使用SHA-1

在面向最终用户的消息中,我们仍然有一些提到SHA-1的地方,而实际上我们指的是(十六进制)对象名称。
请重新编写这些消息。

我希望这可以大部分地通过 s/SHA-1/对象名称/ 来完成,但为了保持结果易读性,有些消息需要重新表述。

新的错误消息将会是:

Do not show object names of boundary commits

对我来说,子文件夹的名称中有一个 .,然后是一个子文件夹。AB.Dir1/Dir2/。我将尝试在一个子文件夹上执行此操作。 - ΩmegaMan
@OmegaMan 我同意。我编辑了答案,以说明带有 . 的文件夹可能会有问题。 - VonC
还有其他问题。我尝试从较低的目录中隐藏,没有出现 . 的问题,但仍然收到相同的错误。我将继续调查。 - ΩmegaMan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接