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

8.6k views Asked by At

Let's say I have this worktree that has already been tracked by git.

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

I want to mark every tracked files inside parentso that any changes made to them woudln't be tracked by git anymore, which in the end would include somefile.txt, anotherfile.txt, file1.txt, file2.txt, file3.txt.

.gitignore only works on untracked files so I'm using --skip-worktree.

I tried:

1.

git update-index --skip-worktree parent/

Gave me Ignoring path parent/ but when I check git ls-files -v, it still shows me that the files I want to skip are still tracked.

2.

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

It gave me fatal: Unable to mark file parent/child1 so it cannot mark a directory and anything it contains.

How can I do this without manually add every file I have inside parent?

1

There are 1 answers

2
VonC On BEST ANSWER

Similarly to assume-unchanged, you need to apply this command to all files within a folder:

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

(there is no "recursive" option to git update-index)

EDITED:

Don't forget to cd into the directory that you want to skip first, then run the above commands.

And, if you want to undo it, just do the opposites with this:

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