【问题标题】:Git PathSpec Issue on Git StashGit Stash 上的 Git PathSpec 问题
【发布时间】:2017-05-11 15:49:32
【问题描述】:

当我运行其新命令 stash -p -- {pathspec} 的新版本 2.13.0.windows.1 时,

git stash -p -- AB.Dir1/Dir2/DestinationHierarchyCreator.cs

报告错误

错误:pathspec 'AB.Dir1/Dir2/DestinationHierarchyCreator.cs' 与 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,那么我可以将片段保存到存储中。


那么我对git stash -p 选项的理解是错误的,还是我对单个文件或其他文件的路径规范的处理不正确?

【问题讨论】:

标签: git git-stash


【解决方案1】:

刚刚遇到了同样的问题。看来git stash pathspecs 必须相对于存储库根目录(顶部)——而不是你当前的工作目录。

由于git status(“与许多其他 Git 命令不同”)显示路径“relative to the current directory,如果您在子目录中工作”,这似乎容易混淆。

因此,如果您当前的工作目录是 ~/dev/git_project_root/subdir,您需要使用:

git stash -p -- subdir/AB.Dir1/Dir2/DestinationHierarchyCreator.cs

(通配符 git stash -p -- *.cs 有效,因为它匹配您根目录下所有修改过的 *.cs 文件——可能您只有那个。)

【讨论】:

  • 只是插话说我刚刚遇到这个(在 Linux 上)。您知道这是一个已知问题还是故意的“功能”?
  • 我猜这是故意的或疏忽。但这并不完全清楚。 pathspec docs 说,“请参阅每个命令的文档以了解路径是相对于当前目录还是顶层。”但是git stash push docs 没有说明该命令如何处理路径规范。
【解决方案2】:

我刚刚在 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 年第四季度)中,不再有“Show blank SHA-1 for boundary commits”:一些面向最终用户的消息已更新为与哈希算法无关。

参见Junio C Hamano (gitster)commit 4279000(2020 年 8 月 13 日)。
(由 Junio C Hamano -- gitster -- 合并于 commit 2a978f8,2020 年 8 月 19 日)

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

当我们指的是面向最终用户的消息中的(十六进制)对象名称时,仍有少数提到 SHA-1。
重写它们。

我希望这主要是 s/SHA-1/object name/,但有些消息需要重新措辞以保持结果可读。

那么新的错误信息将是:

Do not show object names of boundary commits

【讨论】:

  • 对我来说,子文件夹的名称中有一个.,然后是一个子文件夹。 AB.Dir1/Dir2/。我将尝试在子文件夹中执行此操作。
  • @OmegaMan 我同意。我编辑了答案以说明其中包含 . 的文件夹可能会出现问题。
  • 其他事情正在发生。我试图从没有. 问题的较低目录中存储,但仍然收到相同的错误。我会继续调查。
猜你喜欢
  • 2011-04-26
  • 2017-11-25
  • 2013-02-23
  • 2012-09-10
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多