I've renamed a few files in my git repo, but I forgot to use git mv
to rename those files. I'd like to make my commit reflect the renamed files, instead of showing a deletion and an addition to the repo. Is it possible to do something like that?
Or would I have to create a kind of macro that uses mv
to move the file back to the original name, and then git mv
to rename it once again, like so:
function fixmv() { mv $2 $1 && git mv $1 $2 }
If there is a built-in way to do this using git, I'd love to hear.
About three years ago, I wrotea script to do this — git-mv-after
, to be invoked as git mv-after
a la hg mv --after
—but all you really have to do is run git add old-name new-name
.
(My script is extra-fancy as it does a bunch of checks to make sure that the rename is safe.)
I do think git mv
should include a --after
flag, so that Git isn't deliberately dumber than Mercurial here. :-) But it is pretty minor.
Yes; the easy way is to run git add new-name
and git rm --cached old-name
. git doesn't track renames specifically; they're just a deletion from one name and an add of the same content at a new name.
git rm --cached
tells it not to complain about the fact that the file you're asking it to remove doesn't exist.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.