git命令中的<pathspec>是什么?

29

我已经更新、修改和删除了我的应用程序中的文件,现在准备进行提交。这是目前的状态:

C:\G\ab\WebAdminApp>git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   WebAdminApp.csproj
        modified:   WebAdminApp.csproj.user
        modified:   app/admin/controllers/ContentController.ts
        deleted:    app/admin/interfaces/IEnumService.ts
        modified:   app/admin/interfaces/IHomeController.d.ts
        modified:   lib/pagedown/Markdown.Sanitizer.ts
        deleted:    lib/typings/global.ts
        modified:   package.json
        modified:   ../abilitest-admin.v12.suo

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app/interfaces/IEnumService.d.ts
        app/interfaces/IUtilityService.d.ts
        ../npm-debug.log

没有更改添加到提交中 (使用 "git add" 和/或 "git commit -a")

当我输入:

git add . 

它给我发了一条消息,内容是:

C:\G\ab\WebAdminApp>git add .
warning: You ran 'git add' with neither '-A (--all)' or '--ignore-removal',
whose behaviour will change in Git 2.0 with respect to paths you removed.
Paths like 'WebAdminApp/app/admin/interfaces/IEnumService.ts' that are
removed from your working tree are ignored with this version of Git.

* 'git add --ignore-removal <pathspec>', which is the current default,
  ignores paths you removed from your working tree.

* 'git add --all <pathspec>' will let you also record the removals.

我希望将本地PC上的所有更改提交,然后希望GITHUB上的主版本库反映这些更改。

有人可以解释一下什么是,我现在应该输入什么以便使用git commit提交所有更改吗?抱歉,对我来说不够清楚。是目录还是?


7
是的,我收到了信息。它建议使用“git add --all <pathspec>”。你有读过我的问题吗?我在问什么是<pathspec>。我相信对于熟悉Git的人来说这很清楚,但作为新用户,你认为我们都知道<pathspec>的含义吗? - Samantha J T Star
3
“路径规范(pathspec)”是指在Git中指定路径的方式,包括使用通配符。它们被用于“.gitignore”文件中,也可以在命令行上使用(例如“git add *.c”)。 - jub0bs
@jubobs - 谢谢。那么在我想要包含所有内容的情况下,我应该只是运行 git add -all . 还是 git add -all /? - Samantha J T Star
3个回答

22
这个答案主要是从Git Pathspecs and How to Use Them中得出的。我没有复制所有内容,所以请点击链接深入了解。
pathspec 是 Git 用于将 git 命令的范围限定在仓库的子集上的机制。如果你经常使用 Git,无论你是否知道,你很可能已经使用了 pathspec。例如,在命令 git add README.md 中,pathspecREADME.md。然而,它具有更多的细微差别和灵活性。
那么,为什么你应该了解 pathspec 呢?因为它是许多命令的一部分,通过了解 pathspec,这些命令变得更加强大。使用 git add,你可以仅添加单个目录中的文件。使用 git diff,你可以仅查看文件名扩展名为 .scss 的更改。你可以使用 git grep 查找除了 /dist 目录中的文件之外的所有文件。

文件或目录

git add .      # add CWD (current working directory)
git add ..     # add parent directory and its subdirectories
git add src/   # add src/ directory
git add README # add only README directory

请注意,git add 命令接受 [<pathspec>...] 参数。其中的 ... 表示可能会有多个出现。因此,你可以执行以下操作:
git add /content /images

这将会改变两个目录下的所有内容。

如果你使用ls -a命令,它会列出所有文件和“目录项”,其中包括...,更多信息请参考这里

通配符

注意使用单引号

git log '*.js' # logs all .js files in CWD and subdirectories
git log '.*'   # logs all 'hidden' files and directories in CWD
git log '*/.*' # logs all 'hidden' files and directories in subdirectories

git add '*/*.swift' # stages all 'swift' files

顶部

top 签名告诉 git 从 git 仓库的根目录开始匹配模式,而不是当前工作目录。你也可以使用简写 :/ 代替 :(top)

git ls-files ':(top)*.js'
git ls-files ':/*.js' # shorthand

icase icase签名告诉git在匹配时不区分大小写,这对于匹配jpg文件可能很有用,因为有时候会使用大写扩展名JPG。
git ls-files ':(icase)*.jpg'

排除

最后,还有一个“排除”魔术签名(简写为:!:^)。例如,您可以在搜索所有.js文件时排除.spec.js测试文件。

git grep 'foo' -- '*.js' ':(exclude)*.spec.js' # search .js files excluding .spec.js
git grep 'foo' -- '*.js' ':!*.spec.js' .       # shorthand for the same

pathspec的其他好用案例

查看特定提交中单个文件的更改:

git show 78b0383148bdfeca4a85aa445949cfe1a131c4a6 config.yml

搜索特定文件的提交消息中是否包含某个字符串。有关更多信息,请参见这里
git log -S "plus sign" -- Documentation/git-branch.txt

在特定路径上将文件恢复到其先前的提交。请参见这里这里
git checkout c5f567 -- file1/to/restore file2/to/restore # identify the commit with the SHA1
git checkout origin/main -- src/main/java/HelloWorld.java # identify the commit with a branch


8
Git术语表中得知:
路径规范(pathspec)是用于限制 Git 命令中路径的一种模式。在 "git ls-files"、"git ls-tree"、"git add"、"git grep"、"git diff"、"git checkout" 和许多其他命令的命令行上使用路径规范,以将操作范围限制为树或工作区的某个子集。
例如,命令git add :/**.ts会递归添加以 .ts 结尾的所有文件到索引中,从代码库的根目录开始(遵守各种忽略文件的方式)。

1
在git根目录下使用“:/”非常有帮助,可以无需使用相对路径来检出文件。非常感谢。 - jrwren

1
如果您希望在存储库中删除的文件也被删除,请执行 git add --all .。如果您不想在存储库中删除它们,请执行 git add --ignore-removal .

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