Java 操作 git

该博客介绍了如何使用JGit Java库进行Git操作,包括克隆远程仓库、创建本地仓库、添加文件、提交代码、拉取远程更新和推送本地更改。示例代码详细展示了每个操作的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.sf.sgs.smp.manager.test;

import org.eclipse.jgit.api.*;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**

  • JGit API测试
    */
    public class gitTest {

    public String remotePath = “http://user@10.1.2.1:8080/project.git”;//远程库路径
    public String localPath = “D:\project\”;//下载已有仓库到本地路径
    public String initPath = “D:\test\”;//本地路径新建

    /**

    • 克隆远程库

    • @throws IOException

    • @throws GitAPIException
      */
      @Test
      public void testClone() throws IOException, GitAPIException {
      //设置远程服务器上的用户名和密码
      UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
      UsernamePasswordCredentialsProvider(“username”,“password”);

      //克隆代码库命令
      CloneCommand cloneCommand = Git.cloneRepository();

      Git git= cloneCommand.setURI(remotePath) //设置远程URI
      .setBranch(“master”) //设置clone下来的分支
      .setDirectory(new File(localPath)) //设置下载存放路径
      .setCredentialsProvider(usernamePasswordCredentialsProvider) //设置权限验证
      .call();

      System.out.print(git.tag());
      }

    /**

    • 本地新建仓库
      */
      @Test
      public void testCreate() throws IOException {
      //本地新建仓库地址
      Repository newRepo = FileRepositoryBuilder.create(new File(initPath + “/.git”));
      newRepo.create();
      }

    /**

    • 本地仓库新增文件
      */
      @Test
      public void testAdd() throws IOException, GitAPIException {
      File myfile = new File(localPath + “/myfile.txt”);
      myfile.createNewFile();
      //git仓库地址
      Git git = new Git(new FileRepository(localPath+"/.git"));

      //添加文件
      git.add().addFilepattern(“myfile”).call();
      }

    /**

    • 本地提交代码
      */
      @Test
      public void testCommit() throws IOException, GitAPIException,
      JGitInternalException {
      //git仓库地址
      Git git = new Git(new FileRepository(localPath+"/.git"));
      //提交代码
      git.commit().setMessage(“test jGit”).call();
      }

    /**

    • 拉取远程仓库内容到本地
      */
      @Test
      public void testPull() throws IOException, GitAPIException {

      UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
      UsernamePasswordCredentialsProvider(“username”,“password”);
      //git仓库地址
      Git git = new Git(new FileRepository(localPath+"/.git"));
      git.pull().setRemoteBranchName(“master”).
      setCredentialsProvider(usernamePasswordCredentialsProvider).call();
      }

    /**

    • push本地代码到远程仓库地址
      */
      @Test
      public void testPush() throws IOException, JGitInternalException,
      GitAPIException {

      UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
      UsernamePasswordCredentialsProvider(“username”,“password”);
      //git仓库地址
      Git git = new Git(new FileRepository(localPath+"/.git"));
      git.push().setRemote(“origin”). setCredentialsProvider(usernamePasswordCredentialsProvider).call();
      最后记得删除本地文件(这里就不提供代码了。谢谢)
      }
      }

maven引入依赖:

   <dependency>
        <groupId>org.eclipse.jgit</groupId>
        <artifactId>org.eclipse.jgit</artifactId>
        <version>5.1.3.201810200350-r</version>
        <optional>true</optional>
    </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值