Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: rename file and change the content of the file

Tags:

git

rename

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

like image 932
Michael Aigner Avatar asked Feb 01 '16 11:02

Michael Aigner


2 Answers

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.

like image 64
mickiewicz Avatar answered Nov 01 '22 11:11

mickiewicz


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.

like image 21
hinneLinks Avatar answered Nov 01 '22 13:11

hinneLinks