How do I rename a file in JGit

孤人 提交于 2019-12-09 03:39:06

问题


How do I rename a file in JGit. That is, given a working file named file1.

The command line would be:

git mv file1 file2

回答1:


There is no direct equivalent to git mv in Git. git mv is just a short hand for

mv oldname newname
git add newname
git rm oldname

(see here)

Respectively, use File.renameTo() or, since Java 7, Files.move() to move the file and then

git.add().addFilepattern( "newname" ).call();
git.rm().addFilepattern( "oldname" ).call();

to update the Git index.

The paths given to addFilePattern() must be relative to the work directory and path segments must always be separated by slashes (/) independent of the file system in use.

Note, that Git does not track renames or moves. When using the --follow option with git log, it uses heuristics to try to detect renamed or moved files (see Is it possible to move/rename files in Git and maintain their history?)



来源:https://stackoverflow.com/questions/20502431/how-do-i-rename-a-file-in-jgit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!