如何在 Sourcetree 中忽略具有 Git 存储库的文件夹中的所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9665399/
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
How do I ignore all files in a folder with a Git repository in Sourcetree?
提问by rubdottocom
I have a Bitbucket Git repository managed with Sourcetree.
我有一个使用 Sourcetree 管理的 Bitbucket Git 存储库。
I have two folders that I want to commit, but I need to ignore all the files in these folders, because they contain only temporary files.
我有两个文件夹要提交,但我需要忽略这些文件夹中的所有文件,因为它们只包含临时文件。
How can I do that?
我怎样才能做到这一点?
采纳答案by Gergo Erdosi
Add this to .gitignore:
将此添加到 .gitignore:
*
!.gitignore
回答by Kalzem
For Sourcetree users: If you want to ignore a specific folder, just select a file from this folder, right-click on it and do "Ignore...". You will have a pop-up menu where you can ignore "Ignore everything beneath: <YOUR UNWANTED FOLDER>"
对于 Sourcetree 用户:如果您想忽略特定文件夹,只需从该文件夹中选择一个文件,右键单击它并执行“忽略...”。您将有一个弹出菜单,您可以在其中忽略“忽略下面的所有内容:<您不需要的文件夹>”
If you have the "Ignore" option greyed out, you have to select the "Stop Tracking" option. After that the file will be added to Staged files with a minus sign on red background icon and the file's icon in Unstaged fileslist will change to a question sign on a violet background. Now in Unstaged fileslist, the "Ignore" option is enabled again. Just do as described above.
如果“忽略”选项变灰,则必须选择“停止跟踪”选项。之后,文件将添加到暂存文件中,红色背景图标上带有减号,未暂存文件列表中的文件图标将更改为紫色背景上的问号。现在在Unstaged 文件列表中,“忽略”选项再次启用。只需按照上述说明进行操作。
回答by Andrei Karcheuski
Right click on a file → Ignore→ Ignore everything beneath.
右键单击文件 →忽略→忽略下面的所有内容。
If the Ignore optionis grayed out then:
如果忽略选项变灰,则:
- Stage a file first
- Right click → Stop Tracking
- That file will appear in the pane below with a question mark → Right click on it→ Ignore
- 首先暂存文件
- 右键单击 →停止跟踪
- 该文件将出现在下面的窗格中,并带有问号→右键单击它→忽略
回答by Aasmund Eldhuset
As far as I know, Git doesn't track folders, only files - so empty folders (or folders where all files are ignored) cannot be committed. If you e.g. need the folder to be present due to some step in your build process, perhaps you can have your build tool create it instead? Or you could put one empty, unignored file in the folder and commit it.
据我所知,Git 不跟踪文件夹,只跟踪文件 - 因此无法提交空文件夹(或忽略所有文件的文件夹)。例如,如果您由于构建过程中的某个步骤需要该文件夹存在,也许您可以让构建工具创建它?或者您可以在文件夹中放置一个空的、未被忽略的文件并提交它。
回答by Karl Hoaglund
After beating my head on this for at least an hour, I offer this answer to try to expand on the comments some others have made. To ignore a folder/directory, do the following: if you don't have a .gitignore file in the root of your project (that name exactly ".gitignore"), create a dummy text file in the folder you want to exclude. Inside of Source Tree, right click on it and select Ignore. You'll get a popup that looks like this.
在为此纠结了至少一个小时之后,我提供了这个答案,以尝试扩展其他人的评论。要忽略文件夹/目录,请执行以下操作:如果您的项目根目录中没有 .gitignore 文件(该名称正好是“.gitignore”),请在要排除的文件夹中创建一个虚拟文本文件。在 Source Tree 中,右键单击它并选择 Ignore。你会得到一个看起来像这样的弹出窗口。
Select "Everything underneath" and select the folder you want to ignore from the drop-down list. This will create a .gitignore file in your root directory and put the folder specification in it.
选择“下面的所有内容”,然后从下拉列表中选择要忽略的文件夹。这将在您的根目录中创建一个 .gitignore 文件并将文件夹规范放入其中。
If you do have a .gitignore folder already in your root folder, you could follow the same approach above, or you can just edit the .gitignore file and add the folder you want to exclude. It's just a text file. Note that it uses forward slashes in path names rather than backslashes, as we Windows users are accustomed to. I tried creating a .gitignore text file by hand in Windows Explorer, but it didn't let me create a file without a name (i.e. with only the extension).
如果您的根文件夹中已经有一个 .gitignore 文件夹,您可以按照上述相同的方法,或者您可以编辑 .gitignore 文件并添加要排除的文件夹。它只是一个文本文件。请注意,它在路径名中使用正斜杠而不是反斜杠,正如我们 Windows 用户所习惯的那样。我尝试在 Windows 资源管理器中手动创建一个 .gitignore 文本文件,但它不允许我创建一个没有名称的文件(即只有扩展名)。
Note that adding the .gitignore and the file specification will have no effect on files that are already being tracked.If you're already tracking these, you'll have to stop tracking them. (Right-click on the folder or file and select Stop Tracking.) You'll then see them change from having a green/clean or amber/changed icon to a red/removed icon. On your next commit the files will be removed from the repository and thereafter appear with a blue/ignored icon. Another contributor asked why Ignore was disabled for particular files and I believe it was because he was trying to ignore a file that was already being tracked. You can only ignore a file that has a blue question mark icon.
请注意,添加 .gitignore 和文件规范对已经被跟踪的文件没有影响。如果您已经在跟踪这些,则必须停止跟踪它们。(右键单击文件夹或文件并选择停止跟踪。)然后您会看到它们从绿色/干净或琥珀色/已更改图标变为红色/已删除图标。在您下次提交时,文件将从存储库中删除,然后显示为蓝色/忽略图标。另一位贡献者问为什么对特定文件禁用忽略,我相信这是因为他试图忽略已经被跟踪的文件。您只能忽略带有蓝色问号图标的文件。
回答by AD7six
As mentioned by others: git doesn't track folders, only files.
正如其他人所提到的:git 不跟踪文件夹,只跟踪文件。
You can ensure a folder exists with these commands (or equivalent logic):
您可以使用这些命令(或等效逻辑)确保文件夹存在:
echo "*" > keepthisfolder/.gitignore
git add --force keepthisfolder/.gitignore
git commit -m "adding git ignore file to keepthisfolder"
The existence of the file will mean anyone checking out the repository will have the folder.
该文件的存在意味着任何检出存储库的人都将拥有该文件夹。
The contents of the gitignore file will mean anything in it are ignored
gitignore 文件的内容意味着其中的任何内容都将被忽略
You do notneed to not-ignore the .gitignore file itself. It is a rule which would serve no purpose at all once committed.
您不需要不忽略 .gitignore 文件本身。这是一条一旦承诺就毫无意义的规则。
OR
或者
if you prefer to keep all your ignore definitions in the same place, you can create .gitignore in the root of your project with contents like so:
如果您希望将所有忽略定义保留在同一位置,您可以在项目的根目录中创建 .gitignore ,内容如下:
*.tmp # example, ignore all .tmp files in any folder
path/to/keepthisfolder
path/to/keepthatfolder
and to ensure the folders exist
并确保文件夹存在
touch path/to/keepthisfolder/anything
git add --force path/to/keepthisfolder/anything
git commit -m "ensure keepthisfolder exists when checked out"
"anything" can literally be anything. Common used names are .gitignore, .gitkeep, empty.
“任何东西”字面上可以是任何东西。常用名称有 .gitignore、.gitkeep、empty。
回答by LeRoy
If your Ignoreoption is grayed out, you have to stop tracking the file before you can ignore it. You can simply right click on the file and hit "Stop Tracking".
如果您的忽略选项变灰,则您必须先停止跟踪该文件,然后才能忽略它。您只需右键单击该文件并点击“停止跟踪”即可。
A blue icon should appear next to it. Right click on it again and click ignore.
它旁边应该会出现一个蓝色图标。再次右键单击它,然后单击忽略。
回答by Rajesh Kumar
Ignore full folder on source tree.
忽略源树上的完整文件夹。
Just Open Repository >Repository setting > Edit git ignore File and
you can rite some thing like this :
*.pdb
*.bak
*.dll
*.lib
.gitignore
packages/
*/bin/
*/obj/
For bin folder and obj folder just write : */bin/ */obj/
对于 bin 文件夹和 obj 文件夹只需写: */bin/ */obj/
回答by AVEbrahimi
In Sourcetree: Just ignore a file in specified folder. Sourcetree will ask if you like to ignore all files in that folder. It's perfect!
在 Sourcetree 中:只需忽略指定文件夹中的文件。Sourcetree 会询问您是否要忽略该文件夹中的所有文件。这是完美的!