Git:忽略版本控制文件
.gitignore 文件对于忽略一些我们不想控制的文件非常有用。不幸的是,当文件已处于版本控制之下时,它无法使用。例如,我的 .gitignore (已添加到 git 中)文件可能与我的同事想要的不同(例如我想忽略 Vim 文件)。每当我对此文件进行更改时,git 都会将其显示为修改后的文件。所以我的问题是:
- 有没有办法忽略某个已经由 Git 控制的文件的更改?!
- 有没有办法提交这些更改,但只为我自己保留?显然,我不想使用分支,因为我正在某个分支上工作。
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:
- Is there any way to ignore changes for a certain file, which is already controlled by Git?!
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以使用这个命令来得到你想要的。
或者
这只会从 git 中删除轨道,不会删除真正的文件。
然后您可以编辑忽略文件以取消跟踪这些文件或目录。
you can use this command to get you want.
or
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.
我无法真正回答一般性问题(让 Git 忽略跟踪文件)——它让我觉得这是一个弊大于利的功能。
但是,gitignore 手册页指定了几种配置排除文件模式的方法。
特别是,它给出了如何使用这些不同方法的明确说明:
这意味着您的 .gitignore 文件不应该与您同事的文件不同 - 它正在按预期工作。
给你了。在
~/.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:
Meaning that your
.gitignore
file should not be different from your coworkers - it is working as intended.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.我在其他地方写过三种排除文件的方法 。
总之:
列表中较低的项目的优先级高于较高的项目,并且文件中任何模式中的项目前面的
!
都会反转以前的排除。这种范例在 Git 的其他地方也可以看到。例如,如果您使用子模块,要使用的子模块的 url 位于存储库中的
.gitmodules
文件中,但您可以覆盖要在 .git/config 中使用的 url文件。I've written about three ways of excluding files elsewhere.
In summary:
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.对于那些在 SmartGitHG viusal 界面下使用 Git 的人来说,这是一个通用的、普遍的(不是个人的)解决方案:
Here is a generalized, ubiquitos (not individual) solution for those using Git under SmartGitHG viusal interface:
使用 git-update-index 暂时忽略对已在版本下的文件的更改control:
要撤消该使用:
如果您需要将其保留到 git 之后,还可以查看 update-index 的
skip-worktree
和no-skip-worktree
选项-重置Use git-update-index to temporarily ignore changes to files that are already under version control:
To undo that use:
Also have a look at the
skip-worktree
andno-skip-worktree
options for update-index if you need this to persist past a git-reset如果您想排除特定于您的进程的文件(例如 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
.