7

In Intellij, I move files between packages and it seems that git removes and re-adds the file. How do I make Intellij git mv on class movements?

David Williams
  • 8,388
  • 23
  • 83
  • 171
  • 1
    This is a bit old but I faced this problem recently. When you are using the refactoring from Intellij to move a file you will see the file as deleted and added in the Git window (or if you call `git status`). To have them as `moved` use `git add file_name`. Then both in Intellij and in Terminal, you will see `renamed` for the file. Link: https://docs.github.com/en/github/managing-files-in-a-repository/managing-files-using-the-command-line/moving-a-file-to-a-new-location-using-the-command-line – bogdan.rusu Sep 06 '21 at 11:24

2 Answers2

3

Git doesn't really have a concept of "moving a file"; the git mv command is just a shorthand for deleting and re-adding (plus actually moving the file in the file system). You can tell some of the git commands to try to detect renames/moves (by supplying a similarity threshold), but there's no way to record in the repository that file x has been moved to y.

However, a number of git commands are able to track changes across moved files, as long as the add+delete happens in the same commit. For example, git blame -C <filename> will show you the original author of each line, independently of who last moved the file.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • The issue is that Intellij issues a delete and an add, not a move. How can I make it do that, so it preservers history? – David Williams Mar 30 '15 at 23:06
  • 1
    Unless you also change the contents of the file, there's no difference between a move and a delete+add. – Aasmund Eldhuset Mar 30 '15 at 23:28
  • So why does intellij delete history when I move classes? Do you know what I mean? You know that this happens, yes? Can you please help me with the intellij aspect of this question? – David Williams Mar 30 '15 at 23:29
  • Are you talking about IntelliJ's "Local history", or the git history as viewed from IntelliJ, or the git history as viewed from the console (when using git commands directly)? – Aasmund Eldhuset Mar 30 '15 at 23:31
  • If I drag a class from one package to another, then commit my changes, in the remote repository it looks like I am the author of every line of the file. – David Williams Mar 30 '15 at 23:32
  • You _are_ the author of every line in the moved file (it's annoying, but that's how git works). `git blame` attempts to detect renamed/moved files within each commit, but if the file contents are too different, it won't work. Other ways of viewing the file might not do similarity detection, and will show you as the author of all lines. _This happens with git mv as well._ The behavior should be the same remotely and locally, though - how exactly are you determining in the remote repository that you appear as the author? – Aasmund Eldhuset Mar 30 '15 at 23:46
  • You are correct, thanks, hrees more info http://stackoverflow.com/questions/2096491/git-how-to-analyze-code-that-has-a-multi-file-history – David Williams Mar 31 '15 at 00:19
  • I'm a bit aghast here. I just want to move a package up one level (a package with a rich nested hierarchy). Losing all the history seems a high price to pay for this. – WestCoastProjects Nov 26 '18 at 22:58
  • I did other research and have added an answer reflecting it and my own failed and succsessful experience. – WestCoastProjects Nov 28 '18 at 03:36
  • @javadba: I agree that dedicating a commit to the move operation (possibly with minimal changes due to package renaming) is the safest way to go. – Aasmund Eldhuset Nov 28 '18 at 03:49
  • Your info was helpful anyways so went ahead and upvoted – WestCoastProjects Nov 28 '18 at 05:17
3

From other Q&A about git mv it seems that Linus Torvalds dropped the ball here - though he insists it is by design.

Well to get around this the best we can.. the process is to

  • Create git commit only for moving/renaming the directory

git seems able to track this using --follow and Intellij is able to figure it out as well.

Note that mixing and matching updates to files and git mv is a failed recipe - both by git and in Intellij.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560