简体   繁体   English

JGit checkout上次提交

[英]JGit checkout previous commit

How to tell JGit to checkout its parent? 如何告诉JGit结账其父母? For example, if I have a situation like the one below on the master branch: 例如,如果我在主分支上遇到类似下面的情况:

c815b27 newestCommit (HEAD -> master, origin/master, master)
e46dcaf previousCommit
b2d6867 previousPreviousCommit

I would like to call a command from JGit that would look something like: 我想从JGit调用一个看起来像这样的命令:

git.checkout().setName("c815b27~").call();

and would result in the state where HEAD would be moved to commit e46dcaf : 并将导致将HEAD移动到提交e46dcaf

c815b27 newestCommit (origin/master, master)
e46dcaf previousCommit (HEAD)
b2d6867 previousPreviousCommit

However, when I call the above checkout statement nothing happens. 但是,当我打电话给上述结帐声明时,没有任何反应。 I have also come across the following statement, which also does not move HEAD: 我还遇到了以下声明,它也没有移动HEAD:

git.checkout().setStartPoint("c815b27~").call();

Any ideas how to achieve moving to the previous commit based on tilde (~) or caret (^) symbols, and whether is even possible with the JGit API? 任何想法如何实现基于波浪号(​​〜)或插入符号(^)符号移动到先前的提交,以及是否甚至可以使用JGit API?

First, you need to resolve the expression that points to the previous commit. 首先,您需要解析指向上一次提交的表达式 Then you can checkout the resulting commit id. 然后,您可以签出生成的提交ID。

For example: 例如:

ObjectId previousCommitId = git.getRepository().resolve( "HEAD^" );
git.checkout().setName( previousCommitId ).call();

Note that checking out a commit detaches HEAD . 请注意,签出提交会分离HEAD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM