简体   繁体   中英

How could I ignore bin and obj folders from git repository?

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?

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:

*.suo
*.user
_ReSharper.*
bin
obj
packages

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

git rm -r --cached .

git add .

then

git commit -am "Remove ignored files"

However, this ignores my scripts folder which I included later into the repository.

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.

  1. Open Team Explorer
  2. Click on settings
  3. Repository Settings
  4. Under Ignore & Attribute file section 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.

在此处输入图片说明

  • Locate the bin and obj folders in Windows Explorer
  • Right click TortoiseGit > Delete and add to ignore list > bin
  • Right click TortoiseGit > Delete and add to ignore list > 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 or git rm -rf yourProject/obj then commit the deletion

If you want to ignore bin and obj in ALL your projects then you can use (from gitignore man page )

Patterns read from the file specified by the configuration variable core.excludesfile.

core.excludesfile can be set in a config file which is ~/.gitconfig in Unix - I don't know where it is under Windows

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

  1. Deleted the Bin and Obj folders from the repository.
  2. Deleted bin and obj folders from my local repo as well.
  3. Created/Update the gitignore and save (If not done already)

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.

@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.

 Team Explorer->Changes->Right Click
 .gitignore->Stage.

Then commit staged, then sync. Go to Azure DevOps website.
Delete bin/obj folders from Git repository. In Visual Studio,

Team Explorer->Changes->"Undo Changes" to those folders.
Team Explorer->Synch->Pull to update local Git repository.

You can now build your project and Git will ignore the folders as specified in the .gitignore file.

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在此处输入图片说明

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

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 :

bin
obj
packages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM