gpt4 book ai didi

git add * (asterisk) vs git add . (period)(Git Add*(星号)与Git Add。(句号))

转载 作者:bug小助手 更新时间:2023-10-24 23:15:35 27 4
gpt4 key购买 nike



I have a question about adding files in git. I have found multiple stackoverflow questions about the difference between git add . and git add -a, git add --all, git add -A, etc. But I've been unable to find a place that explains what git add * does. I've even looked at the git add man page, but it didn't help. I've been using it in place of git add . and my co-worker asked me why. I didn't have an answer. I've just always used git add *.

我有一个关于在GIT中添加文件的问题。我发现了关于Git Add之间的区别的多个堆栈溢出问题。以及Git Add-a、Git Add--All、Git Add-A等等。但我一直找不到一个地方来解释git Add*的作用。我甚至看过Git Add手册页,但它没有帮助。我一直在用它来代替Git Add。我的同事问我为什么。我没有答案。我只是一直使用Git Add*。


Are git add . and git add * the same? Does one add changed files from the current directory only, while the other adds files from the current directory and subdirectories (recursively)?

都是Git Add。和Git加*一样吗?一个只从当前目录添加更改的文件,而另一个从当前目录和子目录(递归)添加文件?


There's a great chart listed on one of the other stack questions that shows the difference between git add -A git add . and git add -u, but it doesn't have git add *.

在其他堆栈问题之一上列出了一个很好的图表,显示了Git Add-A Git Add之间的区别。和Git Add-u,但它没有Git Add*。


enter image description here


Note: I understand what it means to use the asterisk as a wildcard (add all files with a given extension). For example, git add *.html would add all files that have a .html extension (but ignore .css, .js, etc).

注意:我理解使用星号作为通配符的含义(添加具有给定扩展名的所有文件)。例如,git add*.html将添加所有扩展名为.html的文件(但忽略.css、.js等)。


Thanks for the help!

谢谢你的帮助!


更多回答

Where's that chart from? I just tried git add . again, and it staged a deleted file no problem, unlike the X in that row would suggest.

这张图表是从哪里来的?我刚试过Git Add。再一次,它上演了一个被删除的文件没有问题,不像那一行中的X会建议。

@David That image is from this answer and applies to older versions of git.

@david这张图片来自这个答案,适用于旧版本的git。

Picture outdated! Git 2.x is different: i.stack.imgur.com/KwOLu.jpg

图片过时了!Git 2.x则不同:i.stack.imgur.com/KuOLu.jpg

Note that if you use DOS-style CMD.EXE, you don't have a fancy shell that expands * for you, so this passes the literal * to Git. The result is slightly different than if you are using bash or some other Unix-like shell that does expand * for you.

请注意,如果您使用的是DOS样式的cmd.exe,您就没有为您扩展*的漂亮的外壳,所以这会将文字*传递给Git。结果与使用bash或其他类似Unix的外壳程序略有不同,后者可以为您展开*。

优秀答案推荐

add * means add all files in the current directory, except for files whose name begin with a dot. This is your shell functionality and Git only ever receives a list of files.

添加*表示添加当前目录中的所有文件,但名称以点开头的文件除外。这是您的外壳功能,Git只接收文件列表。



add . has no special meaning in your shell, and thus Git adds the entire directory recursively, which is almost the same, but including files whose names begin with a dot.

加法。在您的外壳中没有特殊的含义,因此Git递归地添加整个目录,这几乎是相同的,但包括名称以点开头的文件。



* is not part of git - it's a wildcard interpreted by the shell. * expands to all the files in the current directory, and is only then passed to git, which adds them all.
. is the current directory itself, and git adding it will add it and the all the files under it.

*不是git的一部分-它是由外壳解释的通配符。*展开到当前目录中的所有文件,然后才传递给Git,Git会添加所有这些文件。。是当前目录本身,Git添加它将添加它及其下的所有文件。




  • git add -A (--all) Adds everything, so that everything in your folder on disk is represented in the staging area

    Git Add-A(--all)添加所有内容,以便磁盘上文件夹中的所有内容都显示在临时区域中


  • git add . Stages everything, but does not remove files that have been deleted from the disk

    吉特补充道。暂存所有内容,但不删除已从磁盘中删除的文件


  • git add * Stages everything, but not files that begin with a dot & does not remove files that have been deleted from the disk

    Git Add*暂存所有内容,但不会转移以点开头的文件,并且不会删除已从磁盘中删除的文件


  • git add -u (--update) Stages only Modified Files, removes files that have been deleted from disk, does not add new

    Git add-u(--update)仅转移已修改的文件,删除已从磁盘中删除的文件,不添加新文件


  • git add <file name 1> <file name 2> Adds only certain file(s)

    Git Add<文件名1><文件名2>仅添加特定文件(S)




For clarity, I put the answer in the table below:

为清楚起见,我将答案放在下表中:




enter image description here



Additional notes (inspired by the @reka18 comment):

其他注释(灵感来自@reka18评论):



Note 1. git add -A and git add -u commands performed without additional parameters would be additional refinement (subdirectory or mask indication for the file name) work in the range of the entire working directory (also if we execute the command in the working subdirectory of the directory).

注1.在没有附加参数的情况下执行的Git Add-A和Git Add-u命令将在整个工作目录的范围内工作(子目录或文件名掩码指示)(如果我们在目录的工作子目录中执行命令也是如此)。



Note 2. The . and * are respectively the directory path (current directory) and the wildcard, which clarify the path of the command. For example, if the git add . or git add * command is executed in some subdirectory of a working directory, then their action is only used within this subdirectory, not the entire working directory.

注2.和*分别是目录路径(当前目录)和通配符,它们阐明了命令的路径。例如,如果GIT添加了。或者在工作目录的某个子目录中执行GIT ADD*命令,则它们的操作仅在该子目录中使用,而不是整个工作目录。



Note 3. The git add -A and git add -u commands can be further refined by adding a path or mask for files, for example, git add -A app/controllers or git add -u app\styles\*.

注3.可以通过为文件添加路径或掩码来进一步细化git add-A和git add-u命令,例如,git add-A app/Controlders或git add-u app\style\*。



Using the dot . in the shell usually means "the current directory".

使用圆点。在外壳中通常意味着“当前目录”。



When you use the asterisk * on a shell a feature called file-globbing is utilized. E.g. on bash the function glob() is doing just that. The manpage for glob (man 7 glob) states:

当您在一个外壳上使用星号*时,就会利用一个称为文件全局连接的特性。例如,在bash上,函数GLOB()就是这样做的。GLOB(Man 7 GLOB)的手册页上写道:




DESCRIPTION



Long ago, in UNIX V6, there was a program /etc/glob that would expand 
wildcard patterns. Soon afterward this became a shell built-in.
These days there is also a library routine glob(3) that will perform this
function for a user program.


Wildcard matching



A string is a wildcard pattern  if it contains one of the characters '?', '*' or '['. 


Globbing



Globbing is the operation that expands a wildcard pattern 
into the list of pathnames matching the pattern.



That means when you pass arguments to any program on the commandline that contain '?', '*'or '[', first globbing expands the wildcard pattern into a list of files and then gives these files as an argument to the program itself.

这意味着,当您将参数传递给命令行上包含‘?’、‘*’或‘[’的任何程序时,首先,GLOBING会将通配符模式扩展为一个文件列表,然后将这些文件作为参数提供给程序本身。



The difference in meaning between 'git add .' and 'git add *'is clearly described by Denis:

Git Add和Git Add在意思上的区别。丹尼斯清楚地描述了‘git add*’:



git add expects a list of files to be added. In the above example the shell expands * or . respectively and gives the result as a parameter to git add. Now the difference is that with git add . git will expand to the current directory whereas git add * triggers file globbing and such expands to all files and directories that do not start with a dot.

Git Add期望添加一个文件列表。在上面的示例中,外壳展开*或。并将结果作为参数提供给git add.现在不同的是,有了Git Add。Git将扩展到当前目录,而git add*会触发文件全局匹配,这样会扩展到所有不以点开头的文件和目录。



In older versions of git, git add . did not remove deleted files from the index and therefore git add -A was recommended. This is NO LONGER THE CASE in recent versions of git. Now, so long as each commands is executed from the ROOT FOLDER of the working tree, git add . is EXACTLY THE SAME as git add -A. However, if git add . is run from a sub-folder then it ONLY updates the index for files in THAT folder and its subfolders recursively, whereas git add -A would still do ALL files in the working tree.

在旧版本的Git中,Git补充道。没有从索引中删除已删除的文件,因此建议使用Git Add-A。在最近的git版本中,情况不再是这样。现在,只要从工作树的根文件夹git add执行每个命令。与Git Add-A完全相同。然而,如果git加上。从子文件夹运行,则它只递归地更新该文件夹及其子文件夹中的文件的索引,而Git Add-A仍然会更新工作树中的所有文件。


The documentation on this is not particularly clear, but I have validated the above information by experimenting directly with git version 2.42.0.windows.1.

关于这方面的文档不是特别清楚,但我已经通过直接使用git版本2.42.0.windows.1验证了上面的信息。


更多回答

so git add . adds all files, folders, and subfolders, including .gitignore and anything else beginning with a dot, while git add * would add any files, folders, and subfolders, except those beginning with a dot? Is that accurate?

所以Git补充道。添加所有文件、文件夹和子文件夹,包括.gitignore和任何其他以点开头的文件,而git add*将添加除以点开头的文件、文件夹和子文件夹以外的任何文件、文件夹和子文件夹?这是真的吗?

That is indeed correct. Also, git add * would still add files beginning with a dot if they are in a subdirectory.

这确实是正确的。此外,如果文件位于子目录中,Git Add*仍会添加以点开头的文件。

git add . also respects .gitignore, whereas git add * will throw an error if any non-dot-files are gitignored. Much better to use git add . than git add *.

吉特补充道。也尊重.gitignore,而git add*将在任何非点文件被gitignored的情况下抛出错误。使用Git Add要好得多。比git加*。

Worth noting: if invoking Git on DOS/Windows from CMD.EXE, it's Git, not the shell, that expands the *. In this case Git will find dot-files.

值得注意的是:如果从cmd.exe调用DOS/Windows上的Git,那么扩展*的是Git,而不是外壳。在本例中,Git将查找点文件。

@Thor84no: Git will find the dot-files even on a Linux system, if you quote the * to protect it from the shell. It's not a matter of the hidden bit, it's just that Git's compiled-in rules differ.

@Thor84no:Git即使在Linux系统上也会找到点文件,如果您引用*来保护它免受外壳攻击的话。这不是隐藏位的问题,只是Git的编译规则不同。

So would there every be a reason to use the asterisk? Is there any advantage to using it instead of a period? Or vice versa? I'm sure I saw it in a tutorial. I wouldn't have known to use it otherwise. I'm not much of a command line guy (as you've undoubtedly guessed).

那么,有没有理由使用星号呢?用它来代替句号有什么好处吗?还是反之亦然?我确信我在教程中见过它。否则我就不知道要用它了。我不是一个喜欢命令行的人(正如您无疑已经猜到的那样)。

* avoids hidden files (i.e., files that their name begins with a .). In any event, if you aren't adding specific files, I'd just use git add -u (or git add -A if you're creating new files).

*避免隐藏文件(即名称以.开头的文件)。无论如何,如果您不是在添加特定的文件,我将只使用git add-u(如果您正在创建新文件,则使用git Add-A)。

Since you both answered my question I had trouble deciding who to give credit to. I chose Denis below because he has less rep than you. So I figured giving him the green check would benefit him more than it would benefit you. I hope that makes sense? But I really appreciate both explanations. Thanks!

既然你们都回答了我的问题,我很难决定该把功劳归功于谁。我选择了下面的丹尼斯,因为他的代表比你少。所以我想给他一张绿色支票对他的好处大于对你的好处。我希望这是有道理的?但我真的很欣赏这两种解释。谢谢!

for completeness, if you quote * it won't be expanded by the shell, but git will still interpret it and stage all files in current directory, including dot and deleted files.

为了完整性,如果您引用*,它不会被外壳展开,但Git仍然会解释它并将所有文件存放在当前目录中,包括Dot和已删除的文件。

git add without flags does not ignore deleted files (anymore). This behavior changed quite a while ago. Also see here: git-scm.com/docs/git-add#Documentation/…

不带标志的Git添加(不再)不会忽略已删除的文件。这种行为在很长一段时间前就改变了。另请参阅此处:git-scm.com/docs/git-Add#Documentation/…

So as of Git v2.x git add -A and git add . are identical?

因此,从Gitv2.x Git Add-A和Git Add开始。都是一样的吗?

Thank you @reka18, for a very good question. It inspired me to complete my answer... The answer to your question: If you call it in the working directory, no, but if in a subdirectory, then yes (git add -A applies to the entire working directory and git add . always the current directory).

谢谢你@reka18,这是一个非常好的问题。这启发了我完成我的答案。问题的答案是:如果您在工作目录中调用它,则不是,但如果在子目录中调用,则为是(Git Add-A适用于整个工作目录,而Git Add。始终是当前目录)。

This is the best answer for me. The other ones are all a bit fuzzy, though boiling down to the same after all.

这对我来说是最好的答案。其他的都有点模糊,尽管归根结底是一样的。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com