How do I rename a file in JGit

2019-02-24 16:26发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-02-24 16:59

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, in 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.

Note that paths given to addFilePattern() are relative to the work directory and path segments must always be separated by forward-slashes (/) independant of the file system in use.

查看更多
登录 后发表回答