Search code examples
eclipsegitegit

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)?

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!


Solution

  • If you consider this commit from 5 days ago, called ' Merge "Implement a revert command" ' (Shawn Pearce), it seems it will be available soon.

    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());
           }
    }