Git:隐藏/删除永远不会被版本控制的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1248827/
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: hide/remove files never to be versioned
提问by peterchen
I'm trying to get started using Git and TortoiseGit.
我正在尝试开始使用 Git 和 TortoiseGit。
Is there a way to hide files that should never be tracked completely? Currently, all temporary build files are in the same "Not Versioned" list as new files when I commit a change.
有没有办法隐藏不应该被完全跟踪的文件?目前,当我提交更改时,所有临时构建文件都在与新文件相同的“未版本控制”列表中。
回答by lemonad
Create a text file called .gitignore
in your root folder and add lines like the following to exclude files:
创建一个.gitignore
在根文件夹中调用的文本文件,并添加如下所示的行以排除文件:
*.obj
test.c
Then add .gitignore to your Git repository and commit:
然后将 .gitignore 添加到您的 Git 存储库并提交:
$ git add .gitignore
$ git commit .gitignore
回答by CB Bailey
回答by linquize
TortoiseGit can add files to the ignore list with many options.
TortoiseGit 可以使用许多选项将文件添加到忽略列表中。
Right click on a file in a repository → TortoiseGit→ Add to ignore list→ by filename / by extension
右键单击存储库中的文件 → TortoiseGit→添加到忽略列表→ 按文件名/按扩展名
A dialog shows to choose options:
一个对话框显示选择选项:
Ignore type
忽略类型
This file only (match absolute name),
仅此文件(匹配绝对名称),
Recursive (match same name)
递归(匹配同名)
Ignore file
忽略文件
.gitignore
in root directory (need to commit)
.gitignore
在根目录中(需要提交)
.gitignore
in included directory (need to commit)
.gitignore
在包含的目录中(需要提交)
.git/info/exclude
(like a configuration file, stored locally)
.git/info/exclude
(就像一个配置文件,存储在本地)
See also: Manual entry, Chapter 2. TortoiseGit Daily Use Guide, Ignoring Files And Directories.
另请参阅:手动输入,第 2 章 TortoiseGit 日常使用指南,忽略文件和目录。
回答by Alexey Vassiliev
In case you have files already added to the repository, but need to remove/ignore them, follow these steps:
如果您已将文件添加到存储库,但需要删除/忽略它们,请按照下列步骤操作:
- Move those files somewhere out of the repository.
- Make a commit, which would delete those files from the repository
- Move those files back to the repository to the place where they have been
- Make a commit, but now instead of adding those files to the repository, right click on those files. Select Add to ignore list→ By file name. The option to which .gitignore you add those is irrelevant, but you can look up the difference on the Internet.
- 将这些文件移出存储库的某个位置。
- 进行提交,这将从存储库中删除这些文件
- 将这些文件移回存储库到它们所在的位置
- 进行提交,但现在不是将这些文件添加到存储库,而是右键单击这些文件。选择添加到忽略列表→按文件名。您添加的 .gitignore 选项无关紧要,但您可以在 Internet 上查找差异。
回答by StuartLC
Since one of the benefits of TortoiseGit is to use the UI, especially if you have used other products like TortoiseSVNand TortoiseHg, here are some UI ways to achieve this:
由于 TortoiseGit 的好处之一是使用 UI,特别是如果您使用过其他产品,如TortoiseSVN和TortoiseHg,这里有一些 UI 方法可以实现这一点:
File not already added
文件尚未添加
An easy way to add files to the .gitignore
exclusions is when you are selecting files to be staged when you do a git commit
through Tortoise - Tortoise will list all non-ignored files which aren't currently in your repository under the Not Versioned Files
box at the bottom of the commit screen:
将文件添加到.gitignore
排除项的一种简单方法是,当您git commit
通过 Tortoise执行时选择要暂存的文件- Tortoise 将Not Versioned Files
在提交底部的框下列出当前不在您的存储库中的所有未忽略文件屏幕:
Unwanted file already added by mistake
不需要的文件已被错误添加
If you've already committed or pushed the undesirable file, another way in newer (circa 1.8.14) versions of TortoiseGit to remove an existing file from your repository branch and add it to one of the .gitignore
s is hidden under the right mouse function Delete and add to ignore list
:
如果您已经提交或推送了不需要的文件,则在较新(大约 1.8.14)版本的 TortoiseGit 中,另一种从存储库分支中删除现有文件并将其添加到.gitignore
s 中的方法隐藏在鼠标右键功能下Delete and add to ignore list
:
You then have several .gitignore
options to elect, either on whether to add just this file or wildcard matches, and to which .gitignore
to add it to - local folder, repository root, or your `.git/info/exclude'.
然后你有几个.gitignore
选项可以选择,要么只添加这个文件,要么添加通配符匹配,以及将它.gitignore
添加到哪个- 本地文件夹、存储库根目录或你的“.git/info/exclude”。
However, right at the last minute, you are given the opportunity to keepthe local copy of the file:
但是,就在最后一刻,您有机会保留文件的本地副本: