track文件 什么是git_如何让顶级git跟踪另一个子目录git下的所有文件

这篇博客探讨了在Git中如何使`git ls-files -o`显示子目录下的未跟踪文件。作者发现默认情况下,Git不会追踪子目录内的文件,并提供了解决方案,包括使用`git rm --cached`和`git add`命令来重新配置跟踪。文章还提到了子模块的处理方式以及如何避免将子目录视为子模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bd96500e110b49cbb3cd949968f18be7.png

command sequence

mkdir topdir

mkdir another-git-directory

touch fileC

touch fileD

git add .

git commit -m "sub-dir init"

cd ..

touch fileA

touch fileB

git add .

git commit -m "top-dir init"

git ls-files

// now we can see that fileC and fileD is not tracked by top-level git //

git ls-files -o

// this would not show us the fileC and fileD as untracked files//

my question is: how can we make "git ls-files -o" to show the untracked files inside sub-directory? why git is behaved such, as i expect git ls-files to show all untracked file (even it is inside another sub-dir git)?

I know that I could make the top git to track the sub-dir files using "git add */."... but I am interested to know why for the question above. Thanks!

directory structure

topdir +

+-- .git

+-- fileA

+-- fileB

+ another-git-directory +-- .git

+-- fileC

+-- fileD

update (26 Jun)

I found this thread Unable to track files within Git submodules and this url (cn) http://blog.ossxp.com/2010/01/425/ which explains how to resolve the problem.

the solution:

git rm --cached another-git-directory #no trailing slash

git add another-git-directory/.

git commit

the 'git rm --cached path/to/sub-dir-or-sub-module' will tell the top-dir not to treat the sub-dir as a submodule... I think....

解决方案

A nested repo is by default untracked.

(actually it tracks the submodule root folder as a special entry in the index)

Git will detect the nested .git repo, and the parent repo will delegate any file status to the nested one.

To show them tracked, you may have to:

remove the gitlink: git rm --cached mysubmodule.

Note the lack of trailing slash: mysubmodule represents the gitlink, while mysubmodule/ is the folder created by the checked out submodule.

add it back to the index of the parent repo: git add mysubmodule (here, you can add or not a trailing slash, it does not matter)

commit the new files added and present in mysubmodule.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值