mirror of
https://github.com/wahyd4/wahyd4.github.com.git
synced 2026-08-19 02:25:56 +10:00
Rebuild blog source from GitHub Pages artifact
- 308 posts reverse-extracted from wahyd4.github.com (Hexo 6.3.0 + NexT 8.25) - Hexo project with NexT theme, reading-experience custom styles - publish-post.sh: write post -> hexo build -> PR (master untouched)
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
---
|
||||
title: "JAVA创建git仓库"
|
||||
date: 2012-07-04 00:00:00
|
||||
tags: [java, 入门, git, jgit]
|
||||
---
|
||||
|
||||
[](/images/2012/07/logo.png)
|
||||
|
||||
我们通常使用git都是使用的git的客户端,比如linux下的脚本。其实windows下也是间接使用linux脚本来进行操作。不过在现在有完全使用java语言实现的git:[JGIT](http://www.eclipse.org/jgit/ "jgit")。我们可以通过jgit来完成我们所有的git操作。
|
||||
|
||||
下面我将简单些一个例子来说明,使用java来创建一个git仓库,并且为仓库添加一个远端仓库地址。如果通过git脚本来执行,那么语句就是:
|
||||
|
||||
```xml
|
||||
git init
|
||||
|
||||
git remote add git://XXXXXX
|
||||
```
|
||||
|
||||
首先我们需要下载[org.eclipse.jgit.jar](http://download.eclipse.org/jgit/maven/org/eclipse/jgit/org.eclipse.jgit/2.0.0.201206130900-r/org.eclipse.jgit-2.0.0.201206130900-r.jar) 包,并将其加入eclipse 的classpath中(当然这里使用eclipse来完成操作)。示例代码为:
|
||||
|
||||
```java
|
||||
package com.toozhao.git;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.InitCommand;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.lib.StoredConfig;
|
||||
import org.eclipse.jgit.transport.RefSpec;
|
||||
import org.eclipse.jgit.transport.RemoteConfig;
|
||||
import org.eclipse.jgit.transport.URIish;
|
||||
|
||||
public class TestJGit {
|
||||
public static void main(String[] args) {
|
||||
Repository repo = null;
|
||||
|
||||
String path = "d:/testjgit"; // 设置git仓库的路径
|
||||
InitCommand init = new InitCommand();
|
||||
// 设置路径
|
||||
init.setBare(false).setDirectory(new File(path));
|
||||
// 执行git init ,创建仓库
|
||||
Git git;
|
||||
try {
|
||||
git = init.call(); // 创建仓库
|
||||
repo = git.getRepository();
|
||||
System.out.println("create repo success");
|
||||
} catch (GitAPIException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 执行 git remote add 命令
|
||||
// 实例化一个RemoteConfig 对象,用户配置远端仓库
|
||||
StoredConfig config = repo.getConfig();
|
||||
try {
|
||||
RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
|
||||
// 设置你的远端地址
|
||||
URIish uri = new URIish("git://github.com/wahyd4/testjgit");
|
||||
// 设置哪个分支
|
||||
RefSpec refSpec = new RefSpec("+refs/head/*:refs/remotes/origin/*");
|
||||
// 更新配置
|
||||
remoteConfig.addFetchRefSpec(refSpec);
|
||||
remoteConfig.addPushRefSpec(refSpec);
|
||||
remoteConfig.addURI(uri);
|
||||
remoteConfig.addPushURI(uri);
|
||||
// 更新配置
|
||||
remoteConfig.update(config);
|
||||
// 保存到本地文件中
|
||||
config.save();
|
||||
System.out.println("git remote add success.");
|
||||
} catch (URISyntaxException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
执行完本代码后,打开d:/testjgit/可以看到一个.git的隐藏文件夹(如果看不到,请设置去除隐藏文件夹)。表明它已经是一个git仓库了。如果你的eclipse安装有aptana 插件,将仓库导入eclipse,eclipse还可以自动识别出git仓库。而在Egit 1.x插件中版本中都不能识别出。
|
||||
|
||||
如果你打开.git/CONFIG文件。你还可以看到:
|
||||
|
||||
```xml
|
||||
[core]
|
||||
repositoryformatversion = 0
|
||||
filemode = false
|
||||
logallrefupdates = true
|
||||
[remote "origin"]
|
||||
url = git://github.com/wahyd4/testjgit
|
||||
pushurl = git://github.com/wahyd4/testjgit
|
||||
fetch = +refs/head/*:refs/remotes/origin/*
|
||||
push = +refs/head/*:refs/remotes/origin/*
|
||||
```
|
||||
|
||||
这些既是我们对仓库进行的配置,全都由这个文件定义。使用aptana,你可以直接将仓库push 到你设置的远端地址。
|
||||
Reference in New Issue
Block a user