Git - Creating a .gitignore file

2019-01-10 23:41发布

问题:

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

回答1:

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"


回答2:

you have to add .gitignore to the index before git sees it. i.e. git add .gitignore



回答3:

The projects that I have worked on, I have the .gitignore file in the root directory of the project, where the .git directory would be. Make sure the file is committed.

Note, that if you have already committed the files you are trying to ignore (i.e. Git is tracking them) then they can't be ignored. You would have to untrack them first.

https://help.github.com/articles/ignoring-files



回答4:

From the link @thescientist has posted:

Git lets you ignore those files by assuming they are unchanged. This is done by running the

git update-index --assume-unchanged path/to/file.txt

command. Once marking a file as such, git will completely ignore any changes on that file; they will not show up when running git status or git diff, nor will they ever be committed.



标签: git gitignore