0

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.

Joeytje50
  • 18,636
  • 15
  • 63
  • 95
  • 1
    You can do `mv` without `git mv`: you need to add both files to the index: `mv $1 $2 && git add $1 $2` – phd Apr 19 '20 at 21:39
  • Does this answer your question? [What's the purpose of git-mv?](https://stackoverflow.com/questions/1094269/whats-the-purpose-of-git-mv) – phd Apr 19 '20 at 21:41

2 Answers2

2

About three years ago, I wrote a 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.

torek
  • 448,244
  • 59
  • 642
  • 775
1

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.

hobbs
  • 223,387
  • 19
  • 210
  • 288