今是昨非

今是昨非

日出江花红胜火,春来江水绿如蓝

gitignore does not ignore specified files.

Background#

When using Tencent IMSDK, the corresponding library is dependent on through Pod. The Pods folder is configured to be ignored in .gitignore, but it is necessary to modify the source code of the IM SDK and worry about being overwritten after reinstallation. Therefore, for Tencent IMSDK, you want to set it to not be ignored in .gitignore. How to do it?

Implementation#

Set as follows:

!/Pods/
/Pods/*
!/Pods/TUI*/

After setting, if it is found that it is not effective, you can verify it with the following command:

git check-ignore -v Pods/TUIChat/

In the figure below, in the first verification, it indicates that a line in .gitignore causes it to be ignored, and then modify it. Run it again and there is no result, indicating that it has been successful.

Example

Principle#

Reprinted from: [Git] Configuration and Use of .gitignore

The matching syntax of the .gitignore ignore rule is as follows:

In the .gitignore file, the syntax of each line of the ignore rule is as follows:
1. Spaces do not match any files and can be used as separators. They can be escaped with a backslash.
2. Lines starting with "#" will be ignored by Git. That is, files starting with "#" are marked as comments and can be escaped with a backslash.
3. Standard glob patterns can be used for matching. The so-called glob pattern refers to the simplified regular expressions used by the shell.
4. Starting with a slash "/" indicates a directory; a pattern ending with a slash "/" only matches the folder and its contents under the folder path, but does not match the file in the folder; a pattern starting with a slash "/" matches the project root directory; If a pattern does not contain a slash, it matches the content relative to the current .gitignore file path. If the pattern is not in the .gitignore file, it is relative to the project root directory.
5. An asterisk "*" matches multiple characters, that is, matches any number of arbitrary characters; using two asterisks "**" indicates matching any intermediate directory, such as a/**/z can match a/z, a/b/z, or a/b/c/z, etc.
6. A question mark "?" matches a single character, that is, matches any single character.
7. A matching list containing a single character is enclosed in square brackets "[]", that is, matches any character listed in the square brackets. For example, [abc] means to match an a, or match a b, or match a c; if you use a hyphen to separate two characters in square brackets, it means that all characters in this range can be matched. For example, [0-9] means to match all digits from 0 to 9, and [a-z] means to match any lowercase letter).
8. An exclamation mark "!" indicates not to ignore (track) the matched file or directory, that is, to ignore all patterns except the specified pattern. You can use an exclamation mark "!" in front of the pattern to negate it. It is important to note that: if the parent directory of the file has been excluded by the previous rule, then the "!" rule does not work for this file. That is to say, the pattern starting with "!" means negation, and the file will be included again. If the parent directory of the file is excluded, the "!" will not include it again. You can use a backslash to escape it.

Please note: Git matches the .ignore configuration file according to the rules from top to bottom, which means that if the previous rule matches a larger range, the subsequent rules will not take effect.

Very important: If you accidentally pushed the project before creating the .gitignore file, even if you write new filtering rules in the .gitignore file, these rules will not take effect, and Git will still manage all files. In other words, the reason for this problem is that Git has already started managing these files, so you cannot filter them through filtering rules. Therefore, it is important to develop the habit of creating the .gitignore file at the beginning of the project, otherwise it will be very troublesome to deal with once pushed.

# This indicates that it is a comment and will be ignored by Git
*.a             # Ignore all files ending with .a
!lib.a          # Except for lib.a
/TODO           # Only ignore the TODO file in the root directory, excluding subdir/TODO
build/          # Ignore all files in the build/ directory, filter the entire build folder;
doc/*.txt       # Ignore doc/notes.txt but not doc/server/arch.txt

bin/:           # Ignore the bin folder in the current path, all contents in this folder will be ignored, except for bin files
/bin:           # Ignore the bin folder in the root directory
/*.c:           # Ignore cat.c, but not build/cat.c
debug/*.obj:    # Ignore debug/io.obj, but not debug/common/io.obj and tools/debug/io.obj
**/foo:         # Ignore /foo, a/foo, a/b/foo, etc.
a/**/b:         # Ignore a/b, a/x/b, a/x/y/b, etc.
!/bin/run.sh    # Do not ignore the run.sh file in the bin directory
*.log:          # Ignore all .log files
config.php:     # Ignore the config.php file in the current path

/mtk/           # Filter the entire folder
*.zip           # Filter all .zip files
/mtk/do.c       # Filter a specific file

The filtered files will not appear in the git repository (GitLab or GitHub), but they still exist in the local repository. They will not be uploaded when pushed.

Please note that .gitignore can also specify which files to add to version control, as follows:
!*.zip
!/mtk/one.txt

The only difference is that there is an exclamation mark at the beginning of the rule. Git will add files that meet these rules to version control. Why are there two types of rules? Imagine a scenario: if we only need to manage the one.txt file in the /mtk/ directory, and other files in this directory do not need to be managed, then the .gitignore rule should be written as follows:
/mtk/*
!/mtk/one.txt

If we only have filtering rules and no adding rules, then we need to list all files in the /mtk/ directory except one.txt!
Note that /mtk/* cannot be written as /mtk/, otherwise the parent directory will be excluded by the previous rule, and even if the one.txt file has a "!" filtering rule, it will not take effect!

----------------------------------------------------------------------------------
There are also some rules as follows:
fd1/*
Explanation: Ignore all contents under the fd1 directory; note that both the /fd1/ directory in the root directory and the /child/fd1/ directory in some subdirectories will be ignored;

/fd1/*
Explanation: Ignore all contents under the /fd1/ directory in the root directory;

/*
!.gitignore
!/fw/
/fw/*
!/fw/bin/
!/fw/sf/
Explanation: Ignore all contents, but do not ignore the .gitignore file, the /fw/bin/ and /fw/sf/ directories in the root directory; note that you need to use the ! rule for the parent directory of bin/ to prevent it from being excluded.

If you find that .gitignore is not effective, refer to: .gitignore ignore rules, you can try the following operations:

1. git rm -r --cached .
2. git add .
3. git commit -m "update .gitignore"

The reason is: After the .gitignore file is configured, it often cannot be invalidated. This is because .gitignore can only ignore files that have not been tracked. Because Git has a local cache, if the file has been included in version control, modifying .gitignore will not be invalidated. The solution is to delete the local cache of git and then recommit it.

Reference#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.