git笔记——jgit使用
有时需要通过Java代码连接Git库,开源的JGit是不错选择。JGit连接代码仓库通常需要鉴权,这里介绍一种使用私有Token进行鉴权连接的方法。
首先在Git库上生成私有Token,Github中生成路径为:Settings -> Developer settings -> Personal access tokens -> Generate new token,最终生成Token字符串,需要牢记,后面使用。
JGit连接Git仓库时,使用CredentialsProvider进行鉴权,私有Token使用UsernamePasswordCredentialsProvider对象进行设置,进行数据库连接即可,代码如下:
package com.king.jgit.service;
import java.io.File;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
public class JGitService {
public Git getGit(String repositoryUrl, String privateToken, String repositoryDir)
throws InvalidRemoteException, TransportException, GitAPIException {
CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider("PRIVATE-TOKEN",
privateToken);
Git git = Git.cloneRepository().setCredentialsProvider(credentialsProvider).setURI(repositoryUrl)
.setDirectory(new File(repositoryDir)).call();
return git;
}
}
其中需要注意的是,UsernamePasswordCredentialsProvider构建方法,第一个参数为用户名,这个用户名在使用私有Token 的时候,必须固定的设置为“PRIVATE-TOKEN”,第二个参数即为上面生成的Git库私有Token。如果你用的是gitlab,那么第一个参数仍然使用的是用户名。
/**
* JGit
*/
public class GitUtils {
//远程库路径
public String remotePath = "git repo address";
//下载已有仓库到本地路径
public String localPath = "";
//本地路径新建
public String initPath = localPath;
public String gitlabUserName = "";
public String gitlabPrivateToken = "";
/**
* 克隆远程库
* @throws IOException
* @throws GitAPIException
*/
public void cloneFromRemote(String path) throws IOException, GitAPIException {
//设置远程服务器上的用户名和密码
UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
UsernamePasswordCredentialsProvider(gitlabUserName,gitlabPrivateToken);
//克隆代码库命令
CloneCommand cloneCommand = Git.cloneRepository();
Git git= cloneCommand.setURI(remotePath)
.setBranch("master")
.setDirectory(new File(path))
.setCredentialsProvider(usernamePasswordCredentialsProvider)
.call();
}
/**
* 本地新建仓库
*/
public void createRepo(String path) throws IOException {
//本地新建仓库地址
Repository newRepo = FileRepositoryBuilder.create(new File(path + "/.git"));
newRepo.create();
}
/**
* 本地仓库新增文件
*/
public void add(File file) throws IOException, GitAPIException {
//git仓库地址
Git git = new Git(new FileRepository(localPath+"/.git"));
//添加文件
git.add().addFilepattern(file.getName()).call();
}
/**
* 本地提交代码
*/
public void commit() throws IOException, GitAPIException,
JGitInternalException {
//git仓库地址
Git git = new Git(new FileRepository(localPath+"/.git"));
//提交代码
git.commit().setMessage("test jGit").call();
}
/**
* 拉取远程仓库内容到本地
*/
public void pullFromRemoteMaster(String path) throws IOException, GitAPIException {
UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
UsernamePasswordCredentialsProvider(gitlabUserName,gitlabPrivateToken);
//git仓库地址
Git git = new Git(new FileRepository(path+"/.git"));
git.pull().setRemoteBranchName("master").
setCredentialsProvider(usernamePasswordCredentialsProvider).call();
}
/**
* push本地代码到远程仓库地址
*/
public void push() throws IOException, JGitInternalException,
GitAPIException {
UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
UsernamePasswordCredentialsProvider(gitlabUserName,gitlabPrivateToken);
//git仓库地址
Git git = new Git(new FileRepository(localPath+"/.git"));
git.push().setRemote("origin"). setCredentialsProvider(usernamePasswordCredentialsProvider).call();
}
}
git丢弃本地仓库修改,强制远程更新
开发时,对于本地的项目中修改不做保存操作(或代码改崩),可以用到Git pull的强制覆盖,具体代码如下:
git fetch --all
git reset --hard origin/master
git pull //可以省略
1、git fetch 指令是下载远程仓库最新内容,不做合并
2、git reset 指令把HEAD指向master最新版本