[Problem analysis and solution] git does not take effect after adding .gitignore

1. Problem phenomenon

In the project managed by git that has been submitted, after adding a new .gitignore file or modifying the .gitignore file, the newly added content will not take effect.

Second, the cause of the problem

Because we misunderstood the purpose of the .gitignore file, this file can only be applied to Untracked Files, that is, files that have never been recorded by Git (files that have never been added and committed since they were added).

The reason why your rule doesn't work is because those .log files have been recorded by Git, so .gitignore has no effect on them at all.

Three, the solution

1. Delete the tracking of the file from the Git database;
2. Write the corresponding rules into .gitignore to make the ignore take effect;
3. Submit + push.

Specific instructions:

git rm -r --cached .
git add .
git commit 

4. Examples

1. First compile main.c with gcc to generate a.exe, and submit a.exe to git record. Then modify main.c to regenerate a.exe, add .gitignore, and add *.exe to .gitignre.

insert image description here

2. Submit the modification of main.c and the addition of .gitignore to git. At this time, git st checks, only .exe is modified:

insert image description here

3. After the preparatory work is completed, perform the above three steps to ignore the *.exe file by git

Delete all modifications:
insert image description here
then add all modifications:
insert image description here
modify main.c again to generate .exe and it will not be recognized by git.

Five, summary

This article records the reasons and solutions for .gitignore not taking effect, for reference.

Guess you like

Origin blog.csdn.net/xuxu_123_/article/details/131710549