是否可以使用JGit将文件提交到远程基础存储库 [英] Is it possible to commit a file to a remote base repository using JGit

查看:764
本文介绍了是否可以使用JGit将文件提交到远程基础存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想知道是否可以在不将基本仓库克隆到本地机器的情况下将文件提交到远程仓库(主仓库)。我是JGit的新手。请让我知道。

解决方案

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



JGit有一个 Command API



例如:

< pre-class =lang-java prettyprint-override> //将基础仓库复制到本地目录
Git git Git.cloneRepository()。setURI(https:// ... ).setDirectory(new File(/ path / to / local / copy / of / repo)).call();
//创建,修改或删除存储库工作目录中的文件
//位于git.getRepository()。getWorkTree()
// //添加新文件并将其更改为暂存区
git.add()。addFilepattern(。).call();
//从暂存区域删除已删除的文件
git.rm()。addFilepattern().call();
//将更改提交到本地存储库
git.commit()。setMessage(...).call();
//将新提交到基本仓库
git.push()。setRemote(http:// ...).setRefspec(new Refspec(refs / heads / *:refs / remotes / origin / *)).call();

以上示例中的 PushCommand 显式声明远程推送到哪个分支来更新。在许多情况下,忽略setter并让命令通过 git.push()。call()

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

有关更多信息,您可能需要查看一些关于克隆进行本地更改,以及其他方面,如身份验证设置开发环境


My requirement is that I want to programmatically commit a file to a remote base repository (that resides in a central location like https//:myproject.git).

I would like to know if it is possible to commit a file to a remote base repository (master) without cloning the base repo into my local machine. I am a newbie to JGit. Please let me know.

解决方案

As @larsks already pointed out, you need to first create a local clone of the remote base repository. Changes can only be committed to the local copy of the base repository. Finally, by pushing to the originating repository, the local changes are made available on the remote repository for others.

JGit has a Command API that is modeled after the Git command line and can be used to clone, commit, and push.

For example:

// 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();

The PushCommand in the above example explicitly states which remote to push to and which branches to update. In many cases, it may be sufficient to omit the setter and let the command read suitable defaults from the repository configuration with git.push().call().

For further information, you may want to have a look at some articles that go into more detail of cloning, making local changes, and other aspects like authentication and setting up the development environment

这篇关于是否可以使用JGit将文件提交到远程基础存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆