threads / discuss / 60738

Make git ls-files omit deleted files

Subject: Make git ls-files omit deleted files

## tl;dr

5 messages between Jan 12, 2024 and Jan 16, 2024.

replies: 4people: 3as markdown or json

Raul Rangel· Jan 12, 2024, 21:19 UTC · lore

Hello, I'm trying to copy my current git worktree to a new directory, while including all modified and untracked files, but excluding any ignored files. I essentially want a copy of the tree assuming someone ran `git add --all && git commit` and cloned that commit into a different directory.

I got pretty close with the following command:
    $ git ls-files --cached --modified --others --exclude-standard
--deduplicate | xargs cp --parents --no-dereference
--target-directory=/tmp/clone/

The problem is that if I have an unstaged delete, `ls-files` will print it out as a modification and `cp` will print an error saying that it failed to copy the non-existing file.

The man page states this behavior:
> --modified: Show files with an unstaged modification (note that an unstaged deletion also counts as an unstaged modification)

Is there another way to achieve what I'm doing that's not too expensive? If not, could a `--no-deleted-as-modified` flag be added?

Thanks, Raul

Junio C Hamano· Jan 12, 2024, 21:37 UTC · re: Raul Rangel · lore

Re: Make git ls-files omit deleted files

Raul Rangel <rrangel@google.com> writes:
> I'm trying to copy my current git worktree to a new directory, while
> including all modified and untracked files, but excluding any ignored
> files.

Curiously missing from the above is "unmodified". You only talked about modified, untracked, and ignored, but what do you want to do with them?

As you are grabbing the files from the working tree, I suspect that you do not want to base your decision on what is in the index, which means that ls-files might be a wrong tool for the job.

Raul Rangel· Jan 12, 2024, 21:50 UTC · re: Junio C Hamano · lore

Re: Make git ls-files omit deleted files

On Fri, Jan 12, 2024 at 2:37 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 10 quoted lines
>
> Raul Rangel <rrangel@google.com> writes:
>
> > I'm trying to copy my current git worktree to a new directory, while
> > including all modified and untracked files, but excluding any ignored
> > files.
>
> Curiously missing from the above is "unmodified".  You only talked
> about modified, untracked, and ignored, but what do you want to do
> with them?

I guess another way of saying it is, I want to make a copy of the worktree while honoring .gitignore. i.e., I don't want my copied work tree to contain any ignored files.

>
> As you are grabbing the files from the working tree, I suspect that
> you do not want to base your decision on what is in the index, which
> means that ls-files might be a wrong tool for the job.
Hrmm, that makes sense. Do you have any recommendations?
rsbecker@nexbridge.com· Jan 12, 2024, 22:18 UTC · re: Junio C Hamano · lore

RE: Make git ls-files omit deleted files

On Friday, January 12, 2024 4:37 PM, Junio C Hamano wrote:
Show 7 quoted lines
>Raul Rangel <rrangel@google.com> writes:
>
>> I'm trying to copy my current git worktree to a new directory, while
>> including all modified and untracked files, but excluding any ignored
>> files.
>
>Curiously missing from the above is "unmodified".  You only talked about
modified,
>untracked, and ignored, but what do you want to do with them?
>
>As you are grabbing the files from the working tree, I suspect that you do
not want
>to base your decision on what is in the index, which means that ls-files
might be a
>wrong tool for the job.

Coupled with ls-files, using git status --porcelain might give some insight into what the condition of each modified and untracked file/directory is.

Raul Rangel· Jan 16, 2024, 21:22 UTC · re: Junio C Hamano · lore

Re: Make git ls-files omit deleted files

On Fri, Jan 12, 2024 at 2:37 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 14 quoted lines
>
> Raul Rangel <rrangel@google.com> writes:
>
> > I'm trying to copy my current git worktree to a new directory, while
> > including all modified and untracked files, but excluding any ignored
> > files.
>
> Curiously missing from the above is "unmodified".  You only talked
> about modified, untracked, and ignored, but what do you want to do
> with them?
>
> As you are grabbing the files from the working tree, I suspect that
> you do not want to base your decision on what is in the index, which
> means that ls-files might be a wrong tool for the job.

Thanks for this insight. I ended up using `git ls-files` and `--ignored` to print out all the files that I want to ignore, and I converted that into a `find` exclusion list.

Here is the final command: $ git ls-files --others --ignored --exclude-standard --directory | (printf "%s\n" \( -path ./.git; sed -e 's/^/.\//' -e 's/\/$//' -e 's/^/-o\n-path\n/'; printf "%s\n" \) -prune -o ! -type d -print0 ) | xargs --exit find . | xargs -0 --max-procs 0 cp --parents --no-dereference -t /tmp/clone

This is actually a lot faster than I would have expected. Parallelizing the `cp` cuts the time down dramatically.

Thanks again!
p.s., Sorry for the resend. I forgot to send a plain text to the mailing list.

← back to recent threads