9

I have a question about renaming files in git.

I want to rename a file. So I rename it. As far as i understand if I don't change the content of the file git will detect the move because the hash of the file is the same.

I'm using C++, so after the rename I have to change the include path in the file (which I have renamed), so the problem is that git does not detect the move, but instead thinks that one file was deleted and the other added. I'm looking for an solution where git detects this change.

Does anybody has a good strategy for that?

Thanks in advance
Tonka

Vasfed
  • 18,013
  • 10
  • 47
  • 53
Michael Aigner
  • 4,820
  • 4
  • 21
  • 33
  • 1
    the offical documentation says: Git has a rename command git mv, but that is just a convenience. The effect is indistinguishable from removing the file and adding another with different name and the same content – Michael Aigner Feb 01 '16 at 11:26

2 Answers2

3

If you want to have two different commits, one with move and second one with file changes, you should use git mv and commit. Then change your include path and do another commit with inside file changes.

mickiewicz
  • 279
  • 2
  • 13
  • 1
    will an interactive rebase (squash) be possible, so that git know it after the squash? – Michael Aigner Feb 01 '16 at 13:51
  • I'm not sure what you mean but if you are asking if squash is possible then yes, it's possible just run `git rebase -i HEAD~2` and change `pick` to `squash` in the proper commit. – mickiewicz Feb 01 '16 at 14:19
1

I'm using Java, with Java-Classes its the same, the contents change if you rename/move it (package declaration, Class-Name).

Without doing something special, git recognized the rename, i can see the full history of the file.

In the commit, where i moved the file, i can see this (via eGit History in Eclipse):

diff --git a/Project/src/de/package/app/DebugInfo.java b/Project/src/de/package/app/development/DebugInfo.java
similarity index 88%
rename from Project/src/de/package/app/DebugInfo.java
rename to Project/src/de/package/app/development/DebugInfo.java
index 46494da..f90778e 100644 

You can also adjust the Similarity Index, so git might recognize renamed files earlier.

Community
  • 1
  • 1
hinneLinks
  • 3,673
  • 26
  • 40