Git - 创建一个 .gitignore 文件

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

Git - Creating a .gitignore file

gitgitignore

提问by Funky

I'm looking to create a .gitignorefile so certain files are do not get checked in to the repository. Does anyone have a guide on how and where to place this file? I have tried placing it in my working directory, ran git status and it is still picking up on the files I would like it to ignore.

我正在寻找创建一个.gitignore文件,因此某些文件不会被签入存储库。有没有人有关于如何以及在哪里放置这个文件的指南?我尝试将它放在我的工作目录中,运行 git status 并且它仍然在接收我希望它忽略的文件。

I used this .gitignorefile I have found:

我使用了.gitignore我发现的这个文件:

###################
# compiled source #
###################
*.com
*.class
*.dll
*.exe
*.pdb
*.dll.config
*.cache
*.suo
# Include dlls if they're in the NuGet packages directory
!/packages/*/lib/*.dll
# Include dlls if they're in the CommonReferences directory
!*CommonReferences/*.dll
####################
# VS Upgrade stuff #
####################
UpgradeLog.XML
_UpgradeReport_Files/
###############
# Directories #
###############
bin/
obj/
TestResults/
###################
# Web publish log #
###################
*.Publish.xml
#############
# Resharper #
#############
/_ReSharper.*
*.ReSharper.*
############
# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
######################
# Logs and databases #
######################
*.log
*.sqlite
# OS generated files #
######################
.DS_Store?
ehthumbs.db
Icon?
Thumbs.db

回答by Peter van der Does

Placement of .gitignore depends if the files need to be ignored for just one repo or for all your repos. For one repo, place it in the root of your repo.

.gitignore 的位置取决于文件是否需要仅针对一个存储库或所有存储库被忽略。对于一个 repo,请将其放在 repo 的根目录中。

When you create a .gitignore file in an existing repo, or add files that were already in the repo, you have to make sure to remove the to-be-ignored files from the repo.

当您在现有存储库中创建 .gitignore 文件或添加存储库中已有的文件时,您必须确保从存储库中删除要忽略的文件。

If you have a test.dll in the root, you have to do

如果根目录中有 test.dll,则必须执行

git rm --cached test.dll

Now if you have a lot of files, like you said you can opt for the following option, remove everything from the cache, add it all back (the files in .gitignore will not be added) and commit everything again.

现在,如果您有很多文件,就像您说的那样,您可以选择以下选项,从缓存中删除所有内容,将其全部添加回来(不会添加 .gitignore 中的文件)并再次提交所有内容。

git rm -r --cached .
git add .
git commit -m "Start using .gitignore"

回答by Fredrik Pihl

You have to add .gitignoreto the index before Git sees it. I.e., git add .gitignore.

你必须.gitignore在 Git 看到它之前添加到索引中。即,git add .gitignore

回答by thescientist

The projects that I have worked on, I have the .gitignore file in the root directory of the project, where the .git directory would be. Make sure the file is committed.

我参与过的项目,我在项目的根目录中有 .gitignore 文件,.git 目录就在那里。确保文件已提交。

Note, that if you have already committed the files you are trying to ignore (i.e. Git is tracking them) then they can't be ignored. You would have to untrack them first.

请注意,如果您已经提交了要忽略的文件(即 Git 正在跟踪它们),那么它们就不能被忽略。您必须先取消跟踪它们。

https://help.github.com/articles/ignoring-files

https://help.github.com/articles/ignoring-files

回答by codingdave

From the link @thescientist has posted:

来自@thescientist 发布的链接:

Git lets you ignore those files by assuming they are unchanged. This is done by running the

Git 允许您通过假设它们未更改来忽略这些文件。这是通过运行

git update-index --assume-unchanged path/to/file.txt

git update-index --assume-unchanged path/to/file.txt

command. Once marking a file as such, git will completely ignore any changes on that file; they will not show up when running git status or git diff, nor will they ever be committed.

命令。一旦将文件标记为这样,git 将完全忽略对该文件的任何更改;它们不会在运行 git status 或 git diff 时出现,也不会被提交。