I just created a Github repository and was wondering what the .gitignore
file was for. I started by not creating one, but added one due to the fact that most repositories have one.
Do I need to have one? Can/do I just ignore it, or does it have a use?
I did some research on the subject but couldn't find a concrete explanation.
问题:
回答1:
.gitignore
tells git which files (or patterns) it should ignore. It's usually used to avoid committing transient files from your working directory that aren't useful to other collaborators, such as compilation products, temporary files IDEs create, etc.
You can find the full details here.
回答2:
It's a list of files you want git to ignore in your work directory.
Say you're on a Mac and you have .DS_Store files in all your directories. You want git to ignore them, so you add .DS_Store as a line in .gitignore. And so on.
The git docs will tell you all you need to know: http://git-scm.com/docs/gitignore
回答3:
When you are doing a commit you do not want accidentally include temporary files or build specific folders. Hence use a .gitignore
listing out items you want to ignore from committing.
Also, importantly git status
is one of the most frequently used command where you want git status
to list out the files that have been modified.
You would want your git status
list look clean from unwanted files. For instance, I changed a.cpp, b.cpp, c.cpp, d.cpp & e.cpp
I want my git status
to list the following:
git status
a.cpp
b.cpp
c.cpp
d.cpp
e.cpp
I dont want git status
to list out changed files like this with the intermediary object files & files from the build folder
git status
a.cpp
b.cpp
c.cpp
d.cpp
e.cpp
.DS_Store
/build/program.o
/build/program.cmake
Hence, to get myself free from git status
to list out these intermediate temporary files & accidentally committing them into the repo, I should create a .gitignore
which everyone does. All I need to do list out the files & folders in the .gitignore
that I want to exclude from committing.
Following is my .gitignore
to avoid committing unnecessary files
/*.cmake
/*.DS_Store
/.user
/build
回答4:
There are files you don't want Git to check in to. Git sees every file in your working copy as one of three things:
- tracked - a file which has been previously staged or committed;
- untracked - a file which has not been staged or committed; or
- ignored - a file which Git has been explicitly told to ignore.
Ignored files are usually built artifacts and machine-generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are:
- dependency caches, such as the contents of
/node_modules
or/packages
- compiled code, such as
.o
,.pyc
, and.class
files - build output directories, such as
/bin
,/out
, or/target
- files generated at runtime, such as
.log
,.lock
, or.tmp
- hidden system files, such as
.DS_Store
orThumbs.db
- personal IDE config files, such as
.idea/workspace.xml
Ignored files are tracked in a special file named .gitignore
that is checked in at the root of your repository. There is no explicit git ignore command: instead the .gitignore
file must be edited and committed by hand when you have new files that you wish to ignore. .gitignore
files contain patterns that are matched against file names in your repository to determine whether or not they should be ignored. Here is a sample.gitignore
file.
For more details look at this link
回答5:
Main purpose of .gitignore – so you avoid adding your passwords etc to Git
Don’t trust your secrets on the internet
Github is on the internet. If you put your code on the internet (i.e. on Github), then you really need to make sure that you don’t put anything compromising on there – e.g. passwords, or security credentials, or secrets. Sure, Github has security, but what if that security gets compromised? You do not want to undermine the entire security of your application, simply because Github has its security undermined. What then is the solution?
Ask Git to Ignore Certain Files
You can ask git to ignore certain files that are essential to the running of your application (e.g. passwords or secrets). That way, when you put your code on the internet, nothing will happen even if somebody manages to see your code. And if you’re working on an open source project – again there are some things you simply don’t want to ‘check into git’.
Anything in the .gitignore file is then ignored by Git. Which is what you want!