是否可以使用 JGit 将文件提交到远程基础存储库

Is it possible to commit a file to a remote base repository using JGit

我的要求是我想以编程方式将文件提交到远程基础存储库(位于中央位置,如 https//:myproject.git)。

我想知道是否可以在不将基本存储库克隆到我的本地计算机的情况下将文件提交到远程基本存储库(master)。我是 JGit 的新手。请告诉我。

正如 @larsks 已经指出的那样,您需要首先创建远程基础存储库的本地克隆。更改只能提交到基础存储库的本地副本。最后,通过推送到原始存储库,本地更改可以在远程存储库上供其他人使用。

JGit 有一个 Command API 模仿 Git 命令行,可用于克隆、提交、并推动。

例如:

// clone base repository into a local directory
Git git Git.cloneRepository().setURI( "https://..." ).setDirectory( new File( "/path/to/local/copy/of/repo" ) ).call();
// create, modify, delete files in the repository's working directory,
// that is located at git.getRepository().getWorkTree()
// add and new and changed files to the staging area
git.add().addFilepattern( "." ).call();
// remove deleted files from the staging area
git.rm().addFilepattern( "" ).call();
// commit changes to the local repository
git.commit().setMessage( "..." ).call();
// push new commits to the base repository
git.push().setRemote( "http://..." ).setRefspec( new Refspec( "refs/heads/*:refs/remotes/origin/*" ) ).call();

上面例子中的PushCommand明确说明了推送到哪个远程和更新哪个分支。在许多情况下,省略 setter 并让命令使用 git.push().call().

从存储库配置中读取合适的默认值可能就足够了

如需了解更多信息,您可能需要查看一些更详细地介绍 cloning, making local changes, and other aspects like authentication and setting up the development environment

的文章