栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 操作Git

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 操作Git

1引用jar

 
            org.eclipse.jgit
            org.eclipse.jgit
            5.12.0.202106070339-r
        

2.代码示例:

package com.imobpay.ifeeling.web.controller;


 
 
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

@Slf4j
public class AgencyDeviceController {

  

    public static void main(String[] args) throws GitAPIException, IOException {


        
       
        //拉取代码
        String path = "D:/test2/";//本地地址
        String pathfile=path+".git";
        Git git = new Git(new FileRepository(pathfile));//链接本地地址
        git.pull().setRemoteBranchName("master").setCredentialsProvider(new UsernamePasswordCredentialsProvider("PA029002", "yuanshaohua000000"))
                .call();//拉取代码
        System.out.println("拉取代码成功");
        String filePath = "D:/test2/" + "test2.txt";
        FileWriter fw = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(filePath);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("hello3n");
            bw.write("worldn");
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        AddCommand add = git.add();
        add.addFilepattern(".").call();
        System.out.println("添加到git");

        CommitCommand commit = git.commit();

        commit.setMessage("这是测试2").call();
        System.out.println("commit提交代码成功");
        git.push()
                .setCredentialsProvider(new UsernamePasswordCredentialsProvider("PA029002", "000000"))   //身份验证
                .call();

 

    }

   
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009174.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号