How to hard reset from one git branch to other in JGit?

[亡魂溺海] 提交于 2019-12-02 05:23:08

问题


I have two branches one is master and another one level1. Now level1 is the latest I need to hard reset master to level1 normally in git bash I can do that by following command.

$ git checkout master
$ git tag old-master-branch 
$ git reset --hard level1
$ git merge -s ours origin/master 
$ git push origin master

This one works fine for me. My question is how can I achieve it by using JGit. I have tried it. But I am not able to figure out how to set the source and target branch.

consider a scenario I have cloned a master branch

 Git git = Git.cloneRepository().setURI(remote).setCredentialsProvider(new UsernamePasswordCredentialsProvider("obuli", "xxxxxx")).setDirectory(gitPath)                    .setNoCheckout(true).call();

Now I need to hard reset it to the level1.

git.reset().setMode(ResetType.HARD).call();

But here I am not specifying level1 . I dont know how to specify it. and also please say how to provide git merge -s ours origin/master in JGit


回答1:


By default the ResetCommand resets to HEAD. To reset to another branch, you need to specify this branch with setRef().

For example:

git.reset().setMode(ResetType.HARD).setRef("refs/heads/level1").call();

The above command will let the current branch point to the latest commit of level1 and checkout its state into the work directory .



来源:https://stackoverflow.com/questions/32736517/how-to-hard-reset-from-one-git-branch-to-other-in-jgit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!