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

.NetGitGitignore

.Net Problem Overview


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?

.Net Solutions


Solution 1 - .Net

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

Solution 2 - .Net

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.

Solution 3 - .Net

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.

enter image description here

Solution 4 - .Net

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

Solution 5 - .Net

  • 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

Solution 6 - .Net

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.

Solution 7 - .Net

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

Solution 8 - .Net

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

Solution 9 - .Net

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

Solution 10 - .Net

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 enter image description here

Solution 11 - .Net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionchester89View Question on Stackoverflow
Solution 1 - .NetTim RobinsonView Answer on Stackoverflow
Solution 2 - .NetArvind KrmarView Answer on Stackoverflow
Solution 3 - .NetNilayView Answer on Stackoverflow
Solution 4 - .NetRaphael PinelView Answer on Stackoverflow
Solution 5 - .NetDenys WesselsView Answer on Stackoverflow
Solution 6 - .NetsavvyView Answer on Stackoverflow
Solution 7 - .NetShoaib KhalilView Answer on Stackoverflow
Solution 8 - .NetmmmmmmView Answer on Stackoverflow
Solution 9 - .NetMr_EngineerView Answer on Stackoverflow
Solution 10 - .NetmakduView Answer on Stackoverflow
Solution 11 - .Netpk_codeView Answer on Stackoverflow