git add * (星号) vs git add 。(时期)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26042390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:32:08  来源:igfitidea点击:

git add * (asterisk) vs git add . (period)

gitversion-controlgit-add

提问by Tyler Youngblood

I'm new to git and 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 中添加文件的问题。我发现约之间的差异多个计算器的问题git add .git add -agit add --allgit add -A等,但我一直无法找到解释了什么地方git add *呢。我什至看过git add man page,但它没有帮助。我一直在用它代替,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 add *一样吗?一个是否仅从当前目录添加更改的文件,而另一个从当前目录和子目录(递归)添加文件?

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

在其他堆栈问题之一上列出了一个很好的图表,显示了git add -Agit 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 *.htmlwould add all files that have a .htmlextension (but ignore .css, .js, etc).

注意:我理解使用星号作为通配符(添加具有给定扩展名的所有文件)意味着什么。例如,git add *.html想补充一点,有一个所有文件.html的扩展名(但忽略.css.js等)。

Thanks for the help!

谢谢您的帮助!

回答by Denis

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.

add *表示添加当前目录中的所有文件,但名称以点开头的文件除外。这是你的 shell 功能,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.

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

回答by Mureinik

*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 的一部分 - 它是由 shell 解释的通配符。*扩展到当前目录中的所有文件,然后才传递给 git,这就是add所有文件。 .是当前目录本身,git add它会添加它和它下的所有文件。

回答by codingdave

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

.在 shell 中使用点通常表示“当前目录”。

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

当您*在 shell 上使用星号时,将使用一个名为的功能file-globbing。例如,在 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.

描述

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.

通配符匹配

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

流口水

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.

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

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

Denis清楚地描述了'git add .'和之间的含义差异:'git add *'

git addexpects 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期望添加文件列表。在上面的示例中,shell分别展开*.并将结果作为参数提供给 git add。现在的区别在于,git add .git 将扩展到当前目录,而git add *触发文件通配,并扩展到所有不以点开头的文件和目录。

回答by simhumileco

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 -Aand git add -ucommands 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 -Agit 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 add .orgit add *命令,则它们的操作仅在该子目录中使用,而不是在整个工作目录中使用。

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

注3.git add -Agit add -u命令可以通过添加一个路径或掩模文件,例如,进一步细化git add -A app/controllersgit add -u app\styles\*

回答by Steffen

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

  • 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 -u(--update) Stages only Modified Files, removes files that have been deleted from disk, does not add new

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

  • git add -A(--all) 添加所有内容,以便磁盘上文件夹中的所有内容都显示在暂存区域中

  • git add .暂存所有内容,但不会删除已从磁盘中删除的文件

  • git add *暂存所有内容,但不包含以点开头的文件,并且不会删除已从磁盘中删除的文件

  • git add -u(--update) 仅暂存修改过的文件,删除已从磁盘中删除的文件,不添加新文件

  • git add <file name 1> <file name 2>仅添加某些文件