简体   繁体   English

git在Egit中恢复

[英]git revert in Egit

Is it possible to do "git revert"s in Egit to rollback changes by creating a new commit (as opposed to checking out an older commit or doing a hard reset which doesn't create a new commit rolling back the changes)? 是否有可能在Egit中执行“git revert”以通过创建新提交来回滚更改(而不是检查旧提交或执行硬重置,而不会创建新提交以回滚更改)?

This seems like an important feature if you have a central repository in case you ever need to undo changes that has already been pushed there. 如果您有一个中央存储库,以防您需要撤消已经推送的更改,这似乎是一个重要的功能。

Thanks in advance! 提前致谢!

  • install latest nightly of EGit (0.11.xx) 安装最新的EGit(0.11.xx)
  • open History View 打开历史视图
  • right-click on the commit you want to revert in the currently checked out branch 右键单击要在当前签出的分支中还原的提交
  • click "Revert Commit" 点击“还原提交”

-- Matthias - 马蒂亚斯

If you consider this commit from 5 days ago , called ' Merge "Implement a revert command" ' (Shawn Pearce), it seems it will be available soon. 如果你从5天前开始考虑这个提交 ,称为' Merge'实现一个恢复命令“ '(Shawn Pearce),它似乎很快就会出现。

The diff are here : 差异在这里

public class RevertCommandTest extends RepositoryTestCase {
       @Test
       public void testRevert() throws IOException, JGitInternalException,
                       GitAPIException {
               Git git = new Git(db);

               writeTrashFile("a", "first line\nsec. line\nthird line\n");
               git.add().addFilepattern("a").call();
               git.commit().setMessage("create a").call();

               writeTrashFile("b", "content\n");
               git.add().addFilepattern("b").call();
               git.commit().setMessage("create b").call();

               writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n");
               git.add().addFilepattern("a").call();
               git.commit().setMessage("enlarged a").call();
               writeTrashFile("a",
                               "first line\nsecond line\nthird line\nfourth line\n");
               git.add().addFilepattern("a").call();
               RevCommit fixingA = git.commit().setMessage("fixed a").call();

               writeTrashFile("b", "first line\n");
               git.add().addFilepattern("b").call();
               git.commit().setMessage("fixed b").call();

               git.revert().include(fixingA).call();

               assertTrue(new File(db.getWorkTree(), "b").exists());
               checkFile(new File(db.getWorkTree(), "a"),
                               "first line\nsec. line\nthird line\nfourth line\n");
               Iterator<RevCommit> history = git.log().call().iterator();
               assertEquals("Revert \"fixed a\"", history.next().getShortMessage());
               assertEquals("fixed b", history.next().getFullMessage());
               assertEquals("fixed a", history.next().getFullMessage());
               assertEquals("enlarged a", history.next().getFullMessage());
               assertEquals("create b", history.next().getFullMessage());
               assertEquals("create a", history.next().getFullMessage());
               assertFalse(history.hasNext());
       }
}

I can't comment due to low reputation, but I wanted to add the final piece to @Matthias Sohn's answer just in case anyone, like me, finds this via searching for how to do this. 由于声誉不佳,我无法发表评论,但我想将最后一篇文章添加到@Matthias Sohn的答案,以防万一有人像我一样通过搜索如何做到这一点。 His steps are here below so you don't have to scroll: 他的步骤如下所示,所以你不必滚动:

  • install latest nightly of EGit (0.11.xx) 安装最新的EGit(0.11.xx)
  • open History View 打开历史视图
  • right-click on the commit you want to revert in the currently checked out branch 右键单击要在当前签出的分支中还原的提交
  • click "Revert Commit" 点击“还原提交”

This will add an entry at the top of the history view "Revert [previous commit comment]". 这将在历史记录视图“恢复[上一次提交注释]”的顶部添加一个条目。 If you right click this new entry you will see an option to commit the Revert operation. 如果右键单击此新条目,您将看到提交“还原”操作的选项。 You need to do it from the History view because as @Lennon said, you cannot commit and push from the Package Explorer. 您需要从History视图中执行此操作,因为正如@Lennon所说,您无法从Package Explorer中提交和推送。

The downside to this method is it will Revert all changes in the Commit. 这种方法的缺点是它将恢复提交中的所有更改。 I would prefer to be able to rollback only a specific file that was in a Changeset, so if anyone knows of a way to do this please add on. 我宁愿只能回滚一个Changeset中的特定文件,所以如果有人知道这样做的方法,请加上。

右键单击要还原的文件 - >替换为 - > HEAD修订版

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

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