Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Revert to a an old commit/revision(Revert to a an old commit/revision)
Revert to a an old commit/revision [message #1794914] Tue, 11 September 2018 04:35 Go to next message
Ali Erturk is currently offline Ali ErturkFriend
Messages: 4
Registered: August 2012
Junior Member
Hello,

I want to revert to a an old revision but cannot. Below is simple code. What I need is to get just "Line 1" and "Line 2" when I revert back to commit #2 in the code.



import java.io.File;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RevertCommand;
import org.eclipse.jgit.revwalk.RevCommit;

public class JGit1 {

    public static void main(String[] args) {
        try {
            // create a clean repository
            File path = new File("c:/temp/agit/gitrepo");
            if (path.exists()) {
                FileUtils.deleteDirectory(path);
            }
            Git git = Git.init().setDirectory(path).call();
            System.out.println("Created a new repository at " + git.getRepository().getDirectory());

            // Create a new file and add it to the index
            File newFile = new File(path, "file1.txt");
            FileUtils.writeStringToFile(newFile, "Line 1\r\n", "UTF-8", true);
            git.add().addFilepattern("file1.txt").call();
            RevCommit rev1 = git.commit().setAuthor("test", "test@test.com").setMessage("Commit Log 1").call();

            // commit some changes
            FileUtils.writeStringToFile(newFile, "Line 2\r\n", "UTF-8", true);
            git.add().addFilepattern("file1.txt").call();
            RevCommit rev2 = git.commit().setAuthor("test", "test@test.com").setMessage("Commit Log 2").call();

            // commit some changes
            FileUtils.writeStringToFile(newFile, "Line 3\r\n", "UTF-8", true);
            git.add().addFilepattern("file1.txt").call();
            RevCommit rev3 = git.commit().setAuthor("test", "test@test.com").setMessage("Commit Log 3").call();

            RevertCommand revertCommand = git.revert();
            // revert to revision 2
            revertCommand.include(rev2);
            RevCommit revCommit = revertCommand.call();


            //git.add().addFilepattern("file1.txt").call();
            RevCommit rev4 = git.commit().setAuthor("test", "test@test.com").setMessage("Commit Log 4").call();
            
            // print logs
            Iterable<RevCommit> gitLog = git.log().call();
            Iterator<RevCommit> it = gitLog.iterator();
            while (it.hasNext()) {
                RevCommit logMessage = it.next();
                System.out.println(logMessage.getName() + " - " + logMessage.getFullMessage());
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}




When opened file now, content is:
Line 1
<<<<<<< master
Line 2
Line 3
=======
>>>>>>> 54e7037 Commit Log 2


But, I expect
Line 1
Line 2

[Updated on: Tue, 11 September 2018 08:01]

Report message to a moderator

Re: Revert to a an old commit/revision [message #1794925 is a reply to message #1794914] Tue, 11 September 2018 08:09 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 274
Registered: July 2009
Senior Member
for commit 2 and 3 you forgot to add the file. Either an explicit git add like for rev1 or a commit command with .setAll(true)
Additionally your add command before committing rev1 uses the wrong path.
Additionally what you try causes a conflict also in native git.
I would start with reverts which do work also in native git


Ciao
Chris
Re: Revert to a an old commit/revision [message #1794926 is a reply to message #1794925] Tue, 11 September 2018 08:13 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 274
Registered: July 2009
Senior Member
opps, sorry, I think you updated your message and I commented on the initial version. Now my comments about problems in the code don't apply anymore.

But still, what you tried leads to conflicts also in native git, or?


Ciao
Chris
Re: Revert to a an old commit/revision [message #1794928 is a reply to message #1794926] Tue, 11 September 2018 08:31 Go to previous messageGo to next message
Ali Erturk is currently offline Ali ErturkFriend
Messages: 4
Registered: August 2012
Junior Member
Hello, Yes, it causes conflicts in native git also.

In summary, what I want is to return back to an older revision keeping history.

e.g. I will commit rev1, rev2, rev3, and rev4. Later, if I decide to back to rev, what I will do. It should be:
rev1-> rev2->-rev3->rev4->rev5(same content as in rev2)
Re: Revert to a an old commit/revision [message #1794934 is a reply to message #1794928] Tue, 11 September 2018 09:59 Go to previous messageGo to next message
Ali Erturk is currently offline Ali ErturkFriend
Messages: 4
Registered: August 2012
Junior Member
Ok, confused some concepts about "revert". If I revert rev4 and rev3, I get rev2 contents then commit it as rev5. No problem here.

I think, the best solution for this case is to checkout rev2 then copy it to a temporary folder; then check out master again and copy rev2 file contents over the file in master, finally commit. Is that a good approach?
Re: Revert to a an old commit/revision [message #1794951 is a reply to message #1794934] Tue, 11 September 2018 14:12 Go to previous message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 274
Registered: July 2009
Senior Member
instead of copying files you could checkout the state you want. Like in (https://stackoverflow.com/questions/3380805/checkout-old-commit-and-make-it-a-new-commit). In your case it would be:

git rm -r .
git checkout rev2 .  # Watch the trailing '.'
git commit -m rev5 ...


Ciao
Chris
Previous Topic:Does EGit 5.x support the Git protocol version 2?
Next Topic:Remove/add file to some commit
Goto Forum:
  


Current Time: Sat Apr 20 11:32:01 GMT 2024

Powered by FUDForum. Page generated in 0.03367 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top