Git:忽略受版本控制的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4633681/
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
Git: Ignoring Version-Controlled Files
提问by Rafid
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:
.gitignore 文件对于忽略一些我们不想控制的文件非常有用。不幸的是,当文件已经在版本控制之下时,它不能使用。例如,我的 .gitignore(已经添加到 git)文件可能与我的同事想要的不同(例如我想忽略 Vim 文件)。每当我对此文件进行更改时,git 都会将其显示为修改后的文件。所以我的问题:
- 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.
- 有没有办法忽略某个文件的更改,它已经被 Git 控制了?!
- 有没有办法提交这些更改,但只保留给我自己?显然,我不想使用分支,因为我正在某个分支上工作。
采纳答案by Greg Hewgill
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.
如果您想排除特定于您的进程的文件(例如 Vim 临时文件),请编辑(本地)文件.git/info/exclude
并在那里添加您的排除模式。此文件专为特定于开发人员的排除而设计,而不是.gitignore
专为项目范围的排除而设计的。
The short summary is, everybody should agree on what is added to .gitignore
. For files where you don't agree, use .git/info/exclude
.
简短的总结是,每个人都应该就添加到.gitignore
. 对于您不同意的文件,请使用.git/info/exclude
.
回答by Aristotle Pagaltzis
Use git-update-indexto temporarily ignore changes to files that are already under version control:
使用git-update-index暂时忽略对已经在版本控制下的文件的更改:
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
如果您需要它在 git-reset 之后持续存在,请查看 update-index的skip-worktree
和no-skip-worktree
选项
回答by user1927627
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.
这只会从 git 中删除轨道,不会删除真实文件。
Then you can edit the ignore file to untrack these files or dirs.
然后您可以编辑忽略文件以取消跟踪这些文件或目录。
回答by Jean Hominal
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.
我无法真正回答一般问题(让 Git 忽略跟踪的文件)——我觉得它是一个弊大于利的功能。
However, the gitignore manual pagespecifies a few ways to configure patterns for excluded files.
但是,gitignore 手册页指定了几种为排除文件配置模式的方法。
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.
- 应该受版本控制并通过克隆分发到其他存储库的模式(即所有开发人员都希望忽略的
.gitignore
文件)应该放入一个文件中。
Meaning that your .gitignore
file should not be different from your coworkers - it is working as intended.
这意味着您的.gitignore
文件不应与您的同事不同 - 它按预期工作。
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
.
特定于特定存储库但不需要与其他相关存储库共享的模式(例如,位于存储库内但特定于一个用户的工作流的辅助文件)应该放入
$GIT_DIR/info/exclude
文件中。用户希望 git 在所有情况下忽略的模式(例如,由用户选择的编辑器生成的备份或临时文件)通常进入用户的 .git文件中指定的文件
core.excludesfile
中~/.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.
你有它。在core.excludesfile
文件中指定文件路径~/.gitconfig
,然后将要排除的模式放入其中。
回答by Abizern
I've written about three ways of excluding fileselsewhere.
In summary:
总之:
- A global git ignore file applies to all repositories on that system
- The .gitignore file in the repository applies the the repository and all clones of that repository.
- The .git/info/exclude file applies only to that repository.
- 全局 git ignore 文件适用于该系统上的所有存储库
- 存储库中的 .gitignore 文件应用存储库和该存储库的所有克隆。
- .git/info/exclude 文件仅适用于该存储库。
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.
列表中较低的项目优先于较高的项目,并且!
文件中任何模式中的项目前面的 a 反转先前的排除。
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.
这种范式可以在 Git 的其他地方看到。例如,如果您使用子模块,则要使用的子模块的 url.gitmodules
位于存储库中的文件中,但您可以覆盖 url 以在 .git/config 文件中使用。
回答by cubrman
Here is a generalized, ubiquitos (not individual) solution for those using Git under SmartGitHG viusal interface:
对于在 SmartGitHG viusal 界面下使用 Git 的人来说,这是一个通用的、无处不在的(不是单独的)解决方案:
- 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".
- 在“存储库”窗口中选择要忽略的文件夹;
- 在“文件”窗口中选择该文件夹中的所有文件(通过单击主窗口右上角区域中的相应过滤器图标取消隐藏未更改的文件);
- 单击控制面板上的“删除”(如果需要,请选中“删除本地文件”复选框);
- 提交本地更改;
- 右键单击“存储库”窗口中的文件夹;
- 单击“忽略”。