这是 git 对于“git add”的正确行为吗?带有子文件夹?

发布于 2024-10-01 18:10:34 字数 1546 浏览 11 评论 0原文

当使用带有文件模式的“git add”时,它只会递归地添加未跟踪的文件,并忽略已更改的文件,除非它们位于当前文件夹中。

示例:

$ git status
# On branch master
# Changed but not updated:
#    (use "git add <file>..." to update what will be committed)
#    (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:    level1/test1.txt
#       modified:    level1/level2/test1.txt
#
# Untracked files:
#   (use "git add <file>..." to incldude in what will be committed)
# 
#       level1/test2.txt
#       level1/level2/test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add level1\*.txt
$ git status
# On branch master
# Changes to be committed:
#       new file:   level1/level2/test2.txt
#       new file:   level1/test2.txt
#
# Changed but not updated:
#       modified:   level1/level2/test1.txt
#       modified:   level1/test1.txt
#

执行 git add level1\*.txt 后,未跟踪的 (test2.txt) 文件会被添加,但不会添加已修改的 (test1.txt) 文件。我尝试过使用 -u 选项、转义和不转义星号等,但似乎没有什么能够简单地添加与模式匹配的所有文件,无论它们是否被跟踪。

当然,在这个例子中,我可以用 -A 添加所有文件,但请记住,这只是为了这个问题的目的,实际上会有更多文件,我不想将它们全部添加,并且层次结构是几个文件夹更深。我添加跟踪文件的唯一方法是指指导或编写整个模式,除了文件名之外,如下所示: git add level1**.txtgit add level1/ level2/*.txt

在 git add 文档中: 这里 它说文件模式应该递归地工作,并且没有说明任何有关跟踪或未跟踪文件的信息,它甚至给出了示例。

我正在使用 msysgit,但我已经在 Windows 和 Linux 上对此进行了测试,以防万一。

我只是想知道。我是否正确解释了文档(因为我认为根据文档它应该有效)或者这就是它应该如何工作?这对我来说没有意义。

When using "git add" with a file pattern it only adds recursively the untracked files, and ignores the changed ones unless they are in the current folder.

Example:

$ git status
# On branch master
# Changed but not updated:
#    (use "git add <file>..." to update what will be committed)
#    (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:    level1/test1.txt
#       modified:    level1/level2/test1.txt
#
# Untracked files:
#   (use "git add <file>..." to incldude in what will be committed)
# 
#       level1/test2.txt
#       level1/level2/test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add level1\*.txt
$ git status
# On branch master
# Changes to be committed:
#       new file:   level1/level2/test2.txt
#       new file:   level1/test2.txt
#
# Changed but not updated:
#       modified:   level1/level2/test1.txt
#       modified:   level1/test1.txt
#

After I execute git add level1\*.txt, the untracked (test2.txt) files are added, but not the modified (test1.txt) files. I've tried with the -u option, escaping and not escaping the asterisk, etc. but nothing seems to simply be able to add ALL files matching a pattern whether they are tracked or not.

Of course in this example I could just add all the files with -A, but keep in mind this is just for the purpose of this question, in reality there will be more files and I would not want to add them all, and the hierarchy is a few folders deeper. The only way for me to add the tracked files is referring to the directing or writing the whole pattern except for the file name like this: git add level1**.txt OR git add level1/level2/*.txt.

In the git add documentation: here it says that the file pattern is supposed to work recursively and doesn't say anything about tracked or untracked files and it even gives and example.

I'm using msysgit but I've tested this on Windows and Linux just in case.

I just want to know. Am I interpreting the documentation correctly (because I think based on the docs it should work) or is this how it is supposed to work? It just doesn't make sense to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌若浮生 2024-10-08 18:10:34

好吧,这个问题或许也有答案了。

不,这似乎根本不是正确的行为。 git add 应该等同于 git add file1 file2...,其中这些是与模式匹配的文件 - 但这不是这里发生的情况。

All right, there might as well be an answer on this question.

No, that doesn't seem to be the right behavior at all. git add <filepattern> should be equivalent to git add file1 file2..., where those are the files matched by the pattern - and that's not what happens here.

水晶透心 2024-10-08 18:10:34

这是因为您使用

git add level1\*.txt

注释反斜杠 \。这等于

git add 'level1*.txt'

要获得预期的行为,请使用斜杠 /

如果您只想添加所有文件,请使用 git add -A 。

This is because you use

git add level1\*.txt

Notes the back-slash \. This is equal to

git add 'level1*.txt'

To get the expected behavior, use slash /.

If you just want to add all files, use git add -A .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文