1.WHY?
. gitignore file just state in this file that you don't want to add those files to git, so when you use git add These files are automatically ignored
2. Principle of ignoring documents
- Ignore the files automatically generated by the operating system, such as thumbnails;
- Ignore the intermediate files and executable files generated by compilation, that is, if a file is automatically generated through another file, it is not necessary to put the automatically generated files into the version library, such as those generated by Java compilation class file;
- Ignore your own configuration files with sensitive information, such as those that store passwords.
3. Usage
First, create a new name in your workspace gitignore file. Then, fill in the file names to be ignored, and Git will automatically ignore these files. There is no need to write from scratch gitignore file, GitHub has prepared various configuration files for us, which can be used only by combining them. All configuration files can be browsed directly online: https://github.com/github/gitignore
4. Examples
For example, your project is a java project Java files will be generated after compilation class files, which in most cases are files that do not want to be transferred to the warehouse. At this time, you can directly apply github gitignore file template. https://github.com/github/gitignore/blob/master/Java.gitignore Copy these ignored file information to your In gitignore file:
*.class # Mobile Tools for Java (J2ME) .mtj.tmp/ # Package Files # *.jar *.war *.ear # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid*
You can see that github provides us with the most popular gitignore file configuration. preservation. After ignoring the file, let's check git status to see if there are any files we don't need that will be added to git:
$ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: .gitignore new file: HelloWorld.java Untracked files: (use "git add <file>..." to include in what will be committed) Config.ini
For example, there is a config. In my project directory INI file, which is a local configuration file. I don't want to upload it to git. We can add such a configuration to gitignore file:
Config.ini;
Or you want to ignore everything ini file you can write as follows:
*.ini
If some files have been ignored by you, you can't add them when you use git add. For example, I ignored * Class, now I want to put HelloWorld Add class to git:
$ git add HelloWorld.class The following paths are ignored by one of your .gitignore files: HelloWorld.class Use -f if you really want to add them.
Git will remind us that this file has been ignored by us. You need to add the - f parameter to force it to be added to git:
$ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: .gitignore new file: HelloWorld.class new file: HelloWorld.java
In this way, it can be forcibly added to the cache. If we accidentally add a file that we want to ignore to the cache, we can use the rm command to remove it from the cache:
$ git rm HelloWorld.class --cached rm 'HelloWorld.class'
If you have uploaded the files you don't want to upload to the git warehouse, you must delete them from the remote warehouse first. We can delete them directly from the remote warehouse, and then pull the code to the local warehouse. These files will be deleted, or these files will be deleted from the local warehouse and displayed in the Add these files you want to ignore to the gitignore file, and then push them to the remote warehouse.
5. View gitignore rules
If you send it There is something wrong with gitignore. You need to find out which rule is wrong. You can use git check ignore command to check:
$ git check-ignore -v HelloWorld.class .gitignore:1:*.class HelloWorld.class
You can see HelloWorld class matches our first * class, so the file was ignored.
6. Ignore rule file syntax
a. Ignore specified file / directory
# Ignore specified file HelloWrold.class # Ignore specified folder bin/ bin/gen/
b. Wildcard ignore rule
The wildcard rules are as follows:
# Ignore All files of class *.class # Ignore folders with ignore at the end of the name *ignore/ # Ignore folders with ignore in the middle of the name *ignore*/