简体   繁体   中英

Git local repo doesn't show the name of the branch

I have a Jenkins Maven build where the git client issues the following commands:

git rev-parse --is-inside-work-tree
git config remote.origin.url ssh://git@HOST:PORT/Project/main.git
git --version
git fetch --tags --progress ssh://git@HOST:PORT/Project/main.git +refs/heads/*:refs/remotes/origin/*
git rev-parse refs/remotes/origin/BRANCH_1^{commit}
git rev-parse refs/remotes/origin/origin/BRANCH_1^{commit}
git config core.sparsecheckout
git checkout -f f60bbb84ae0189b766a357a7d589b14c360f9c03
git rev-list 0caaf02f6a5789e54c6562f4d34632eb8e0a7e89

and checks out the requested BRANCH_1 branch. In its workspace, the Git configuration file contains only the following rows:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = ssh://HOST:PORT/Project/main.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    prune = true

In these circumstances, JGit lists only

HEAD for

repository.eaxctRef(Constants.HEAD).getLeaf().getName()

and origin for

git.remoteList().call().get(i).getName()

How could I retrieve, using JGit, the branch name, in this case, BRANCH_1 ?

The used JGit version is 4.9.0.201710071750-r.

The command git checkout -f puts your repository into a state called detached head . Hence the result of JGit's exactRef() is correct.

In order to find branches that point to the commit that you checked out, JGit provides an implementation of git name-rev --refs=refs/heads/* <commit id> .

For example:

git.nameRev().add( ObjectId.fromString( "abc123..." ).addPrefix( "refs/heads" ).call();

will return a map of commit ids to names. Note, however, that if multiple branches point to the given commit, JGit will return an arbitrary matching branch name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM