今天为一个旧的git repository添加了“.gitignore”,但发现gitignore看起来没有生效?然后就必应,然后复制粘贴,有了下面这么一大段。
TL;DR
Step 1. Type the following command and hit Enter to execute it. This command will unstage and remove the path to your files from the Git index.
1 | git rm -r --cached . |
Step 2. Execute the following command. It will re-add all your files back and only the correct files will be updated.
1 | git add . |
Step 3. Execute the following command to commit all your files back into the Git index.
1 | git commit -m ".gitignore is now working" |
After that, you can test the files and see if the git ignore not working issue is fixed or not.
One reason why .gitignore might not work
In order to fix the .gitignore not working issue, it is necessary to have an overall understanding of gitignore. This issue often occurs when the .gitignore files can’t work in Git. Git can view every file in your working directory that categories the files into the following 3 types:
- Tracked: These files are previously either committed or staged in history.
- Untracked: These files haven’t been staged or committed previously.
- Ignored: They are the files that can be set by users to ignore completely.
To make .gitignore work, you have to ensure that the file should not be part of the repository yet. The file won’t be ignored once it is already added to the repository. Even if you have changed its name or rule in the .gitignore file, Git can’t ignore it. This is because Git can only ignore the untracked files.
If you want to ignore the file that you have already added rules into .gitignore, you can re-add the file. That means you will delete everything from the Git index and then re-add everything back into your repository. So, we recommended you back up your codes somewhere in advance and follow the steps below.
Ignoring a Previously Committed Files
The files in your working copy can be either tracked or untracked.
To ignore a file that has been previously committed, you’ll need to unstage and remove the file from the index, and then add a rule for the file in .gitignore
:
1 | git rm --cached filename |
The --cached
option tells git not to delete the file from the working tree but only to remove it from the index.
To recursively remove a directory, use the -r
option:
1 | git rm -r --cached directoryname |
If you want to remove the file from both the index and local filesystem, omit the --cached
option.
When recursively deleting files, use the -n
option that will perform a “dry run” and show you what files will be deleted:1
git rm -r -n directory
Debugging .gitignore File
Sometimes it can be challenging to determine why a specific file is being ignored, especially when you’re are using multiple .gitignore files or complex patterns. This is where the git check-ignore
command with the -v
option, which tells git to display details about the matching pattern, comes handy.
For example, to check why the www/yarn.lock
file is ignored you would run:1
git check-ignore -v www/yarn.lock
The output shows the path to the gitignore file, the number of the matching line, and the actual pattern.1
www/.gitignore:31:/yarn.lock www/yarn.lock
The command also accepts more than one filename as arguments, and the file doesn’t have to exist in your working tree.
git ls-files
When accepting a new code drop for a vendor branch, you probably want to record both the removal of paths and additions of new paths as well as modifications of existing paths.
Typically you would first remove all tracked files from the working tree using this command:
1 | git ls-files -z | xargs -0 rm -f |