git Git忽略特定文件

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

Git ignore particular files

gitversion-controlmsysgit

提问by Headshota

I've been playing around with it for a while and I like it very much. Let me describe my case.

我已经玩了一段时间了,我非常喜欢它。让我描述一下我的情况。

I have two computers where I have my repositories and a remote repository.

我有两台计算机,其中有我的存储库和一个远程存储库。

in my files I have one configuration file which differs on both computers. so when I do pull on my computers I don't need that config file to be pulled, but everything else. How can I achieve that?

在我的文件中,我有一个在两台计算机上都不同的配置文件。所以当我拉我的电脑时,我不需要拉那个配置文件,而是其他所有东西。我怎样才能做到这一点?

I've read about .gitignore files but I can't figure out how they work or are they the thing that I need in my case.

我已经阅读了 .gitignore 文件,但我无法弄清楚它们是如何工作的,或者它们是我需要的东西。

回答by Ivan Zlatev

.gitignore is for files/folders that you haven't checked in already. It's essentially a text file that you create in the repository and has a list of paths relative to the directory it was placed in, which Git will ignore. You can check-in the .gitignore file as part of the repository. you can find examples - at the end of this page

.gitignore 用于您尚未签入的文件/文件夹。它本质上是您在存储库中创建的文本文件,并且具有相对于放置它的目录的路径列表,Git 将忽略这些路径。您可以将 .gitignore 文件签入作为存储库的一部分。您可以找到示例 -在本页末尾

However if your file has already been checked inthe repository then you can use:

但是,如果您的文件已经在存储库中检查过,那么您可以使用:

git update-index --assume-unchanged file

which will tell Git to ignore any changes to that file made in the future. However this is a local configuration so you will have to do it on each machine you check out. You can revert it by doing:

这将告诉 Git 忽略将来对该文件所做的任何更改。然而,这是一个本地配置,因此您必须在您签出的每台机器上进行配置。您可以通过执行以下操作来还原它:

git update-index --no-assume-unchanged file

Depending on what configuration that file contains it might be a good practice to have a skeleton/example config file in the repository - similar to what PHP guys do with config_example.php, which then is used by people to create config.php, which in turn never get's checked in the repository because it is ignored.

根据该文件包含的配置,在存储库中有一个骨架/示例配置文件可能是一个好习惯 - 类似于 PHP 人员对 config_example.php 所做的,然后人们使用它来创建 config.php,其中turn never get 在存储库中被检查,因为它被忽略了。

回答by Michael Krelin - hacker

If you want to ignore file config.localin your repository, you just create a .gitignorefile with /config.localline in it, add it and commit to the repository. That's it.

如果您想忽略config.local存储库中的.gitignore文件,只需创建一个包含/config.local行的文件,将其添加并提交到存储库。就是这样。

回答by Victor Zamanian

The .gitignorefile might be what you need. In that file, you list file patterns -- one per line -- which will cause git statusnot to report on those files.

.gitignore文件可能正是您所需要的。在该文件中,您列出了文件模式——每行一个——这将导致git status不报告这些文件。

But if you have already committed those files, they willbe pulled when you do a git pull.

但是,如果你已经犯这些文件,他们被当你做一把拉住git pull

You can also commit the .gitignorefile to the repository. That way the same files will be ignored on all computers.

您还可以将.gitignore文件提交到存储库。这样,所有计算机上的相同文件都将被忽略。

回答by Derick Schoonbee

Maybe an example will help. Here is a section of my .gitignorefile that is also part of the project files:

也许一个例子会有所帮助。这是我的.gitignore文件的一部分,它也是项目文件的一部分:

# 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]
# *~
*.pro.user.*
tmp/
release/
debug/
bin/
moc_*
ui_*
*.bck
*_debug

#Visual Studio stuff
lib/*.lib
*.ncb
*.suo

This is just a part of the file. You'll need to tune yours depending on your project.

这只是文件的一部分。你需要根据你的项目调整你的。

The point is that the format is flexible and you should not have issues sharing the file. If you have issues, exclude it from the repository.

关键是格式是灵活的,您应该不会在共享文件时遇到问题。如果您有问题,请将其从存储库中排除。

回答by CbIpHuK

Please also note that

另请注意

git update-index --assume-unchanged file

will not help if you try to switch branch

如果您尝试切换分支将无济于事

$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
    file
Please commit your changes or stash them before you switch branches.
Aborting