git add 除了忽略 .gitignore 文件中的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6612630/
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-10 11:18:10  来源:igfitidea点击:

git add all except ignoring files in .gitignore file

gitinitgitignore

提问by E-rich

I am adding source control to a project that had none. The problem is that there are a lot of files to initially add to git with a .gitignorefile, but I can't figure out how to add all files without including the files matching something in the .gitignorefile.

我正在向一个没有的项目添加源代码控制。问题是最初有很多文件要添加到带有.gitignore文件的git 中,但是我不知道如何添加所有文件而不包括与.gitignore文件中的某些内容匹配的文件。

git add *

The above command will not add any files because it detects files which are ignored by the .gitignore.

上面的命令不会添加任何文件,因为它检测到被.gitignore忽略的文件。

git add -f *

The above command will add all files including the files I wish to ignore.

上面的命令将添加所有文件,包括我希望忽略的文件。

So, how do I add all files while still adhering to the .gitignorefile?

那么,如何在仍然坚持.gitignore文件的同时添加所有文件?

回答by Nic

I think you mean git add .which will add all of the files to the repo that AREN'T specified in the .gitignore- you can see these changes by typing git status

我认为您的意思git add .是将所有文件添加到未指定的存储库中.gitignore- 您可以通过键入来查看这些更改git status

The .in bash usually means this directory and all other directories recursively, so if you do this from the bottom level of your repo, you should add all of the files.

.bash中通常意味着该目录和所有其他目录递归,因此,如果您从回购的最底层做到这一点,你应该添加的所有文件。

My usual git flow is to create the .gitignorefile and add the project files to the repo. I'll test the .gitignorefile by typing git statusafter importing the files - if I see the files that I've added (for example only .php or .html, NOT .mp3 or .mov), then you can git add .to add all, and git commit -m "initial commit"to commit them and you should be set.

我通常的 git 流程是创建.gitignore文件并将项目文件添加到 repo。我测试.gitignore通过键入文件git status导入文件后-如果我看到,我已经增加(例如仅.PHP或.html,而不是.MP3或.mov),那么你可以将文件git add .添加所有,并git commit -m "initial commit"以提交它们,你应该被设置。

回答by Leom Burke

git add .

This will add all paths and ignore matches from .gitignore

这将添加所有路径并忽略来自 .gitignore 的匹配项

回答by wadesworld

Try git add .(which means "add all files in the current directory and below")

尝试git add .(这意味着“添加当前目录及以下目录中的所有文件”)

回答by Brett DeWoody

Another option is to use the Git repo's excludefile. This file works similar to a .gitignorefile but isn't committed to the repo.

另一种选择是使用 Git 存储库的exclude文件。此文件的工作方式类似于.gitignore文件,但不提交到存储库。

From the Github docs:

来自Github 文档

You can use this technique for locally-generated files that you don't expect other users to generate, such as files created by your editor.

您可以将此技术用于您不希望其他用户生成的本地生成文件,例如由您的编辑器创建的文件。

Use your favorite text editor to open the file called .git/info/excludewithin the root of your Git repository. Any rule you add here will not be checked in, and will only ignore files for your local repository.

使用您喜欢的文本编辑器打开在.git/info/exclude您的 Git 存储库的根目录中调用的文件。您在此处添加的任何规则都不会被签入,并且只会忽略本地存储库的文件。

  1. In Terminal, navigate to the location of your Git repository
  2. Using your favorite text editor, open the file .git/info/exclude
  3. Add rules to the excludefile as you would the .gitignorefile
  1. 在终端中,导航到 Git 存储库的位置
  2. 使用您喜欢的文本编辑器打开文件 .git/info/exclude
  3. 添加规则的exclude文件,你会的.gitignore文件