Tk421
Tk421

Reputation: 6418

JGIT: git checkout --

I have a not staged modified file and I would like to discard the changes.

In git it would be something like

git checkout -- .

How can I emulate this behavior with JGit ?

Thanks in advance.

Upvotes: 2

Views: 656

Answers (1)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 20985

To revert a single file, you could use the CleanCommand:

Set<String> paths = new HashSet<String>();
paths.add( ... );
git.clean().setPaths( paths ).call();

Unfortunately there is a bug that prevents the CleanCommand to reset files in sub-directories.

If I interpret the '.' in git checkout -- . correctly you want to revert all changes in the work directory. The ResetCommand does that:

git.reset().setMode( ResetType.HARD ).call();

This would also override the index with the contents from HEAD.

If you don't care about the index, you could also read the file-contents from the HEAD commit and write them to the work directory yourself. Let me know if that is of interest for you and I will try to assemble a snippet that does so.

Upvotes: 1

Related Questions