如何配置 git 以忽略本地的某些文件?
我可以在本地忽略文件而不污染其他人的全局 git 配置吗?我的 git 状态中有未跟踪的文件是垃圾邮件,但我不想为本地分支中的每个随机未跟踪文件提交 git 配置更改。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
来自相关 Git 文档:
.git/info/exclude
文件与任何.gitignore
文件具有相同的格式。另一个选项是将 core.excludesFile 设置为包含全局模式的文件的名称。请注意,如果您已经进行了未暂存的更改,则必须在编辑忽略模式后运行以下命令:
关于
$GIT_DIR
的注释:这是使用 整个 git 手册只是为了指示 git 存储库的路径。如果设置了环境变量,那么它将覆盖您所在的存储库的位置,这可能不是您想要的。编辑:另一种方法是使用:
通过以下方式反转它:
From the relevant Git documentation:
The
.git/info/exclude
file has the same format as any.gitignore
file. Another option is to setcore.excludesFile
to the name of a file containing global patterns.Note, if you already have unstaged changes you must run the following after editing your ignore-patterns:
Note on
$GIT_DIR
: This is a notation used all over the 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.Edit: Another way is to use:
Reverse it by:
更新:考虑使用
git update-index --skip-worktree [...]
代替,谢谢@danShumway!请参阅 Borealid 关于两个选项差异的解释。旧答案:
如果您需要忽略对跟踪文件的本地更改(我们对配置文件进行了本地修改),请使用 git update-index --assume-unchanged [...]。
Update: Consider using
git update-index --skip-worktree [<file>...]
instead, thanks @danShumway! See Borealid's explanation on the difference of the two options.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>...]
.将以下行添加到 .gitconfig 文件的 [alias] 部分
现在您可以使用 gitignore my_file 来忽略对本地文件的更改,并使用 git unignore my_file 来停止忽略这些变化。
gitignoreed
列出了被忽略的文件。这个答案是从 http://gitready.com/intermediate/2009/02/18 收集的/temporarily-ignoring-files.html。
Add the following lines to the [alias] section of your .gitconfig file
Now you can use
git ignore my_file
to ignore changes to the local file, andgit unignore my_file
to stop ignoring the changes.git ignored
lists the ignored files.This answer was gleaned from http://gitready.com/intermediate/2009/02/18/temporarily-ignoring-files.html.
您有多种选择:
.gitignore
文件(或使用 topgit 或其他类似的修补工具)。$GIT_DIR/info/exclude
文件中。~/.gitignore
中。如果您想忽略所有树中的某些模式,则适用此选项。例如,我将其用于.pyc
和.pyo
文件。另外,请确保您使用模式而不是显式枚举文件(如果适用)。
You have several options:
.gitignore
file in your working dir (or apply it automatically using topgit or some other such patch tool).$GIT_DIR/info/exclude
file, if this is specific to one tree.git config --global core.excludesfile ~/.gitignore
and add patterns to your~/.gitignore
. This option applies if you want to ignore certain patterns across all trees. I use this for.pyc
and.pyo
files, for example.Also, make sure you are using patterns and not explicitly enumerating files, when applicable.
我认为您正在寻找:
忽略本地所做的更改
这是 http://devblog.avdi.org/2011/05/20/keep-local-modifications-in-git-tracked-files/ 有关这些解决方案的更多说明!
撤消使用:
I think you are looking for:
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!
to undo use:
您只需将 .gitignore 文件添加到主目录即可,即
$HOME/.gitignore
或~/.gitignore
。然后告诉 git 使用以下命令使用该文件:这是一个普通的 .gitignore 文件,git 在决定忽略什么时会引用该文件。由于它位于您的主目录中,因此它仅适用于您并且不会污染任何项目 .gitignore 文件。
我多年来一直使用这种方法,取得了很好的效果。
You can simply add a .gitignore file to your home directory, i.e.
$HOME/.gitignore
or~/.gitignore
. Then tell git to use that file with the command: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.
I've been using this approach for years with great results.
您可以安装一些 git 别名 使这个过程更简单。这将编辑
.gitconfig
文件的[alias]
节点。为您安装的快捷方式如下:
gitignore config.xml
config.xml
上的任何更改 - 防止您意外提交这些更改。git unignore config.xml
config.xml
的更改 - 允许您再次提交这些更改。git 被忽略
我通过参考 phatmann 的答案 构建了这些 - 它提供了
--assume-unchanged
版本相同。我提供的版本使用
--skip-worktree
来忽略本地更改。有关差异的完整解释,请参阅 Borealid 的回答,但本质上是--skip-worktree
'其目的是让开发人员可以更改文件,而无需承担提交更改的风险。此处介绍的 gitignoreed 命令使用 git ls-files -v,并过滤列表以仅显示以
S
标记开头的条目。S
标记表示状态为“跳过工作树”的文件。有关 git ls-files 显示的文件状态的完整列表:请参阅git ls-files
。You can install some git aliases to make this process simpler. This edits the
[alias]
node of your.gitconfig
file.The shortcuts this installs for you are as follows:
git ignore config.xml
config.xml
— preventing you from accidentally committing those changes.git unignore config.xml
config.xml
— allowing you again to commit those changes.git ignored
I built these by referring to phatmann's answer — which presents an
--assume-unchanged
version of the same.The version I present uses
--skip-worktree
for ignoring local changes. See Borealid's answer for 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.The
git ignored
command presented here usesgit ls-files -v
, and filters the list to show just those entries beginning with theS
tag. TheS
tag denotes a file whose status is "skip worktree". For a full list of the file statuses shown bygit ls-files
: see the documentation for the-t
option ongit ls-files
.这是一个简短的单行解决方案,用于排除本地文件。
基于@Vanduc1102 评论。如果它没有应用,然后运行以下命令。
This is a brief one-line solution to exclude a local file.
based on @Vanduc1102 comment. if it didn't applied run the following commend after that.
--assume-unchanged 和 --skip-worktree 都不是忽略本地文件的正确方法...请检查这个答案以及 git update-index 的文档。由于任何原因经常更改(和/或从克隆更改为另一个)的文件,并且不应提交其更改,那么这些文件不应该首先被跟踪。
但是,有两种在本地忽略文件的正确方法(都适用于未跟踪的文件)。将文件名放入 .git/info/exclude 文件中,该文件是 .gitignore 的本地替代文件,但特定于当前克隆。或者使用全局 .gitignore (应该仅正确用于常见辅助文件,例如 pyz、pycache 等),并且该文件将在您计算机中的任何 git 存储库中被忽略。
要使上述操作自动化(添加到排除或全局 .gitignore),您可以使用以下命令(添加到您的 bash 配置文件):
由于使用相对路径调用命令时的repository),将##FILE-NAME##改为
.git/info/exclude
将 ##FILE-NAME## 更改为
~/.gitignore
Linux
MacOS (您需要
sed
的 .bak > 就地修改(即,您被迫在就地 sed 添加文件扩展名。即在替换某些内容之前进行备份),因此要删除 .bak 文件,我添加了 rm filename.bak)然后您可以执行以下操作:
git-忽略 example_file.txt
git-unignore example_file.txt
Both --assume-unchanged and --skip-worktree are NOT A CORRECT WAY to ignore files locally... Kindly check this answer and the notes in the documentation of git update-index. Files that for any reason keep changing frequently (and/or change from a clone to another) and their changes should not be committed, then these files SHOULD NOT be tracked in the first place.
However, the are two proper ways to ignore files locally (both work with untracked files). Either to put files names in .git/info/exclude file which is the local alternative of .gitignore but specific to the current clone. Or to use a global .gitignore (which should be properly used only for common auxiliary files e.g. pyz, pycache, etc) and the file will be ignored in any git repo in your machine.
To make the above as kind of automated (adding to exclude or global .gitignore), you can use the following commands (add to your bash-profile):
repository when calling the command because of using the relative path), change ##FILE-NAME## to
.git/info/exclude
change ##FILE-NAME## to
~/.gitignore
Linux
MacOS (you need the .bak for
sed
inplace modifications (i.e. you are forced to add a file extension to inplace sed. i.e. make a backup before replacing something), therefore to delete the .bak file I added rm filename.bak)Then you can do:
git-ignore example_file.txt
git-unignore example_file.txt
为了忽略未跟踪的文件,特别是如果它们位于(一些)未跟踪的文件夹中,一个简单的解决方案是向每个未跟踪的文件夹添加一个
.gitignore
文件,并在一行中输入包含*
后跟一个新行。如果未跟踪的文件位于几个文件夹中,那么这是一个非常简单直接的解决方案。对我来说,所有文件都来自一个未跟踪的文件夹vendor
,上面的内容就可以了。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
.gitignore
file 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 foldervendor
and the above just worked.只需简单地添加您想要从当前存储库的任何分支上的提交中删除的文件的路径:
就是这样!
对我来说,上面的答案太长了。 KISS - 保持愚蠢简单
Just Simply add path to the file ypu want to remove from commits on any branch of current repo:
And thats it !
For me answears above are too long. KISS - Keep it stupid simple
TL;DR - 过滤输出以忽略文件
不要这样做。关于实现这一点的技术方法有很好的答案。但是,我已经多次找到此 Q/A 页面。正如您所看到的,有多种解决方案。问题是每一个都会让 git 忽略该文件;这可能是你现在想要的,但也许不是将来的。
细节中的魔鬼是,
git
将不再报告这些文件。将其添加到 .gitignore 或将过滤器写入
git status
来主动过滤掉文本。每个人都会忽略该文件,但为什么它被忽略的答案更明显。包装脚本(或命令行调用)的第二种情况使您选择忽略的内容以及您的工作流程是非标准的变得非常明显。TL;DR - filter the output to ignore the files
Don't do it. There are excellent answers on the technical ways to do this. However, I have found this Q/A page several times. As you can see there are multiple solutions. The problem is that each will make git ignore the file; which might be what you want now, but maybe not in the future.
The devil in the details are,
git
will no longer report the files.Either add it to .gitignore or write a filter to
git status
that will actively filter out the text. Each will ignore the file, but the answer of why it is being ignored is more obvious. The 2nd case of a wrapper script (or command line recall) makes it abundantly apparent what you are choosing to ignore and that your workflow is non-standard.对于任何想要在子模块中本地忽略文件的人:
使用与
.gitignore< 相同的格式编辑
my-parent-repo/.git/modules/my-submodule/info/exclude
/代码> 文件。For anyone who wants to ignore files locally in a submodule:
Edit
my-parent-repo/.git/modules/my-submodule/info/exclude
with the same format as a.gitignore
file.如果您想使用
skip-worktree
忽略目录中的所有文件而不仅仅是一个文件,请使用 -->;
git update-index --skip-worktree path/to/directory/*
但不是
git update-index --skip-worktree path/to/directory
也不是
git update-index --skip-worktree path/to/directory/
但我无法对嵌套目录执行此操作!
If you want to ignore all files in a directory not only one file using
skip-worktree
Use -->
git update-index --skip-worktree path/to/directory/*
But not
git update-index --skip-worktree path/to/directory
Nor
git update-index --skip-worktree path/to/directory/
I couldn't do it for nested directories though!
如果您的存储库还没有 .gitignore 文件,那么一个简单的解决方案是创建一个 .gitignore 文件,并在其中将
.gitignore
添加到要忽略的文件列表中。If your repo doesn't already have a .gitignore file, then a simple solution is to create a .gitignore file, and in it add
.gitignore
to the list of files to be ignored.