JGit 支持推送

JGit is a pure Java library implementing the Git version control system. It is an Eclipse project and started out as the Git library for EGit, which provides a Git integration into the Eclipse IDE.

A Guide to JGit
JGit - Tutorial
JGit User Guide

基礎(chǔ)的流程包括:clone肛跌、更新內(nèi)容艺配、提交commit、推送惋砂。教程通常缺少最后一步:推送更新到遠(yuǎn)端妒挎。

使用用戶名、密碼方式——

   Git git = Git.open(localPath); 

    // add remote repo:
    RemoteAddCommand remoteAddCommand = git.remoteAdd();
    remoteAddCommand.setName("origin");
    remoteAddCommand.setUri(new URIish(httpUrl));
    // you can add more settings here if needed
    remoteAddCommand.call();

    // push to remote:
    PushCommand pushCommand = git.push();
    pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("username", "password"));
    // you can add more settings here if needed
    pushCommand.call();    

針對github可優(yōu)化未token方式西饵,從github tokens這里新建一個自己的token酝掩,然后上面的Credential Provider可以直接使用new UsernamePasswordCredentialsProvider("token", ""即可。

上面的內(nèi)容親測可用眷柔。

GitHub uses password authentication for https URLs and public-key authentication for SSH URLs


使用public key時需要注意:Windows環(huán)境下生成的私鑰默認(rèn)無法識別(因為head行不兼容期虾,參考這里)使用ssh-keygen -p -f file -m pem -P passphrase -N passphrase進(jìn)行重置(如果創(chuàng)建時passphrase沒有設(shè)置,使用""代替即可)

JGit Authentication Explained這里提到了各個不同協(xié)議使用的區(qū)別驯嘱。使用SSH的方式暫未驗證镶苞。

org.eclipse.jgit.test官方測試庫里有對應(yīng)的代碼。

通常使用的兩種方式——

  • https://github.com/username/project.git需要使用用戶名+密碼登錄
  • git@github.com:username/project.git表示SSH方式登錄

這里對各個協(xié)議類型做了詳細(xì)的說明鞠评。備份如下——


Primary differences between SSH and HTTPS. This post is specifically about accessing Git repositories on GitHub.

Protocols to choose from when cloning:

plain Git, aka git://github.com/

  • Does not add security beyond what Git itself provides. The server is not verified.

    If you clone a repository over git://, you should check if the latest commit's hash is correct.

  • You cannot push over it. (But see "Mixing protocols" below.)

HTTPS, aka https://github.com/

  • HTTPS will always verify the server automatically, using certificate authorities.

  • (On the other hand, in the past years several certificate authorities have been broken into, and many people consider them not secure enough. Also, some important HTTPS security enhancements are only available in web browsers, but not in Git.)

  • Uses password authentication for pushing, and still allows anonymous pull.

  • Downside: You have to enter your GitHub password every time you push. Git can remember passwords for a few minutes, but you need to be careful when storing the password permanently – since it can be used to change anything in your GitHub account.

  • If you have two-factor authentication enabled, you will have to use a personal access token instead of your regular password.

  • HTTPS works practically everywhere, even in places which block SSH and plain-Git protocols. In some cases, it can even be a little faster than SSH, especially over high-latency connections.

HTTP, aka http://github.com/

  • Doesn't work with GitHub anymore, but is offered by some other Git hosts.

  • Works practically everywhere, like HTTPS.

  • But does not provide any security – the connection is plain-text.

SSH, aka git@github.com: or ssh://git@github.com/

  • Uses public-key authentication. You have to generate a keypair (or "public key"), then add it to your GitHub account.

  • Using keys is more secure than passwords, since you can add many to the same account (for example, a key for every computer you use GitHub from). The private keys on your computer can be protected with passphrases.

  • On the other hand, since you do not use the password, GitHub does not require two-factor auth codes either – so whoever obtains your private key can push to your repositories without needing the code generator device.

  • However, the keys only allow pushing/pulling, but not editing account details. If you lose the private key (or if it gets stolen), you can just remove it from your GitHub account.

  • A minor downside is that authentication is needed for all connections, so you always need a GitHub account – even to pull or clone.

  • You also need to carefully verify the server's fingerprint when connecting for the first time. Many people skip that and just type "yes", which is insecure.

  • (Note: This description is about GitHub. On personal servers, SSH can use passwords, anonymous access, or various other mechanisms.)

Mixing protocols

Globally

You can clone everything over git://, but tell Git to push over HTTPS.

[url "https://github.com/"]
    pushInsteadOf = git://github.com/

Likewise, if you want to clone over git:// or HTTPS, but push over SSH:

[url "git@github.com:"]
    pushInsteadOf = git://github.com/
    pushInsteadOf = https://github.com/

These go to your git config file – sometimes ~/.config/git/config, or ~/.gitconfig, or just run git config --edit --global.

Per-repository

You can also set different pull and push URLs for every remote separately, by changing <code>remote.<i>name</i>.pushUrl</code> in the repository's own .git/config:

[remote "origin"]
    url = git://nullroute.eu.org/~grawity/rwho.git
    pushUrl = ssh://sine/pub/git/rwho.git
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末窃植,一起剝皮案震驚了整個濱河市亭罪,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖侥涵,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件借跪,死亡現(xiàn)場離奇詭異胚宦,居然都是意外死亡益兄,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門抖棘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來茂腥,“玉大人狸涌,你說我怎么就攤上這事∽罡冢” “怎么了帕胆?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長般渡。 經(jīng)常有香客問我惶楼,道長,這世上最難降的妖魔是什么诊杆? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮何陆,結(jié)果婚禮上晨汹,老公的妹妹穿的比我還像新娘。我一直安慰自己贷盲,他們只是感情好淘这,可當(dāng)我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著巩剖,像睡著了一般铝穷。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上佳魔,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天曙聂,我揣著相機與錄音,去河邊找鬼鞠鲜。 笑死宁脊,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的贤姆。 我是一名探鬼主播榆苞,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼霞捡!你這毒婦竟也來了坐漏?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤碧信,失蹤者是張志新(化名)和其女友劉穎赊琳,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體音婶,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡慨畸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了衣式。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片寸士。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡檐什,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出弱卡,到底是詐尸還是另有隱情乃正,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布婶博,位于F島的核電站瓮具,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏凡人。R本人自食惡果不足惜名党,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望挠轴。 院中可真熱鬧传睹,春花似錦、人聲如沸岸晦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽启上。三九已至邢隧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間冈在,已是汗流浹背倒慧。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留包券,地道東北人迫靖。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像兴使,于是被迫代替她去往敵國和親系宜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容