我学习git并使用JGit从java代码访问Git repos.默认情况下,Git不允许克隆到非空目录.我们怎么知道在本地机器上已经为一个特定的git repo做了一个git clone,这样我们以后只能做一个Git pull?
目前我正在使用这种方法:
if a root folder is existing in the specified location
clone has been done
pull
else
clone
Run Code Online (Sandbox Code Playgroud)
不确定这是否正确.有更好的想法吗?
谢谢.
通过克隆,我能够找到 git 存储库的 HEAD 并使用 HEAD 我可以使用 Egit 中的以下代码在 GITHUB 中找到修订 ID:- ObjectId revId = repository.resolve(Constants.HEAD); 如何在不克隆整个存储库的情况下通过只知道我的 GITHub 存储库的 url 来找到此修订 ID,因为克隆整个存储库需要时间。请建议。
我正在写这个方法,我正在使用jgit库克隆一个git repo,然后用这些文件做一些事情,最后我想删除repo.我遇到的问题是当我在.pack文件(位于.git\objects\pack)上调用delete()方法时,它无法删除.但是可以删除所有其他文件.为什么会这样?
我有一个 git 存储库,我可以使用 .gitignore 查看特定分支中的所有提交git log branch-name。但我想打印 HTML 页面上特定分支的提交消息、作者和所有提交的日期。有什么解决办法吗?
我该如何使用jgit它?
如何使用 JGit 列出当前分支中的所有标签?
我可以轻松列出所有标签
List<Ref> call = jGit.tagList().call();
for (Ref ref : call) {
System.out.println("Tag: " + ref);
}
Run Code Online (Sandbox Code Playgroud)
但是如何只列出当前分支中的那些?
我正在尝试从托管在 bitbucket 的远程存储库中提取数据。为此,我使用 JGit。我的代码如下;
Git git = Git.open( new File( localPath+"/.git" ) );
PullCommand pullCmd = git.pull();
pullCmd.setCredentialsProvider(this.credentialsProvider);
pullCmd.setRemoteBranchName(branch);
//pullCmd.setRemote("origin");
pullCmd.setRemote(remoteUrl);
PullResult result = pullCmd.call();
//FetchResult fetchResult = result.getFetchResult();
//MergeResult mergeResult = result.getMergeResult();
//mergeResult.getMergeStatus();
Run Code Online (Sandbox Code Playgroud)
但我总是遇到例外,
org.eclipse.jgit.api.errors.InvalidConfigurationException: No value for key remote.https://xyz@bitbucket.org/xyz/testproject.git.url found in configuration
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:266)
at git.GitPullOperation.execute(GitPullOperation.java:37)
at ui.MainScreen.invokeGitOperation(MainScreen.java:214)
Run Code Online (Sandbox Code Playgroud)
尝试了 stackoverflow 中的所有答案,但没有任何效果。
任何帮助对我来说都是很大的。
我在一个项目中使用JGit,我想实现一个与git --fetch all. 该JGit API文档提供了三种可供选择提取操作叫setRefSpecs(...)。所有替代方案都需要开发人员定义RefSpecs要更新的具体内容。
我希望我必须查询所有可用的存储库RefSpecs并将它们聚合为一个组合 fetch 调用(类似于使用 JGit 获取所有分支
)。但是,我不明白如何将 aRef转换为 a RefSpec。我很感激这里的任何指针。
与此非常相关,我也不确定如何修剪已远程删除的本地分支 ( fetch --prune)。我是否正确假设该设置setRemoveDeletedRefs可以true实现这一目标?
我需要一段可以pull来自原点和merge本地存储库的代码。我尝试使用可以获取最后一次提交和修订的本地存储库。但是无法pull从原点进行更改。
File gitWorkDir = new File(gitDir);
Git git = Git.open(gitWorkDir);
Repository repo = git.getRepository();
UsernamePasswordCredentialsProvider user = new
UsernamePasswordCredentialsProvider("username", "password");
PullCommand pullCmd = git.pull();
pullCmd.setCredentialsProvider(user);
pullCmd.call();
ObjectId lastCommitId = repo.resolve(Constants.HEAD);
log.info(lastCommitId);
Run Code Online (Sandbox Code Playgroud)
gitDir我的本地存储库目录在哪里。
我可以用这段代码推送到远程
return git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)).setRemote("origin").call();
Run Code Online (Sandbox Code Playgroud)
我还能够获得具有所有必要范围的oauth访问令牌.如何推送访问令牌?
我在项目中使用JGit库。这是我的JGit util类:
public class GitUtil {
private static final RefSpec REF_SPEC = new RefSpec(Constants.GIT_REF_SPEC);
private static final RefSpec REMOTE_REF_SPEC = new RefSpec(Constants.GIT_REMOTE_REF_SPEC);
private static final CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(Constants.GIT_REMOTE_USER_NAME, Constants.GIT_REMOTE_PASSWORD);
private static Git git;
@Autowired
private HibernateUtilServiceImpl hibernateUtilServiceImpl;
@PostConstruct
public void initGitRepository() throws Exception {
File file = new File(Constants.GIT_REPOSITORY_MAIN_FILE_PATH);
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
repositoryBuilder.addCeilingDirectory(file);
repositoryBuilder.findGitDir(file);
if (repositoryBuilder.getGitDir() == null) {
git = Git.init().setDirectory(file.getParentFile()).call();
StoredConfig config = git.getRepository().getConfig();
config.setString("remote", "origin", "url", Constants.GIT_HSQLDB_REMOTE_REPOSTITORY_URL);
RemoteConfig remoteConfig = new RemoteConfig(config, "remote"); …Run Code Online (Sandbox Code Playgroud)