Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Creating a .gitignore file

Tags:

git

gitignore

I'm looking to create a .gitignore file so certain files are do not get checked in to the repository. Does anyone have a guide on how and where to place this file? I have tried placing it in my working directory, ran git status and it is still picking up on the files I would like it to ignore.

I used this .gitignore file I have found:

###################
# compiled source #
###################
*.com
*.class
*.dll
*.exe
*.pdb
*.dll.config
*.cache
*.suo
# Include dlls if they’re in the NuGet packages directory
!/packages/*/lib/*.dll
# Include dlls if they're in the CommonReferences directory
!*CommonReferences/*.dll
####################
# VS Upgrade stuff #
####################
UpgradeLog.XML
_UpgradeReport_Files/
###############
# Directories #
###############
bin/
obj/
TestResults/
###################
# Web publish log #
###################
*.Publish.xml
#############
# Resharper #
#############
/_ReSharper.*
*.ReSharper.*
############
# Packages #
############
# it’s better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
######################
# Logs and databases #
######################
*.log
*.sqlite
# OS generated files #
######################
.DS_Store?
ehthumbs.db
Icon?
Thumbs.db
like image 934
Funky Avatar asked Jun 15 '12 12:06

Funky


People also ask

Where can I create a .gitignore file?

You can create a . gitignore file in your repository's root directory to tell Git which files and directories to ignore when you make a commit. To share the ignore rules with other users who clone the repository, commit the . gitignore file in to your repository.

What is the purpose of adding a .gitignore file to a Git repository?

The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked. To stop tracking a file that is currently tracked, use git rm --cached.

What should I add in Git ignore?

gitignore should list the names or name-patterns of files that will be found in work-trees when working with your project, but that should not be committed to the project. In other words, it's not OS-specific, it's project-specific.


2 Answers

Placement of .gitignore depends if the files need to be ignored for just one repo or for all your repos. For one repo, place it in the root of your repo.

When you create a .gitignore file in an existing repo, or add files that were already in the repo, you have to make sure to remove the to-be-ignored files from the repo.

If you have a test.dll in the root, you have to do

git rm --cached test.dll

Now if you have a lot of files, like you said you can opt for the following option, remove everything from the cache, add it all back (the files in .gitignore will not be added) and commit everything again.

git rm -r --cached .
git add .
git commit -m "Start using .gitignore"
like image 54
Peter van der Does Avatar answered Oct 14 '22 00:10

Peter van der Does


You have to add .gitignore to the index before Git sees it. I.e., git add .gitignore.

like image 27
Fredrik Pihl Avatar answered Oct 14 '22 01:10

Fredrik Pihl