Git 跳过目录及其子目录中所有跟踪文件的工作树

Git Skip Worktree on All Tracked Files inside Directory and its Subdirectories

假设我的工作树已经被 git 跟踪。

.
├── parent
│   ├── child1
│   |    └── file1.txt
│   ├── child2
│   |    ├── file2.txt
│   |    └── grandchild
│   |         └── file3.txt
│   ├── somefile.txt
│   └── anotherfile.txt
|

我想标记 parent 中的每个跟踪文件,这样对它们所做的任何更改都不会再被 git 跟踪,最终将包括 somefile.txtanotherfile.txt, file1.txt, file2.txt, file3.txt.

.gitignore 仅适用于未跟踪的文件,所以我使用 --skip-worktree.

我试过了:

1.

git update-index --skip-worktree parent/

给我 Ignoring path parent/ 但是当我检查 git ls-files -v 时,它仍然显示我想跳过的文件仍然被跟踪。

2.

git update-index --skip-worktree parent/*

它给了我 fatal: Unable to mark file parent/child1 所以它无法标记目录及其包含的任何内容。

如何在不手动添加我在 parent 中的每个文件的情况下执行此操作?

Similarly to assume-unchanged,您需要将此命令应用于文件夹中的所有文件:

find . -maxdepth 1 -type d \( ! -name . \) -exec bash -c "cd '{}' && pwd && git ls-files -z ${pwd} | xargs -0 git update-index --skip-worktree" \;

git update-index 没有 "recursive" 选项)

已编辑:

不要忘记先cd进入你想要skip的目录,然后运行上面的命令。

而且,如果您想撤消它,只需执行相反的操作即可:

find . -maxdepth 1 -type d \( ! -name . \) -exec bash -c "cd '{}' && pwd && git ls-files -z ${pwd} | xargs -0 git update-index --no-skip-worktree" \;