preface

Now that I’ve covered Git a little bit, there’s one last tip: let Git add files to ignore. In real project development, you might not want to add some files to your Git repository. For example, some sensitive files, temporary files, auto-generated files, log files, etc. So how do you do that?

Practical example

Suppose we want to create a project and manage it with Git, and now we have three files a.d, B.d, and c.d, and now we don’t want to add the C.D file to the repository. But Git will see these files, so it will still show up in the file list when we run Git status:

The GitTestProject project shown in the figure below has already been initialized with git init.

So what should we do at this point? If you want to keep a file in your project but don’t want it to be tracked by Git, you can use the.gitignore file. You just create the file and add it to the root of your project. Finally, fill in the name of the file you want to ignore (so Git doesn’t track). If we want to ignore c.mod we can add this line to the.gitignore file.

The.gitignore file cannot be created directly on the Mac system, we need to open the console in the root directory of the current project and type touch.gitignore to generate the.gitignore file.

C.md
Copy the code

Git status = git status = git status = git status

From the picture above, you can see that the C. med file is no longer controlled by Git. Because Git looks at the contents of the.gitignore file and automatically ignores the files you want to ignore based on what’s in that file.

Now that the c.mod file is ignored, we can simply use git add. To add all files in the current directory.

Remember the git add command from the previous section? Instead of adding files one by one, we can use a special character to represent the current directory and all subdirectories. Do you remember what it was? That’s right.(dot).

Ignore file wildcards

While we can write the names of the files we want to ignore directly in the.gitignore file, we can also use wildcards to ignore a list of files or folders that meet the criteria.

Wildcards allow you to use special characters to represent certain formats/characters. In the.gitignore file, we can use:

  • Blank lines as Spaces
  • #: marks the line as a comment
  • *: Matches 0 or more characters
  • ?: Matches 1 character
  • [abc]: – Matches a, B, or C
  • **: matches the nested directory, such as a/**/z matches the following
    • a/z
    • a/b/z
    • a/b/c/z

Let’s look at some simple examples to learn how to use wildcards. For example, if we need to ignore all 80 PNG images in the A folder at the root of the current project, we can write in the.gitignore file:

/A/*.png
Copy the code

Note: Git config –get core-.ignorecase Git config –get core-.ignorecase Git config core.ignorecase false can also be set to false.

Q&A

Now that you’ve learned how to add ignore files to Git, there are a few problems you’ll encounter in the process.

You cannot add files to your Git repository

Sometimes we want to add a file to the repository, but we find that we can’t because the file has been added to the.gitignore file and ignored. For example, if we wanted to add a c.mod file, we would get the following prompt:

git add C.md
The following paths are ignored by one of your .gitignore files:
C.md
Use -f if you really want to add them.
Copy the code

If we do need to add the file, we can use the -f option to force it.

git add -f C.md
Copy the code

If you use the -f option to force files to be added, they will have the same effect as other files normally tracked by Git. This means that the c. med file will be managed by Git trace.

Git check-ignore: git check-ignore: git check-ignore: git check-ignore: git check-ignore: git check-ignore

git check-ignore -v c.md
.gitignore:1:C.md c.md
Copy the code

For more information about this command, see the official documentation git-check-ignore

How do I ignore files that have been added to the staging area

In conjunction with our example above, in the project we forgot to add the ignore file c.mod to the.gitignore file and used the gid add. Command to add all the files to the staging area, as shown below:

Use git rm –cached to remove the c. med file from the staging area:

For details about how to use the git rm command, see the official git rm document

git rm --cached C.md
Copy the code

At this point, we are using the git status command to store the status of our repository, and we will find that the c. med file has been removed from the staging area.

Next, we can modify our.gitignore file to add c.mod to the ignore file.

How to ignore files that have been traced

Not only are files added to Git repositories, they are also pushed to remote repositories. And other partners have pulled updates from remote repositories. This is when some files need to be ignored.

In this case, if we add the file we want to ignore to the.gitignore file, it won’t do anything at all.

So what to do? We can use another command:

git update-index --assume-unchanged <file>
Copy the code

As for the example of C.md above, we can do as follows:

git update-index --assume-unchanged C.md
Copy the code

With this command, even if we make changes to a tracked c.md file, Git defaults that we haven’t changed the file. What does that mean? This means that we can modify the file locally and Git will always assume that it is the same as the original version. There will be no commit containing this file later in the project development.

Similarly, we can resume tracing the file by using the command:

git update-index --no-assume-unchanged <file>
Copy the code

For information on the use of this command, see the official documentation, no-assumption-Unchanged

A little advice

By reading this article, I believe you have realized how important it is to find the right time to add ignore files. A word of caution for those of you who are using Git or another version management system for the first time. When creating the repository, be sure to deal with the ignored files first, i.e., your.gitignore files must be submitted to !!!! after the project is initialized Because later to deal with ignored files, it is very troublesome and complex.

So in your regular projects, be sure to determine which files in your project should be tracked and which should not. Take the Android project for example:

*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
Copy the code

The above ignored files are common for Android projects because they are configuration files, intermediate files, project configuration files, etc. Of course, different projects need to configure different ignore files, you can configure according to their own requirements. The configuration of ignore files for other projects will not be covered here.

The last

The whole Git tutorial series has been finished by now, although it took a month, but when I finished, I couldn’t help feeling satisfied and proud. It’s not about having a lot of followers, it’s about being happy that you can stick to one thing. It feels good to set a small goal and achieve it step by step