How to create and push tags with JGit

Viewed 1395

I am using JGit to clone repository, make some changes in file, commit, tag it, and then push it to remote repository.

I have written code using JGit, which will clone repository, make some change in file, then commit the change, create new tag and push the commit and tag.

Git git = Git.cloneRepository()
             .setURI("https://*****@stash.dts.*****.git")
             .setDirectory(outputFolder)
             .call();

// Some code to update the file
git.add().addFilepattern(".").call();

git.commit().setMessage("File Commit").call();

// Creating tag
git.tag().setName(version).setForceUpdate(true).call();

// Pushing the commit and tag
git.push().call();

After push, I expect the remote repository to show my change and new tag, but change with commitId is showing in remote repoitory but tag is not visible. It's showing 'No Tags'.

1 Answers

To push all tags, you need to specifically enable this before calling the PushCommand.

For example:

List<PushResult> pushResult = git.push().setPushTags().call();

Alternatively you can use PushCommand::setRefSpecs to set a list of refspecs that you wish to get pushed.

Related