我如何忽略 git 存储库中的 bin 和 obj 文件夹?
我想忽略 git 存储库中的 bin 和 obj 文件夹。正如我发现的,在 .gitignore 中没有简单的方法可以做到这一点。那么,还有其他办法吗?在 Visual Studio 中使用干净的解决方案?
I want to ignore bin and obj folders from my git repository. As I've found out, there is no easy way to do this in .gitignore. So, are there any other way? Using clean solution in Visual Studio?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我不确定为什么这对你不起作用。如果有帮助,这里是我的 Visual Studio/git 项目之一中的典型 .gitignore 文件:
I'm not sure why this doesn't work for you. In case it helps, here's a typical .gitignore file from one of my Visual Studio/git projects:
简单地在 gitignore 中输入一个条目可能不会忽略文件,您可能需要提交它。我使用了以下方法并为我工作
,但是
,这会忽略我稍后将其包含到存储库中的脚本文件夹。
simply making an entry in gitignore may not ignore files, you may need to commit it. I used the following method and worked for me
then
However, this ignores my scripts folder which I included later into the repository.
更新
最近更新 Visual Studio 2019 后,以下选项不可用。
或者,您也可以从以下位置复制最新的 gitignore 内容。
https://raw.githubusercontent.com/github/gitignore/master/VisualStudio.gitignore
原始答案
如果您使用的是 Visual Studio,那么有一个简单的方法可以做到这一点。
单击“忽略文件”旁边的“添加”。
它将创建一个默认的 .gitignore 文件,该文件将忽略大多数常见文件夹和文件。框架/语言将使用的文件。
Update
After a recent update of Visual Studio 2019, the below option was not available.
Alternatively, you can also copy the latest gitignore content from the below location.
https://raw.githubusercontent.com/github/gitignore/master/VisualStudio.gitignore
Original Answer
If you are using Visual Studio then there is a simple way to do this.
Click on Add beside Ignore file.
It will create a default .gitignore file, which will ignore most of the common folders & files that will be used by the framework/language.
如果您之前已提交 bin 和 obj 文件夹,将它们添加到 .gitignore 不会自动删除它们。
您需要提交删除这些文件夹,例如
git rm -rf obj 或 git rm -rf yourProject/obj
然后提交删除
If you have committed the bin and obj folders previously, adding them to .gitignore will not automatically delete them.
You need to commit deleting these folders with for example
git rm -rf obj
orgit rm -rf yourProject/obj
then commit the deletion
尝试添加不需要的文件夹的名称,git 永远不会从前面提到的根目录开始进行这些更改。这对我来说很有效,可以将这些文件夹名称添加到
.gitignore
中:Try adding the names of unwanted folders and git will never take those changes right from that root directory mentioned onward. This worked for me though to add these folder names into
.gitignore
:我让一些新开发人员将他们的 OBJ 和 Bin 文件签入存储库。
尽管我有 gitignore 来排除这些文件,但它在我的本地存储库中不起作用。
我执行了以下操作来解决此问题:
如果存储库中还有其他用户特定文件,例如:Visual studio 中的 .suo
完成上述操作后,您可以将它们添加到 gitignore 文件中。
我在浪费了一点时间后发现了这一点,希望这对处于同一场景的人有所帮助。
I have had some new developers check in their OBJ and Bin files to the repository.
Even though I have the git ignore to exclude those files it would not work in my local repo.
I did the following to fix this
If you have other user specific files also in the repo ex: .suo in Visual studio
you can add them to the gitignore file after doing the above.
I found this after wasting quiet a bit of time hope this helps some one in the same scenario.
bin
和obj
文件夹,bin
andobj
folders in Windows Explorer如果您想忽略所有项目中的 bin 和 obj ,那么您可以使用(来自 gitignore 手册页)
core.excludesfile 可以在配置文件中设置,在 Unix 中是 ~/.gitconfig - 我不知道它在 Windows 下在哪里
If you want to ignore bin and obj in ALL your projects then you can use (from gitignore man page)
core.excludesfile can be set in a config file which is ~/.gitconfig in Unix - I don't know where it is under Windows
@Raphael Pinel 是绝对正确的。如果文件已在 Git 存储库中,则 .gitignore 不起作用。
我使用 VS2019 和 Azure DevOps 作为我的 Git 存储库。这是我修复它的方法。
在 Visual Studio 中,添加 .gitignore 文件(如有必要)并将该文件推送到存储库。
然后提交暂存,然后同步。
访问 Azure DevOps 网站。
从 Git 存储库中删除 bin/obj 文件夹。
在 Visual Studio 中,
您现在可以构建项目,Git 将忽略 .gitignore 文件中指定的文件夹。
@Raphael Pinel is absolutely correct. .gitignore doesn't work if the files are already in the Git repository.
I'm using VS2019 and Azure DevOps as my Git repository. Here's how I fixed it.
In Visual Studio, add .gitignore file (if necessary) and push that file to the repository.
Then commit staged, then sync.
Go to Azure DevOps website.
Delete bin/obj folders from Git repository.
In Visual Studio,
You can now build your project and Git will ignore the folders as specified in the .gitignore file.
如果您尝试通过源代码树,但忽略文件仍然没有进入,请转到工具-->选项->Git,然后选择 .gitignore 的位置
If you try through source tree and still the ignore files are not coming in , go to tools-->option->Git and then select location of the .gitignore
就我而言, bin 和 obj 文件夹嵌套在根 .gitignore 文件的 Project 文件夹下,因此我必须按如下所示添加它:
[PROJECT_FOLDER_NAME]/bin
[PROJECT_FOLDER_NAME]/obj
将 PROJECT_FOLDER_NAME 替换为您的项目文件夹名称
In my case, bin and obj folders were nested under Project folder from the root .gitignore file, so I had to add it like below:
[PROJECT_FOLDER_NAME]/bin
[PROJECT_FOLDER_NAME]/obj
Replace PROJECT_FOLDER_NAME with your project folder name