Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JGit Check if a branch is checked out

Tags:

java

jgit

I am working on a project using JGit. I managed to delete a branch, but I also want to check if that branch is checked out or not. I found a variable in CheckoutCommand but it is private:

private boolean isCheckoutIndex() {
    return startCommit == null && startPoint == null;
}

No public method returns what I want. When I use the command below on a checked out branch it returns an error that the branch cannot be deleted, so I want to check first if is checked out or not.

git.branchDelete().setForce(true).setBranchNames(branchName).call();
like image 691
marius9228 Avatar asked Jul 28 '14 10:07

marius9228


1 Answers

Repository::getFullBranch() returns the full name (e.g. refs/heads/main) of the currently checked out branch, if any. Otherwise the ID of the HEAD commit is returned or null, if there is no commit.

like image 129
Rüdiger Herrmann Avatar answered Oct 22 '22 04:10

Rüdiger Herrmann