Converting file (tracked with Git) from Ja

2020-06-07 04:43发布

A conversion from Java to Kotlin in Android Studio 2.3.2 (in 3.0 the same behaviour) creates a new file and deletes previous. So Git doesn't know anything about this conversion. And the git history doesn't save. In Intellij Idea everything's fine. IDE just renames file and git saves the history. How can do the same in Android Studio.

4条回答
一纸荒年 Trace。
2楼-- · 2020-06-07 05:11

Git guesses renames from added/removed file pairs, but only if these files are close enough, i.e. if the file was renamed with no or small amount of changes.

When you apply java-to-kotlin conversion usually every line of a file changes, so that git cannot find that these old and new files somehow relate to each other.

You can use the following two-stage approach instead:

  • just change the extension of .java file to .kt and commit it;
  • rename it back, apply the conversion and commit the modified .kt file.
查看更多
Juvenile、少年°
3楼-- · 2020-06-07 05:16

Git doesn't actually track renames directly; it infers them based on file add/delete pairs. I assume that Idea is running a git add when it renames, whereas Android Studio is just deleting the old files. Try running git add yourself on the new files and a git rm on the old files and Git should show them as renames.

查看更多
戒情不戒烟
4楼-- · 2020-06-07 05:20

As mentioned in the other answers, git tracks the contents of the file, not its renames. When git log is run with --follow option, it shows history beyond renames, however it considers a file to be renamed only if the previous and current file contents have a similarity index of 50% or more, i.e. less than half the lines of the file have changed.

For this case, where most of the lines have changed, you may set a lower bar for the similarity index using the -M option:

git log -M20% --follow -- /path/to/file

Depending on the case, you may need to go even lower than 20%.

查看更多
啃猪蹄的小仙女
5楼-- · 2020-06-07 05:23

In case this might help a future reader:

If you use the Git commit dialog integrated with IntelliJ (Commit via Ctrl+K), there is a checkbox on the right in recent versions: ☑ Extra commit for .java > .kt renames

Submitting the dialog this way will create two commits, the first one being just .java files renamed into .kt files with no content changes. This helps Git track the content.

查看更多
登录 后发表回答