3

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.

DheltaHalo
  • 109
  • 1
  • 6

3 Answers3

1

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.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • 3
    Even that's an overstatement: an entry in a `.gitignore` file is only considered when some file is *untracked*. A committed file, as (because) it is being checked out, automatically becomes tracked. – torek May 08 '21 at 14:55
  • That's not what hes asking for. The header says .gitignore is tracked, but isn't downloaded when he clones the repo. Have the same issue here. – Pebermynte Lars Jan 03 '23 at 09:53
  • If it is committed, it will be downloaded while cloning - if you don't want that, create a commit removing those files. – dan1st Jan 03 '23 at 10:23
1

.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].

Obsidian
  • 3,719
  • 8
  • 17
  • 30
0

.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?

LeGEC
  • 46,477
  • 5
  • 57
  • 104