Jenkins + Git Publisher – How to push back to {GIT_BRANCH}?

I’m using the current version of the Git plugin (SCM provider and Publisher) on Jenkins 1.500. The SCM polling works fine, using my git HTTP URL and “branches to build” setting of “feature-*”. This picks up changes to any branch, e.g., “feature-1234”, runs the build/test/coverage tasks, and reports on success or failure. All of this works fine, including a merge from the integration branch where the code should end up after a successful build and code review.

The problem is in trying to push the completed build branch BACK to origin, on the same “feature-1234” branch. The build variable “GIT_BRANCH” in this case contains “origin/feature-1234”, which produces the following error and failure in the Git Publisher after an otherwise successful build:

  • Can you run jekyll on your own server?
  • How to tell if git has ever tracked file X
  • Revert and unstage
  • git/github: Remove from history (but not from HEAD) some large binary files
  • How can I set up gVim as my editor on Git for Windows?
  • How to prevent Jenkins SCM polling from triggering a build on branch creation
  • Pushing HEAD to branch origin/feature-1234 at repo origin
    ERROR: Failed to push branch origin/feature-1234 to origin
    hudson.plugins.git.GitException: Command "/usr/bin/git push HEAD:origin/feature-1234" returned status code 1:
    stderr: error: unable to push to unqualified destination: origin/feature-1234
    The destination refspec neither matches an existing ref on the remote nor
    begins with refs/, and we are unable to guess a prefix based on the source ref.
    error: failed to push some refs to ''
        at hudson.plugins.git.GitAPI.launchCommandIn(
        at hudson.plugins.git.GitAPI.launchCommand(
        at hudson.plugins.git.GitAPI.push(
        at hudson.plugins.git.GitPublisher$4.invoke(
        at hudson.plugins.git.GitPublisher$4.invoke(
        at hudson.FilePath.act(
        at hudson.FilePath.act(
        at hudson.plugins.git.GitPublisher.perform(
        at hudson.tasks.BuildStepMonitor$3.perform(
        at hudson.model.AbstractBuild$AbstractBuildExecution.perform(
        at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(
        at hudson.model.Build$BuildExecution.post2(
        at hudson.model.AbstractBuild$
        at hudson.model.Run.execute(
        at hudson.model.ResourceController.execute(
    Build step 'Git Publisher' changed build result to FAILURE
    Build step 'Git Publisher' marked build as failure

    See the extra “origin” in there? origin/feature-1234 <== that’s the current value of ${GIT_BRANCH}, and while I understand it’s a remote branch and all, it’s stopping me from running the process we want to follow.

    If I’m missing something simple, I’d love to hear it. But I have tried many different settings for the various git-related portions of my build and nothing seems to allow me to commit the merged and tested code back to the work branch (as opposed to the integration branch, which is easy to do.

  • Git Remote Add Privacy / History
  • Unable to Install Core Library for Git on Ubuntu
  • AWS Elastic Beanstalk + Git Submodules
  • Could I change my name and surname in all previous commits?
  • How to apply patch hunk by hunk?
  • Tell git that a text file is actually binary
  • 4 Solutions collect form web for “Jenkins + Git Publisher – How to push back to {GIT_BRANCH}?”

    A workaround for me seems to push the branch manually, at least when it is going to be the same each time.

    In the build configuration, “Source Code Management” => Advance
    There is a field “Name”
    You need to specify the ID of the repository and then to use it at the git publisher plugin: “Target remote name”

    Those two names need to be identical
    look at the “?” – to get more information

    I push changes by hand. You can use the windows environment variable as below in batch scripts:

    This evaluates to eg. ‘git checkout feature-1234’

    git checkout %GIT_BRANCH:origin/= %

    This evaluates to eg. ‘git push origin feature-1234’

    git push %GIT_BRANCH:/= %

    You can also do something similar with token macros such as ${GIT_BRANCH##origin/} and ${GIT_BRANCH#*/}. These work in some Jenkins plugins, but not all, so it might not work in the Git Publisher.

    I recommend to configure fully qualified /refs/remotes/origin/feature-1234 as your Git “Branch to build”. This way it seems more understandable to me.

    In such case your $GIT_BRANCH would get magically set by Jenkins to origin/feature-1234 same as you observed.

    Then, instead of using overly complicated (but portable) GitPublisher, just add a post-build step “Execute Shell”:

    echo Remote branch is $GIT_BRANCH, replacing origin with refs/heads.
    git push --follow-tags "$GIT_URL" "+HEAD:${GIT_BRANCH/#origin\//refs/heads/}"
    Git Baby is a git and github fan, let's start git clone.