Home git Files in .gitignore are not ignored

Files in .gitignore are not ignored

Author

Date

Category

I am reading the book Test-Driven Development with Python, the second chapter describes how to create a git repository. I follow all the instructions, but my .gitignore file does not ignore what it says. What’s the problem?

pic


1, authority 100%

I suspect you added files to the index before you started to ignore them. That is, before that git add. , which is in the screenshot, there was another such command. To find out exactly what is missing is git status before adding.

If the file has already been added, then the change to .gitignore does not cause deletion from the current index (which is logical and safe).

If files have just been added, but not yet included in the commit

In this particular case, this is exactly the case. In this case, it is enough to remove them from the index. This command returns the index to HEAD, which is the state of the last commit.

git reset & lt; file-name & gt;

You may notice that Git itself suggests using a different command:

git rm --cached & lt; file-name & gt;

In this case these commands are equivalent. This duplication is a result of the evolutionary development of Git functionality. This is also reflected in the documentation and in the Git posts: somewhere it is recommended reset , somewhere rm --cached .

If ignored files already exist in the last commit

Such a situation is also possible, just in case I will describe it as well. Here reset won’t work, you need rm . The --cached argument tells Git to remove the file from the index, but not touch the workspace. That is, it literally indexes the deletion of the file, even though there was no such deletion. If you do this with a file that is not ignored, then after the commit it will be in the untracked category.

git rm --cached & lt; file-name & gt;

If you need to remove an entire ignored folder, add the -r switch:

git rm -r --cached & lt; path & gt;

This handy command applies rm to all files specified in .gitignore :

git rm --cached `git ls-files -i --exclude-from = .gitignore`

a variation of the same for windows powershell :

foreach ($ i in iex 'git ls-files -i --exclude-from = .gitignore') {git rm --cached $ i}

Now the output of the git rm command needs to be committed with a commit.

git commit -m'removed gitignored files'

2, authority 15%

Command

git rm --cached `git ls-files -i --exclude-from = .gitignore`

has problems with spaces in titles.

The easiest way is to delete ALL files from the cache, and add them again with gitignore. for everything about all 2 commands.

git rm -rf --cached.
git add.

then commit:

git commit -m "fix gitignore"

3

git filter-branch --force (to keep the directory from being deleted from the hard drive)
git filter-branch --tree-filter "rm -rf PATH" HEAD

where PATH is the path to the file / folder

https://toster.com/q/176843

https://help.github.com / en / articles / removing-sensitive-data-from-a-repository

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions