Renganathan Selvaraju
Renganathan Selvaraju

Reputation: 85

How to reset a file to a particular commit with JGit?

Consider my local repository contains more than one file, while doing checkout for a particular commit of a file, other files in the repository got deleted.

I am using following API (git is the instance of git repository)

git.checkout().setName(commitId).call()

Is this correct way to check out a particular commit of a particular file?

Upvotes: 2

Views: 1145

Answers (2)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 20985

The JavaDoc of setName() says

When only checking out paths and not switching branches, use setStartPoint(} to specify from which branch or commit to check out files.

And for addPath() it states:

If this option is set, neither the setCreateBranch() nor setName() option is considered. In other words, these options are exclusive.

Therefore I think you should use

git.checkout().addPath( ... ).setStartPoint( ... ).call();

Upvotes: 2

VonC
VonC

Reputation: 1324347

Your call reset the index (and can remove files no longer present in the new commit you check out)

You can look for a more precise example in jgit/porcelain/RevertChanges.java

 // revert the changes
 git.checkout().addPath(fileName).call();

In your case:

git.checkout().setname(commitId).addPath(fileName).call()

Upvotes: 0

Related Questions