Venom Venom - 4 months ago 47
Java Question

JGit checkout over the same branch

I need to sequentially check out tags over a branch I created with JGit.

CheckoutCommand checkout = new Git(testRepository).checkout();

if (!branchExists())

is a
which contains the name of the commit I want to checkout (
changes in a loop). I don't want to create a branch each time I checkout as I don't need to. The following error is shown:

org.eclipse.jgit.api.errors.JGitInternalException: Could not rename file target\TestRepository\server\db\scripts\postgresql\._db_script.sql6197897692249726905.tmp to target\TestRepository\server\db\scripts\postgresql\_db_script.sql

Before this happened I was trying to checkout inside the else statement with


It did not throw any error but it did not checkout the tag either.


Well, it seems that using JGit in Windows may generate conflicts related to managed files sometimes. What I made for solving this recurrent issue was to execute a git clean --force followed by a git reset --hard before each git checkout <ref_name>. This was totally fine for me, as I didn't need to stage changes...

The problem was that basically after using some files (even in a read-only way), they were marked as changed (I verified it with git status). The solution explained above implied more operations, but totally solved my problem.

For further details, refer to git documentation about these actions:

  • Git clean: Removes untracked files from the working tree.
  • Git reset: Resets current HEAD to the specified state.

And develop the appropiate implementation of these two actions with JGit: