git not tracking files after cp C:\folder\ -Recurse but after cp C:\folder\* -Recurse

228 Views Asked by At

Not sure I did something wrong or misunderstood one particularity in git, I have a local repo that works fine, but when I copy some contents from outside into the repo using

cp ..\folder\ -Recurse

then only the new folder is shown as 'tracked', not the files inside this folder, although the files are in the folder within the repo;

all the files become 'tracked' only when I do

cp ..\folder\* -Recurse

What did I miss?

I am using Windows 10 (20H2), with Windows Terminal (1.4.3243.0), PowerShell 7.1.0 and git version 2.28.0.windows.1


To reproduce it, please do:

mkdir test_git
cd .\test_git\
mkdir from
new-item .\from\test1.txt .\from\test2.txt .\from\test3.txt

mkdir  git1,git2

cd git1
git init -b main
cp ..\from\ -Recurse .
ls .\from\ # show the 3 files in the folder
git status

>On branch main
>
>No commits yet
>
>Untracked files:   
>  (use "git add <file>..." to include in what will be committed)
>        from/
>
>nothing added to commit but untracked files present (use "git add" to track)

Now, second case:

cd ..\git2\
git init -b main
cp ..\from\* -Recurse .
ls .\from\ # show the 3 files in the folder as well
git status

>On branch main
>
>No commits yet
>
>Untracked files:
>  (use "git add <file>..." to include in what will be committed)
>        test1.txt
>        test2.txt
>        test3.txt
>
>nothing added to commit but untracked files present (use "git add" to track)
1

There are 1 best solutions below

0
On

Copying files from outside the repo does not make them tracked; it makes them untracked. If nothing at all under a directory is tracked, then git only reports the directory in the untracked list. But if you git add one file in the directory, then that file will show as a "new file" while the other individual files will show up as "untracked". (If the directory contains subdirectories that are still untracked, their contents will not be listed, same as before; just the directory that's untracked is shown.) OTOH if you git add the directory (or all of its contents), then all of the individual files will show up as staged "new files".

All of the above are true regardless of what commands you use to do the copying. That is, it makes no difference whether you say cp -R /some/source/directory vs cp -R /some/source/directory/*. (It might change which files are copied - that's an issue of the shell expanding the wildcards. But it won't make a difference in how git behaves.)

So the question is why your observations disagree with how git definitely behaves.

Since your "how to reproduce" example uses commands that don't exist in standard environments, and your cp syntax also wouldn't work in standard environments, my guess is that you're working from memory and mis-remembering things. It's also possible that you in fact have a non-standard environment that behaves in ways nobody here will be able to predict.