将忽略的文件排除在 git status 之外

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

Keep ignored files out of git status

gitgitignore

提问by knuton

I would like to stop Git from showing ignored files in git status, because having tons of documentation and config files in the list of Changed but not updatedfiles, renders the list half-useless.

我想阻止 Git 在 中显示被忽略的文件git status,因为在已更改但未更新的文件列表中有大量文档和配置文件,使列表变得半无用处。

Is it normal for Git to show these files?

Git 显示这些文件是正常的吗?

I put the ignore information in a .gitignorefile in the root directory of the Git repository and they are not added when using git add .but it seems that they are not fully ignored either, as they show up in the aforementioned list and do notshow up in the list printed by git ls-files --others -i --exclude-standard. Only files matched by the patterns in ~/.gitignoreshow up there.

我将忽略信息放在.gitignoreGit 存储库根目录下的一个文件中,使用时没有添加它们,git add .但似乎也没有完全忽略它们,因为它们出现在上述列表中而未出现在列表中由git ls-files --others -i --exclude-standard. 只有与模式匹配的文件~/.gitignore才会显示在那里。

Could it be because at an earlier stage I didn't ignore them and they were thus committed at least once?

难道是因为在较早的阶段我没有忽略它们,因此它们至少被提交过一次?

回答by MBO

As I found in this post, .gitignoreonly works for untracked files. If you added files to repository, you can:

正如我在这篇文章中发现的那样.gitignore仅适用于未跟踪的文件。如果您将文件添加到存储库,您可以:

git update-index --assume-unchanged <file>

or remove them from repository by

或通过以下方式从存储库中删除它们

git rm --cached <file>

Edit

编辑

This articleexplains that too

这篇文章也解释了

回答by newbreedofgeek

I've had this issue come up as well, it's probably because you added your ignored directory/files to .gitignore after they were marked as "to be tracked" by GIT in an initial commit flow.

我也遇到过这个问题,这可能是因为您在初始提交流中被 GIT 标记为“要跟踪”后将您忽略的目录/文件添加到 .gitignore。

So you need to clear the git tracking cache like so:

所以你需要像这样清除 git 跟踪缓存:

git rm --cached -r [folder/file name]

git rm --cached -r [folder/file name]

A more detailed explanation can be read here: http://www.frontendjunkie.com/2014/12/stop-git-from-tracking-changes-to.html

更详细的解释可以在这里阅读:http: //www.frontendjunkie.com/2014/12/stop-git-from-tracking-changes-to.html

The above command also removed the remnants of the folder/files from your remote GIT origin. So your GIT repos become clean.

上面的命令还从远程 GIT 源中删除了文件夹/文件的残余。所以你的 GIT 仓库变得​​干净了。

回答by Ramis

Problem can be that this file is still in the git cache. To solve this problem needs to reset git cache.

问题可能是该文件仍在 git 缓存中。解决这个问题需要重置git缓存。

Reset git cache. This removes everything from the index.

重置 git 缓存。这将从索引中删除所有内容。

git rm -r --cached . 

Add all files again:

再次添加所有文件:

git add . 

Commit:

犯罪:

git commit -m ".gitignore was fixed." 

回答by Usman

This surely works,

这肯定有效,

git rm --cached -r [folder/file name]

git rm --cached -r [folder/file name]

回答by Kursat Turkay

I assume you want not to add tmp directory (Visual Studio Platform)

我假设您不想添加 tmp 目录(Visual Studio 平台)

1- add lines below to your local .gitignore file

1-将下面的行添加到本地 .gitignore 文件中

## ignore tmp
/tmp/
./tmp/

3- backup and delete tmp Folder locally. (Backup somewhere else. eg desktop?)

3- 在本地备份和删除 tmp 文件夹。(备份其他地方。例如桌面?)

4- Commit Changes to Local than Sync to Remote (eg. github).

4- 将更改提交到本地而不是同步到远程(例如 github)。

After these steps your tmp directory wont be uploaded again.

完成这些步骤后,您的 tmp 目录将不会再次上传。

回答by harshul ar

Following steps work for untracked filesonly. This info is applicable for following configuration:

以下步骤仅适用于未跟踪的文件。此信息适用于以下配置:

Platform: linux

Git version: git version 1.8.3.1

Put list of files to be ignored in "exclude" file present at following location.

将要忽略的文件列表放在以下位置的“排除”文件中。

<path till .git directory>/.git/info/exclude

Initial content of "exclude" file

“排除”文件的初始内容

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~

Final content of "exclude" file

“排除”文件的最终内容

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~


*.tar
*.gch

回答by Francislainy Campos

As per the accepted answer, you can assume the file as unchanged, so that whatever you do on your local machine is not commited to your remote branch.

根据接受的答案,您可以假设该文件未更改,这样您在本地机器上所做的任何事情都不会提交到您的远程分支。

Something like git update-index --assume-unchanged src/main/resources/config.properties where src/main.resources/config.properties is a sample for the path to find that file within your project.

类似于git update-index --assume-unchanged src/main/resources/config.properties src/main.resources/config.properties 是在项目中查找该文件的路径示例。

Now, if you want to remove the file completelyfrom your repository, you may want to use remove cachedinstead as of `git rm --cached

现在,如果你想从你的存储库中完全删除文件,你可能需要使用remove cached`git rm --cached

This scenario could be applicable on the following situation:

此场景可能适用于以下情况:

Let's suppose I have a file where I store passwords. I've initially commited that file with wrong details, just so my colleagues have that same file on their machines. However, now I've added my correct details on my local machine. How do I prevent that file from being accidentally committed again with the now correct password details?

假设我有一个存储密码的文件。我最初使用错误的细节提交了该文件,只是为了让我的同事在他们的机器上拥有相同的文件。但是,现在我已经在本地机器上添加了正确的详细信息。如何使用现在正确的密码详细信息防止该文件再次被意外提交?

回答by BastiBen

From man git-lsfiles:

来自man git-lsfiles

-i, --ignored
Show ignored files in the output. Note that this also reverses any exclude list present.

Personally I tend to keep doxygen files in my source tree, so I simply added this to my .gitignore (which is in the topmost directory of my source tree):

就我个人而言,我倾向于将 doxygen 文件保存在我的源代码树中,所以我只是将其添加到我的 .gitignore(位于我的源代码树的最顶层目录中):

docs/*

Hope that helps.

希望有帮助。

回答by Alex

like this

像这样

git --ignored myfolder

And will show status only for myfolder

并且只会显示myfolder 的状态