Git Tutorial 8 - Use .gitignore to ignore files
What I don't want
Sometimes there are some files or directories I don't want to commit to Git. 'Please let me know if you don't want some files or directories to be committed', Git said to me if it can. That's it. There is a way to inform Git what I don't want. That's the job of file .gitignore.
What's file .gitignore
You can create a text file named .gitignore (be careful it starts with dot '.'), which includes some rules you add to indicates which files or directories you want Git to ignore. One thing you must keep in mind when using it. That's to create file .gitignore before you make a commit. Git will NOT ignore any file or directory if you already have checked in any file or directory. What if you add a rule later than you add a file that you should haven't added? I will tell you how to deal with it later on.
How to use file .gitignore
Let's take a look at an example which I've been working on recently. Before I create file .gitignore, there is the screen shot which shows out untrack files and directories from Git's sight.

figure 8.1
In my working directory we can see the list of untracked files and directories.
For some reasons I don't want file go.txt, directory node_modules and two subdirectories (test/scan and test/wait) to be checked in. On the other words I want Git to ignore these 2 items. We'll do the following steps to ignore them one by one.
1. Create a text file .gitignore in my working directory (the location of my Git repository, here it called fimpo)
2. Open file .gitignore to add a rule for ignoring files and directories
3. Run 'git status' again to check if they are excluded from the list of untracked files
Let's do the steps
1. Create a text file .gitignore in my working directory
Navigate to your working directory and run the following command to create a empty file .gitignore
touch .gitignore
Since I used Git Bash console which accepts linux bash command, I use command touch to create a text file.
2. Open file .gitignore to add a rule for ignoring files or directories
You can use any text editor to open .gitignore and edit rules in it. Here I use vi, a text editor in linux command environment as below

figure 8.2
You can see I've added 4 lines to file .gitignore . They are 4 rules:
The first line is a rule telling Git to ignore file go.txt in the current working directory.
The second line is another rule telling Git to ignore directory node_modules.
The rest lines are rules telling Git to ignore these 2 subdirectories.
The rules are simple, just list filename and directory name which you do not want to check in.
3. Run 'git status' again to check if they are excluded from the list of untracked files

figure 8.3
The file go.txt and directory node_modules are removed from the list.
We ignore 2 subfolders in folder 'test', which didn't prevent folder 'test' still being there. Let's add folder 'test' to staging are to see if 2 subfolders will be added along with.

figure 8.4
Besides the 2 subdirectories, other items (3 js files here) in directory 'test' are added along to staging area. That prove the rules for them are working well.
How to ignore a file which has been checked in
If you already have a file checked in and you want to ignore it. Git will NOT ignore the file if you add a rule later. So make sure you add the rule before you do something to the files or directories. What if you forget to add the rules before you check in? What can you do to go back?
Let's take the example as above. In figure 8.4 there are 4 files in test folder being added to staging area. Actually I don't want file report.txt in. This is my test report generated automatically(for example). I don't want to check in and share with others because they'll have their own. Now what I can do ...
1) Add the below rule to .gitignore
test/report.txt
2) Untrack file test/report.txt by running command
git rm --cached test/report.txt
3) Check status if the file report.txt has been ignored
As the picture shows us, the file report.txt is out of tracked list and also out of untracked list.

figure 8.5

浙公网安备 33010602011771号