简体   繁体   English

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

[英]What's a <pathspec> in the git command?

I have updated, modified and removed files in my application and I am now ready to commit.我已经更新、修改和删除了我的应用程序中的文件,现在我可以提交了。 Here is the status:这是状态:

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

no changes added to commit (use "git add" and/or "git commit -a")未向提交添加任何更改(使用“git add”和/或“git commit -a”)

When I enter:当我输入:

git add . 

It gives me a message saying:它给了我一条消息说:

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.

I would like everything I did on my local PC to be committed and then I want the master on GITHUB to reflect this.我希望我在本地 PC 上所做的一切都被提交,然后我希望 GITHUB 上的 master 反映这一点。

Can someone explain what does it mean谁能解释一下是什么意思and what should I now enter so all the changes can be committed with a git commit ?我现在应该输入什么,以便可以使用 git commit 提交所有更改? Sorry it's not clear to me.对不起,我不清楚。 Is the directory or ?是目录还是?

This answer was entirely derived from Git Pathspecs and How to Use Them .这个答案完全来自Git Pathspecs 和 How to Use Them I haven't copied everything over, so look into the link to dig deeper我没有复制所有内容,因此请查看链接以进行更深入的挖掘


The pathspec is the mechanism that git uses for limiting the scope of a git command to a subset of the repository. pathspec是 git 用于将 git 命令的范围限制为存储库子集的机制。 If you have used much git, you have likely used a pathspec whether you know it or not.如果您使用过很多 git,那么无论您是否知道,您都可能使用过pathspec For example, in the command git add README.md , the pathspec is README.md .例如,在命令git add README.mdpathspecREADME.md However, it is capable of much more nuance and flexibility.但是,它具有更多的细微差别和灵活性。

So, why should you learn about pathspec s?那么,为什么要了解pathspec呢? Since it is a part of many commands, these commands become much more powerful with an understanding of pathspec s.由于它是许多命令的一部分,这些命令在理解pathspec变得更加强大。 With git add , you can add just the files within a single directory.使用git add ,您可以只添加单个目录中的文件。 With git diff , you can examine just the changes made to filenames with an extension of .scss .使用git diff ,您可以只检查对扩展名为.scss文件名所做的更改。 You can git grep all files except for those in the /dist directory.您可以 git grep 除/dist目录中的文件之外的所有文件。

File or directory文件或目录

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

if you ever do ls -a , it will list all files and 'directory entries', it will include .如果您执行ls -a ,它将列出所有文件和“目录条目”,它将包括. and .. for more see here..更多见这里

Wildcards通配符

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

top最佳

The top signature tells git to match the pattern from the root of the git repository rather than the current working directory. top签名告诉 git 匹配来自 git 存储库根目录而不是当前工作目录的模式。 You can also use the shorthand :/ rather than :(top) .您也可以使用简写:/而不是:(top)

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

icase凯斯

The icase signature tells git to not care about case when matching eg this could be useful for matching jpg files, which sometimes use the uppercase extension JPG . icase签名告诉 git 在匹配时不关心大小写,例如这对于匹配jpg文件很有用,有时使用大写扩展名JPG

git ls-files ':(icase)*.jpg'

exclude排除

Lastly, there is the “exclude'” magic signature (shorthand of :! or :^ ).最后,还有“排除”魔术签名( :!:^简写)。 eg you can search through all of your .js files while excluding the .spec.js test files.例如,您可以搜索所有.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

From the Git Glossary :来自Git 词汇表

[A pathspec is a pattern] used to limit paths in Git commands. [路径规范是一种模式] 用于限制 Git 命令中的路径。

Pathspecs are used on the command line of "git ls-files", "git ls-tree", "git add", "git grep", "git diff", "git checkout", and many other commands to limit the scope of operations to some subset of the tree or worktree.路径规范用于“git ls-files”、“git ls-tree”、“git add”、“git grep”、“git diff”、“git checkout”等许多命令的命令行来限制范围对树或工作树的某个子集的操作。

As an example, the command git add :/**.ts will recursively add to the index all of the files that end with .ts starting at the root of the repository (respecting the various ways of ignoring files).例如,命令git add :/**.ts将递归地将所有以.ts结尾的文件添加到索引中,从存储库的根目录开始(尊重忽略文件的各种方式)。

If you want the files you deleted to be deleted in the repository as well, do git add --all .如果您希望删除的文件也从存储库中删除,请执行git add --all . . . If you do not want them deleted in the repository, do git add --ignore-removal .如果您不想在存储库中删除它们,请执行git add --ignore-removal . . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM