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

发布于 2024-08-11 17:53:27 字数 91 浏览 13 评论 0原文

我可以在本地忽略文件而不污染其他人的全局 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

终止放荡 2024-08-18 17:53:27

来自相关 Git 文档

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

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

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

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

关于 $GIT_DIR 的注释:这是使用 整个 git 手册只是为了指示 git 存储库的路径。如果设置了环境变量,那么它将覆盖您所在的存储库的位置,这可能不是您想要的。


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

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

通过以下方式反转它:

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

From the relevant Git documentation:

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/exclude file.

The .git/info/exclude file has the same format as any .gitignore file. Another option is to set core.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:

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

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:

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

Reverse it by:

git update-index --no-skip-worktree <file-list>
我不会写诗 2024-08-18 17:53:27

更新:考虑使用 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>...].

无声静候 2024-08-18 17:53:27

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

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

现在您可以使用 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

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_file to ignore changes to the local file, and git 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.

彻夜缠绵 2024-08-18 17:53:27

您有多种选择:

  • 在工作目录中保留脏的(或未提交的).gitignore 文件(或使用 topgit 或其他类似的修补工具)。
  • 如果这是特定于一棵树的,请将排除项放入您的 $GIT_DIR/info/exclude 文件中。
  • 运行 git config --global core.excludesfile ~/.gitignore 并将模式添加到您的 ~/.gitignore 中。如果您想忽略所有树中的某些模式,则适用此选项。例如,我将其用于 .pyc.pyo 文件。

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

You have several options:

  • Leave a dirty (or uncommitted) .gitignore file in your working dir (or apply it automatically using topgit or some other such patch tool).
  • Put the excludes in your $GIT_DIR/info/exclude file, if this is specific to one tree.
  • Run 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.

晚雾 2024-08-18 17:53:27

我认为您正在寻找:

git update-index --skip-worktree FILENAME

忽略本地所做的更改

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

撤消使用:

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

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!

to undo use:

git update-index --no-skip-worktree FILENAME
梦在夏天 2024-08-18 17:53:27

您只需将 .gitignore 文件添加到主目录即可,即 $HOME/.gitignore~/.gitignore。然后告诉 git 使用以下命令使用该文件:

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

这是一个普通的 .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:

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.

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

神也荒唐 2024-08-18 17:53:27

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

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"'

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

  • gitignore config.xml
    • git 会假装它没有看到 config.xml 上的任何更改 - 防止您意外提交这些更改。
  • git unignore config.xml
    • git 将继续确认您对 config.xml 的更改 - 允许您再次提交这些更改。
  • git 被忽略
    • 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.

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.

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 uses git ls-files -v, and filters the list to show just those entries beginning with the S tag. The S tag 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 -t option on git ls-files.

烟沫凡尘 2024-08-18 17:53:27

这是一个简短的单行解决方案,用于排除本地文件。

 echo YOUR_FILE_OR_DIRECTORY >> .git/info/exclude

基于@Vanduc1102 评论。如果它没有应用,然后运行以下命令。

git update-index --assume-unchanged YOUR_FILE_OR_DIRECTORY

This is a brief one-line solution to exclude a local file.

 echo YOUR_FILE_OR_DIRECTORY >> .git/info/exclude

based on @Vanduc1102 comment. if it didn't applied run the following commend after that.

git update-index --assume-unchanged YOUR_FILE_OR_DIRECTORY
乞讨 2024-08-18 17:53:27

--assume-unchanged 和 --skip-worktree 都不是忽略本地文件的正确方法...请检查这个答案以及 git update-index 的文档。由于任何原因经常更改(和/或从克隆更改为另一个)的文件,并且不应提交其更改,那么这些文件不应该首先被跟踪。

但是,有两种在本地忽略文件的正确方法(都适用于未跟踪的文件)。将文件名放入 .git/info/exclude 文件中,该文件是 .gitignore 的本地替代文件,但特定于当前克隆。或者使用全局 .gitignore (应该仅正确用于常见辅助文件,例如 pyz、pycache 等),并且该文件将在您计算机中的任何 git 存储库中被忽略。

要使上述操作自动化(添加到排除或全局 .gitignore),您可以使用以下命令(添加到您的 bash 配置文件):

  • 每个克隆本地排除(请注意,您应该位于
    由于使用相对路径调用命令时的repository),将##FILE-NAME##改为 .git/info/exclude
  • 全局.gitignore,首先使全局.gitignore 这里 然后
    将 ##FILE-NAME## 更改为 ~/.gitignore

Linux

alias git-ignore='echo $1 >> ##FILE-NAME##'
alias git-show-ignored='cat ##FILE-NAME##'
git-unignore(){
  GITFILETOUNIGNORE=${1//\//\\\/}
  sed -i "/$GITFILETOUNIGNORE/d" ##FILE-NAME##
  unset GITFILETOUNIGNORE
}

MacOS (您需要 sed 的 .bak > 就地修改(即,您被迫在就地 sed 添加文件扩展名。即在替换某些内容之前进行备份),因此要删除 .bak 文件,我添加了 rm filename.bak)

alias git-ignore='echo $1 >> ##FILE-NAME##'
alias git-show-ignored='cat ##FILE-NAME##'
git-unignore(){
  GITFILETOUNIGNORE=${1//\//\\\/}
  sed -i.bak "/$GITFILETOUNIGNORE/d" ##FILE-NAME##
  rm ##FILE-NAME##.bak
  unset GITFILETOUNIGNORE
}

然后您可以执行以下操作:

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):

  • Per clone local exclude (Note that you should be in the root of the
    repository when calling the command because of using the relative path), change ##FILE-NAME## to .git/info/exclude
  • Global .gitignore, first make global .gitignore here then
    change ##FILE-NAME## to ~/.gitignore

Linux

alias git-ignore='echo $1 >> ##FILE-NAME##'
alias git-show-ignored='cat ##FILE-NAME##'
git-unignore(){
  GITFILETOUNIGNORE=${1//\//\\\/}
  sed -i "/$GITFILETOUNIGNORE/d" ##FILE-NAME##
  unset GITFILETOUNIGNORE
}

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)

alias git-ignore='echo $1 >> ##FILE-NAME##'
alias git-show-ignored='cat ##FILE-NAME##'
git-unignore(){
  GITFILETOUNIGNORE=${1//\//\\\/}
  sed -i.bak "/$GITFILETOUNIGNORE/d" ##FILE-NAME##
  rm ##FILE-NAME##.bak
  unset GITFILETOUNIGNORE
}

Then you can do:

git-ignore example_file.txt
git-unignore example_file.txt

白日梦 2024-08-18 17:53:27

为了忽略未跟踪的文件,特别是如果它们位于(一些)未跟踪的文件夹中,一个简单的解决方案是向每个未跟踪的文件夹添加一个 .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 folder vendor and the above just worked.

寒冷纷飞旳雪 2024-08-18 17:53:27

只需简单地添加您想要从当前存储库的任何分支上的提交中删除的文件的路径:

  1. 在 Windows 目录设置中取消隐藏隐藏文件
  2. 打开路径 REPO_MAIN_PATH/.git/info/exclude
  3. 添加例如 **/toExcludeFile.bat

就是这样!
对我来说,上面的答案太长了。 KISS - 保持愚蠢简单

Just Simply add path to the file ypu want to remove from commits on any branch of current repo:

  1. Unhide hidden files in Windows Directories Settings
  2. Open Path REPO_MAIN_PATH/.git/info/exclude
  3. Add for example **/toExcludeFile.bat

And thats it !
For me answears above are too long. KISS - Keep it stupid simple

孤独陪着我 2024-08-18 17:53:27

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.
  • You need to remember which of the mechanisms you used.

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.

岁吢 2024-08-18 17:53:27

对于任何想要在子模块中本地忽略文件的人:

使用与 .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.

梦毁影碎の 2024-08-18 17:53:27

如果您想使用 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!

年华零落成诗 2024-08-18 17:53:27

如果您的存储库还没有 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文