Git:忽略版本控制文件

发布于 2024-10-10 20:18:32 字数 267 浏览 0 评论 0原文

.gitignore 文件对于忽略一些我们不想控制的文件非常有用。不幸的是,当文件已处于版本控制之下时,它无法使用。例如,我的 .gitignore (已添加到 git 中)文件可能与我的同事想要的不同(例如我想忽略 Vim 文件)。每当我对此文件进行更改时,git 都会将其显示为修改后的文件。所以我的问题是:

  1. 有没有办法忽略某个已经由 Git 控制的文件的更改?!
  2. 有没有办法提交这些更改,但只为我自己保留?显然,我不想使用分支,因为我正在某个分支上工作。

The .gitignore file is very useful in ignoring some of the files that we don't want to control. Unfortunately, it cannot be used when the file is already under version control. For example, my .gitignore (which is already added to git) file might be different than what my coworker wants it to be (e.g. I want to ignore Vim files). Whenever I make changes to this file, git shows it as a modified file. So my questions:

  1. Is there any way to ignore changes for a certain file, which is already controlled by Git?!
  2. Is there any way to commit these changes, but keep it for myself only? Obviously, I don't want to use a branch, because I am working on a certain branch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

蘸点软妹酱 2024-10-17 20:18:33

你可以使用这个命令来得到你想要的。

git rm --cached path/to/file

或者

git rm -r --cached path/ignore/dir

这只会从 git 中删除轨道,不会删除真正的文件。

然后您可以编辑忽略文件以取消跟踪这些文件或目录。

you can use this command to get you want.

git rm --cached path/to/file

or

git rm -r --cached path/ignore/dir

This will only remove the track from git, will not delete the real files.

Then you can edit the ignore file to untrack these files or dirs.

九厘米的零° 2024-10-17 20:18:33

我无法真正回答一般性问题(让 Git 忽略跟踪文件)——它让我觉得这是一个弊大于利的功能。

但是,gitignore 手册页指定了几种配置排除文件模式的方法。

特别是,它给出了如何使用这些不同方法的明确说明:

  • 应进行版本控制并通过克隆分发到其他存储库的模式(即所有开发人员都希望忽略的文件)应放入 .gitignore 文件中。

这意味着您的 .gitignore 文件不应该与您同事的文件不同 - 它正在按预期工作。

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

  • 用户希望 git 在所有情况下都忽略的模式(例如,由用户选择的编辑器生成的备份或临时文件)通常会进入由 core 指定的文件中用户的 ~/.gitconfig 中的 .excludesfile

给你了。在 ~/.gitconfig 文件中指定一个 core.excludesfile 文件路径,然后将要排除的模式放入其中。

I cannot really answer the general question (having Git ignore tracked files) - it strikes me as a feature that would be much more detrimental than useful.

However, the gitignore manual page specifies a few ways to configure patterns for excluded files.

In particular, it gives explicit instructions on how to use these various ways:

  • Patterns which should be version-controlled and distributed to other repositories via clone (i.e., files that all developers will want to ignore) should go into a .gitignore file.

Meaning that your .gitignore file should not be different from your coworkers - it is working as intended.

  • 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.

  • Patterns which a user wants git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesfile in the user’s ~/.gitconfig.

There you have it. Specify a core.excludesfile file path into your ~/.gitconfig file, and then put into it the patterns that you want to exclude.

无尽的现实 2024-10-17 20:18:33

我在其他地方写过三种排除文件的方法

总之:

  1. 全局 gitignore 文件适用于该系统上的所有存储库
  2. 存储库中的 .gitignore 文件适用于该存储库以及该存储库的所有克隆。
  3. .git/info/exclude 文件仅适用于该存储库。

列表中较低的项目的优先级高于较高的项目,并且文件中任何模式中的项目前面的 ! 都会反转以前的排除。

这种范例在 Git 的其他地方也可以看到。例如,如果您使用子模块,要使用的子模块的 url 位于存储库中的 .gitmodules 文件中,但您可以覆盖要在 .git/config 中使用的 url文件。

I've written about three ways of excluding files elsewhere.

In summary:

  1. A global git ignore file applies to all repositories on that system
  2. The .gitignore file in the repository applies the the repository and all clones of that repository.
  3. The .git/info/exclude file applies only to that repository.

The lower items in the list have priority over the higher items, and a ! in front of an item in any of the patterns in the file reverses a previous exclusion.

This paradigm is seen elsewhere in Git. For example, if you were using submodules, the url to the submodule to use is in the the .gitmodules file in the repository, but you can over-ride the url to use in the .git/config file.

不必在意 2024-10-17 20:18:33

对于那些在 SmartGitHG viusal 界面下使用 Git 的人来说,这是一个通用的、普遍的(不是个人的)解决方案:

  • 在“存储库”窗口中选择要忽略的文件夹;
  • 在“文件”窗口中选择该文件夹中的所有文件(通过单击主窗口右上角区域中的相应过滤器图标取消隐藏未更改的文件);
  • 单击控制面板上的“删除”(如果需要,请选中“删除本地文件”复选框);
  • 提交本地更改;
  • 右键单击“存储库”窗口中的文件夹;
  • 单击“忽略”。

Here is a generalized, ubiquitos (not individual) solution for those using Git under SmartGitHG viusal interface:

  • choose a folder you want to ignore in "Repositories" window;
  • select all the files from that folder in "Files" window (unhide unchanged files by clicking the respective filter icon in the Right Top region of the main window);
  • click "Remove" on control panel (check the "Delete local files" check box if you need it);
  • commit local changes;
  • rightclick on the folder in "Repositories" window;
  • click "Ignore".
懒的傷心 2024-10-17 20:18:32

使用 git-update-index 暂时忽略对已在版本下的文件的更改control:

git update-index --assume-unchanged <files>

要撤消该使用:

git update-index --no-assume-unchanged <files>

如果您需要将其保留到 git 之后,还可以查看 update-index 的 skip-worktreeno-skip-worktree 选项-重置

Use git-update-index to temporarily ignore changes to files that are already under version control:

git update-index --assume-unchanged <files>

To undo that use:

git update-index --no-assume-unchanged <files>

Also have a look at the skip-worktree and no-skip-worktree options for update-index if you need this to persist past a git-reset

梦明 2024-10-17 20:18:32

如果您想排除特定于您的进程的文件(例如 Vim 临时文件),请编辑(本地)文件 .git/info/exclude 并在其中添加排除模式。该文件是为开发人员特定的排除而设计的,而不是为项目范围的排除而设计的 .gitignore

简而言之,每个人都应该就 .gitignore 添加的内容达成一致。对于您不同意的文件,请使用 .git/info/exclude

If you want to exclude files that are specific to your process (such as Vim temporary files), edit the (local) file .git/info/exclude and add your exclusion patterns there. This file is designed for developer-specific exclusions rather than .gitignore, which is designed for project-wide exclusions.

The short summary is, everybody should agree on what is added to .gitignore. For files where you don't agree, use .git/info/exclude.

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