Christian
Christian

Reputation: 105

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!

Upvotes: 10

Views: 20787

Answers (4)

Hau Le
Hau Le

Reputation: 121

Right click file which you want to revert -> Replace With -> HEAD revision

Upvotes: 0

Joel Bricker
Joel Bricker

Reputation: 11

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. His steps are here below so you don't have to scroll:

  • install latest nightly of 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.

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.

Upvotes: 1

Matthias Sohn
Matthias Sohn

Reputation: 101

  • install latest nightly of 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

Upvotes: 10

VonC
VonC

Reputation: 1323573

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

Upvotes: 3

Related Questions