如何配置 git 以忽略本地的某些文件?

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

How do I configure git to ignore some files locally?

gitignoregitignore

提问by Bjorn

Can I ignore files locally without polluting the global git config for everyone else? I have untracked files that are spam in my git status but I don't want to commit git config changes for every single little random untracked file I have in my local branches.

我可以在不污染其他人的全局 git 配置的情况下忽略本地文件吗?我的 git 状态中有垃圾邮件的未跟踪文件,但我不想为本地分支中的每个小随机未跟踪文件提交 git 配置更改。

回答by Josh Lee

From the relevant Git documentation:

相关的 Git 文档

Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user's workflow) should go into the $GIT_DIR/info/excludefile.

特定于特定存储库但不需要与其他相关存储库共享的模式(例如,位于存储库内但特定于一个用户工作流的辅助文件)应放入$GIT_DIR/info/exclude文件中。

The .git/info/excludefile has the same format as any .gitignorefile. Another option is to set core.excludesFileto the name of a file containing global patterns.

.git/info/exclude文件具有与任何.gitignore文件相同的格式。另一个选项是设置core.excludesFile为包含全局模式的文件的名称。

Note, if you already have unstaged changes you must run the following after editing your ignore-patterns:

请注意,如果您已经有未暂存的更改,则必须在编辑忽略模式后运行以下命令:

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

Note on $GIT_DIR: This is a notation used all overthe git manual simply to indicate the path to the git repository. If the environment variable is set, then it will override the location of whichever repo you're in, which probably isn't what you want.

注意事项$GIT_DIR:这是一个符号使用遍布git的手册只是为了表明路径git仓库。如果设置了环境变量,那么它将覆盖您所在的任何存储库的位置,这可能不是您想要的。



Edit: Another way is to use:

编辑:另一种方法是使用:

git update-index --skip-worktree <file-list>

Reverse it by:

通过以下方式反转它:

git update-index --no-skip-worktree <file-list>

回答by Florian Sesser

Update: Consider using git update-index --skip-worktree [<file>...]instead, thanks @danShumway! See Borealid's explanation on the difference of the two options.

更新:考虑git update-index --skip-worktree [<file>...]改用,谢谢@danShumway!参见Borealid 对这两个选项的区别的解释



Old answer:

旧答案:

If you need to ignore local changes to tracked files (we have that with local modifications to config files), use git update-index --assume-unchanged [<file>...].

如果您需要忽略对跟踪文件的本地更改(我们有对配置文件的本地修改),请使用git update-index --assume-unchanged [<file>...].

回答by phatmann

Add the following lines to the [alias] section of your .gitconfig file

将以下行添加到 .gitconfig 文件的 [alias] 部分

ignore = update-index --assume-unchanged
unignore = update-index --no-assume-unchanged
ignored = !git ls-files -v | grep "^[[:lower:]]"

Now you can use git ignore my_fileto ignore changes to the local file, and git unignore my_fileto stop ignoring the changes. git ignoredlists the ignored files.

现在您可以使用git ignore my_file忽略对本地文件git unignore my_file的更改,并停止忽略更改。git ignored列出被忽略的文件。

This answer was gleaned from http://gitready.com/intermediate/2009/02/18/temporarily-ignoring-files.html.

这个答案是从http://gitready.com/intermediate/2009/02/18/temporously-ignoring-files.html收集的。

回答by Emil Sit

You have several options:

您有多种选择:

  • Leave a dirty (or uncommitted) .gitignorefile in your working dir (or apply it automatically using topgitor some other such patch tool).
  • Put the excludes in your $GIT_DIR/info/excludefile, if this is specific to one tree.
  • Run git config --global core.excludesfile ~/.gitignoreand add patterns to your ~/.gitignore. This option applies if you want to ignore certain patterns across alltrees. I use this for .pycand .pyofiles, for example.
  • .gitignore在您的工作目录中保留一个脏(或未提交)文件(或使用topgit或其他一些此类补丁工具自动应用它)。
  • $GIT_DIR/info/exclude如果这是特定于一棵树的,请将排除项放在您的文件中。
  • 运行git config --global core.excludesfile ~/.gitignore并将模式添加到您的~/.gitignore. 如果您想忽略所有树中的某些模式,则此选项适用。例如,我将它用于.pyc.pyo文件。

Also, make sure you are using patterns and not explicitly enumerating files, when applicable.

此外,如果适用,请确保您使用的是模式而不是显式枚举文件。

回答by Piotr Korlaga

I think you are looking for:

我认为您正在寻找:

git update-index --skip-worktree FILENAME

which ignore changes made local

忽略本地所做的更改

Here's http://devblog.avdi.org/2011/05/20/keep-local-modifications-in-git-tracked-files/more explanation about these solution!

这是http://devblog.avdi.org/2011/05/20/keep-local-modifications-in-git-tracked-files/关于这些解决方案的更多解释!

to undo use:

撤消使用:

git update-index --no-skip-worktree FILENAME

回答by Birchlabs

You can install some git aliasesto make this process simpler. This edits the [alias]node of your .gitconfigfile.

你可以安装一些git 别名来简化这个过程。这将编辑文件的[alias]节点.gitconfig

git config --global alias.ignore 'update-index --skip-worktree'
git config --global alias.unignore 'update-index --no-skip-worktree'
git config --global alias.ignored '!git ls-files -v | grep "^S"'

The shortcuts this installs for you are as follows:

本次为您安装的快捷方式如下:

  • git ignore config.xml
    • git will pretend that it doesn't see any changes upon config.xml— preventing you from accidentally committing those changes.
  • git unignore config.xml
    • git will resume acknowledging your changes to config.xml— allowing you again to commit those changes.
  • git ignored
    • git will list all the files which you are "ignoring" in the manner described above.
  • git ignore config.xml
    • git 会假装它没有看到任何更改config.xml- 防止您不小心提交这些更改。
  • git unignore config.xml
    • git 将继续确认您的更改config.xml- 允许您再次提交这些更改。
  • git ignored
    • git 将以上述方式列出您“忽略”的所有文件。


I built these by referring to phatmann's answer— which presents an --assume-unchangedversion of the same.

我通过参考phatmann 的回答来构建这些- 它提供--assume-unchanged了相同的版本。

The version I present uses --skip-worktreefor ignoring local changes. See Borealid's answerfor a full explanation of the difference, but essentially --skip-worktree's purpose is for developers to change files without the risk of committing their changes.

我提供的版本--skip-worktree用于忽略本地更改。有关差异的完整解释,请参阅Borealid 的回答,但本质上--skip-worktree的目的是让开发人员更改文件而无需承担更改的风险

The git ignoredcommand presented here uses git ls-files -v, and filters the list to show just those entries beginning with the Stag. The Stag denotes a file whose status is "skip worktree". For a full list of the file statuses shown by git ls-files: see the documentation for the -toption on git ls-files.

git ignored此处显示的命令使用git ls-files -v, 并过滤列表以仅显示以S标记开头的条目。该S标记表示其状态为“跳过worktree”的文件。有关由 显示的文件状态的完整列表git ls-files:请参阅 上的-t选项的文档git ls-files

回答by JESii

You can simply add a .gitignore file to your home directory, i.e. $HOME/.gitignoreor ~/.gitignore. Then tell git to use that file with the command:

您可以简单地将 .gitignore 文件添加到您的主目录,即$HOME/.gitignore或.gitignore 文件~/.gitignore。然后通过以下命令告诉 git 使用该文件:

git config --global core.excludesfile ~/.gitignore

This is a normal .gitignore file which git references when deciding what to ignore. Since it's in your home directory, it applies only to you and doesn't pollute any project .gitignore files.

这是一个普通的 .gitignore 文件,git 在决定忽略什么时引用它。由于它位于您的主目录中,因此它仅适用于您,不会污染任何项目 .gitignore 文件。

I've been using this approach for years with great results.

我多年来一直在使用这种方法并取得了很好的效果。

回答by aak318

In order to ignore untracked files especially if they are located in (a few) folders that are not tracked, a simple solution is to add a .gitignorefile to every untracked folder and enter in a single line containing *followed by a new line. It's a really simple and straightforward solution if the untracked files are in a few folders. For me, all files were coming from a single untracked folder vendorand the above just worked.

为了忽略未跟踪的文件,尤其是当它们位于(几个)未跟踪的文件夹中时,一个简单的解决方案是将.gitignore文件添加到每个未跟踪的文件夹中,并在一行中输入,其中包含*后跟一个新行。如果未跟踪的文件位于几个文件夹中,这是一个非常简单直接的解决方案。对我来说,所有文件都来自一个未跟踪的文件夹vendor,上面的只是有效。

回答by Gavin S. Yancey

If your repo doesn't already have a .gitignore file, then a simple solution is to create a .gitignore file, and in it add .gitignoreto the list of files to be ignored.

如果您的 repo 还没有 .gitignore 文件,那么一个简单的解决方案是创建一个 .gitignore 文件,并在其中添加.gitignore到要忽略的文件列表中。