Re: git mv after the fact
- From
Junio C Hamano <gitster@pobox.com>
- Date
- May 27, 2026, 03:09 UTC
- Message-ID
- <xmqqy0h5lfa0.fsf@gitster.g>
- In-Reply-To
- <CAPx1Gvd9+z0th9whCbcA60_bWproPp+kwp3qDmhQOe4G=0=E6A@mail.gmail.com>
Chris Torek <chris.torek@gmail.com> writes:
Show 7 quoted lines
> On Tue, May 26, 2026 at 6:18 AM Frieder Hannenheim <mail@fhannenheim.net> wrote: >> I'd like to propose a new flag for git mv, that updates the index >> like git mv normally would but does not move the file. ... > > You may already know this, but technically no flag is needed: > you can just "git add" the new name and "git rm" the old one, > with the same effect.
Correct.
> A flag for "git mv" would be convenient (and slightly more > efficient, not in terms of storage but in terms of CPU time > spent discovering that the contents under the new name > already exist in the object database).
May be convenient, but I do not get the "efficient" part. Do you mean that for two operations "add" and "rm", you need to spend two index writes plus one file contents hash, as opposed to one index rite without having to do any contents hash?
> But Git will discover > the rename on its own in the usual way regardless of how > you get to that point.
This is not incorrect per-se, but it is a confusing thing to say to somebody who does not know the equivalence of "mv" and "rm + add". It would not be clear to them that you are not talking about what happens during "mv" or "rm + add", but about what happend during "git log -M", "git diff -M", etc.
There is "git rm --cached" that can be used to recover from an "oops, I removed the file from the filesystem without telling Git".
$ date >new-file.txt
$ git add new-file.txt
$ rm new-file.txt
$ git rm --cached new-file.txtI think the requested "feature" is not all that outrageous. It would be a similar value as a morning-after correction measure for "oops, I moved the file in the filesystem without telling Git".
$ date >old-file.txt
$ git add old-file.txt
$ mv old-file.txt new-file.txt
$ git mv --cached old-file.txt new-file.txtThanks.