10

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

1
  • 2
    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 Commented Feb 1, 2016 at 11:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

will an interactive rebase (squash) be possible, so that git know it after the squash?
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.
Squash is possible but useless. After squash you get the same result (deleted and added).
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.

2 Comments

i try to call diff --git in the my terminal, but i get an unknow option -- git error
This is the output of the Eclipse EGit Plugin, i dont know which git command is behind it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.