简体   繁体   中英

Git clone ignoring .gitignore file

my problem is that when I try to do the following command on my terminal:

git clone https://github.com/DheltaHalo/test.git

It also clones the files that are supposed to be ignored by.gitignore.

I've read that if, for example, I have written in my.gitignore file *.txt it should ignore all .txt files, but when I clone it to my desktop it downloads those files as well.

The .gitignore is considered only when adding (staging) files that are not already staged (except with git add -f ).

The process of cloning ignores the .gitignore file.

If a file is added, committed and another commit with the .gitignore is created, the file will still be part of the repository and therefore cloned.

.gitignore is only meant to ignore files that have not been tracked yet. If your files already are inside your repository, it's now part of history and there's no point of ignoring them at this stage, otherwise the outchecking of your revision would be incomplete.

Note that it won't work the other way neither if you add some already tracked files to .gitignore and alter them. Git won't care about new files but it will still track the ones that are already recorded. If this is what you want to do, you have to perform a "remove" (file deletion) that would apply only on the index, without touching the working directory. This can be done with: git rm --cached [file] .

.gitignore applies to new files, but if you have files that were already tracked when you added some lines in your .gitignore file, git will still track these files:
you will see them listed when they are modified, they will be pushed, pulled, and they will still appear when you clone your repo.

If you want to make git "forget" about these files, you need to run some extra steps: see for example

How to make Git "forget" about a file that was tracked but is now in.gitignore?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM