Revert to a an old commit/revision [message #1794914] |
Tue, 11 September 2018 04:35 |
Ali Erturk 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
[Updated on: Tue, 11 September 2018 08:01] Report message to a moderator
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05133 seconds