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

kuc*_*tam 12 git git-skip-worktree

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

.
??? parent
?   ??? child1
?   |    ??? file1.txt
?   ??? child2
?   |    ??? file2.txt
?   |    ??? grandchild
?   |         ??? file3.txt
?   ??? somefile.txt
?   ??? anotherfile.txt
|
Run Code Online (Sandbox Code Playgroud)

我想标记其中的每个跟踪文件,parent以便 git 不再跟踪对它们所做的任何更改,最终将包括somefile.txt, anotherfile.txt, file1.txt, file2.txt, file3.txt.

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

我试过:

1.

git update-index --skip-worktree parent/
Run Code Online (Sandbox Code Playgroud)

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

2.

git update-index --skip-worktree parent/*
Run Code Online (Sandbox Code Playgroud)

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

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

Von*_*onC 8

与假设未更改类似,您需要将此命令应用于文件夹中的所有文件:

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

(没有“递归”选项git update-index

编辑:

不要忘记先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" \;
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,您的命令有效。虽然我也试过`git ls-files -z parent/ | xargs -0 git update-index --skip-worktree` 基于该问题,它也可以工作,但命令较少,这很好。使用它有什么区别或缺点吗?抱歉,我在终端方面不是那么精通。 (2认同)