Java で Git のリポジトリを操作するライブラリに JGit があります:
http://www.eclipse.org/jgit/
2016082801


現在は Eclipse 参加のサブプロジェクトとして提供されています。すごく便利な反面、ちょっとクセがあります。以下、基本的な使い方を説明しますが、まずは前提をいくつか:

【前提】
(1) 対象とする Git のリモートリポジトリ URI は http://xxxgit.com/name/project.git とします。
(2) 上記リポジトリにアクセス(特にプッシュ)する際の認証は ID: username, Password: password であるとします。
(3) ローカルリポジトリ(上記リポジトリをローカルにクローンする先)のディレクトリは ./project (つまりカレントディレクトリ上に project というサブディレクトリを作る)とします。


【準備】
JGit のダウンロードページから最新版の JGit をダウンロードし、展開して JAR を取り出し、自分のプロジェクト内に(Classpath を通すなどして)用意します。

ここまで用意できれば JGit を使うことができます。以下、一通りの git 操作(clone, pull, add, commit, push)を行う様子を順に紹介します。


【git clone】
目的のリモートリポジトリ(今回の例では http://xxxgit.com/name/project.git)からコードをクローンします:
try{
  Repository localRepo = new FileRepository( "./project/.git" );
  Git git = new Git( localRepo );

  if( git != null ){
    //. git clone
    git.cloneRepository().setURI( "http://xxxgit.com/name/project.git" ).setDirectory( new File( "./project" ) ).call();
  }
}catch( Exception e ){
  e.printStackTrace();
}

以下全ての例に言えることですが、ローカルリポジトリのインスタンス変数を作る際に指定するのは、ローカルリポジトリのフォルダに "/.git" をつけたものです。

一方、クローン実行時に指定するローカルフォルダはローカルリポジトリのフォルダそのものです。この辺りがクセのある所で、知らないと混乱します。


【git pull】
クローンしたローカルリポジトリに対し、リモートリポジトリに加えられている最新の変更を反映させます:
try{
  Repository localRepo = new FileRepository( "./project/.git" );
  Git git = new Git( localRepo );

  if( git != null ){
    //. git pull
    PullCommand pc = git.pull();
    pc.call();
  }
}catch( Exception e ){
  e.printStackTrace();
}

JGit では各コマンドを実行する際には ****Command クラスの(上記例では PullCommand クラス)インスタンスを作って、必要であれば設定を加えて、call() する、という処理を実行します。

そして次の add コマンドを実行する前に、このローカルリポジトリ内のファイルに何らかの変更が加わっていることを想定してください。


【git add】
ローカルリポジトリ内のファイルシステムに対して行った変更作業をリポジトリに加えます:
try{
  Repository localRepo = new FileRepository( "./project/.git" );
  Git git = new Git( localRepo );

  if( git != null ){
    //. git add
    AddCommand ac = git.add();
    ac.addFilepattern( "." );  //. 全ての変更を git add する
    try{
      ac.call();
    }catch( NoFilepatternException e ){
      e.printStackTrace();
    }
  }
}catch( Exception e ){
  e.printStackTrace();
}

AddCommand インスタンスにファイルパターンを指定して call() する、という手順です。


【git commit】
ローカルリポジトリの変更内容をコミットします:
try{
  Repository localRepo = new FileRepository( "./project/.git" );
  Git git = new Git( localRepo );

  if( git != null ){
    //. git commit
    CommitCommand cc = git.commit();
//. コミッターの名前とメールアドレス、コミットメッセージを指定 cc.setCommitter( "commiter_name", "committer_email" ).setMessage( "Some message." ); try{ cc.call(); }catch( NoHeadException e ){ e.printStackTrace(); }catch( NoMessageException e ){ e.printStackTrace(); }catch( ConcurrentRefUpdateException e ){ e.printStackTrace(); }catch( WrongRepositoryStateException e ){ e.printStackTrace(); } } }catch( Exception e ){ e.printStackTrace(); }

コミッターの名前とメールアドレス、そしてコミットメッセージを追加した上で call() します。


【git push】

ローカルリポジトリの変更内容をリモートリポジトリに対してプッシュします:
try{
  Repository localRepo = new FileRepository( "./project/.git" );
  Git git = new Git( localRepo );

  if( git != null ){
//. git push CredentialsProvider cp = new UsernamePasswordCredentialsProvider( username, password ); PushCommand pc = git.push(); pc.setCredentialsProvider( cp ).setForce( true ).setPushAll(); } }catch( Exception e ){ e.printStackTrace(); }

認証用の usernamepassword を指定してプッシュする、という流れです。


以上、JGit を使った基本的な git コマンドの実行方法を紹介しました。クセがある、と紹介しましたが、ある程度慣れてしまえばそんなに苦ではないと思います。何よりもプログラムから git が使えると複製機能をこれで実装できたりするのですごく便利です。