11

Due to the tools I usually use, I often rename a file, and make changes to it, before getting ready to commit.

At this point, git no longer detects the file as a simple rename, because the file contents are different.

Is there a command I can use to specify that some file, is actually also a rename of another file, even though the contents appear different?

Robert Christ
  • 1,910
  • 2
  • 13
  • 19
  • Did you use `git mv` to rename? `git status` should in this case tell you that you renamend and modified the file. – Jan Mar 04 '15 at 16:21
  • possible duplicate of [How does git detect similar files, for its rename detection?](http://stackoverflow.com/questions/7938582/how-does-git-detect-similar-files-for-its-rename-detection) – Edward Thomson Mar 04 '15 at 16:43
  • @Jan No, I'm usually renaming files through other tools, not git. Hence why I was hoping there was a command so I could retroactively tell the server that a rename occurred that it didn't detect. – Robert Christ Mar 04 '15 at 16:52

3 Answers3

7

Not in Git. Git uses similarity-based heuristic rename detection (you might need to enable it for viewing changes: git diff -M, or git config diff.renames true to always use it (or git diff -C and git config diff.renames copies to also detect copies). It detects renames on-the-fly.

It doesn't do rename tracking, and does not store information about renames anywhere.


That's assuming that you did not forget to include renamed file in the commit, i.e. you used git mv <old> <new>... which is just mv <old> <new> && git add <new>.

This means that if you have renamed file using other tools (e.g. IDE, filemanager, etc.), then you need to git add new file (after rename), and delete old file if it exists (git rm to remove it entirely, git rm --cached to keep it in the working directory).

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
3

For git, you should never really rename and make file changes in the same commit. you should run a git mv oldName newName to ensure that git knows that it was a simple move of a file, then commit your changes.

Ryen Nelsen
  • 367
  • 1
  • 9
0

The safest way is to run git mv old new and commit the change (not just staging). Though this might break your build. And then make the new changes.

There is very nice blog and a link to discussion(rough) from git creator about this topic here https://coderwall.com/p/_csouq/renaming-and-changing-files-in-git-without-losing-history

Milind Dumbare
  • 3,104
  • 2
  • 19
  • 32