Search code examples
gitjenkinsbitbucket

How do I keep downstream branches in sync with master?


I maintain a git repo of tests for another project. In this repo we have branches that map to different environments for testing:

Bitbucket has a nice feature that any pull requests to master can also be merged into development branch. We utilize this feature a lot because often the tests we are adding can be run across all environments. The issue is that we have 2 other branches we want changes from master to also get.

I have a Jenkins job setup to run whenever changes to master are made that is supposed to pull those master changes into the other branches but keep I getting this error:

error: failed to push some refs to 'my_repo'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

this is what the jenkins job does:

git fetch
git branch -D ci_env (branch that should also get master changes)
git checkout -b ci_env origin/ci_env
git pull origin master
git push

These are the global configurations on the Jenkins node:

+ git config --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=****my_repo****
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

Solution

  • Adding this to your pipeline is probably your best bet.

    EDIT: i had something else on my mind and didn't pay attention to the OP question.

    the following will sync the branches

    • git fetch grabs the new changes from the source refspec (sourceRef)
    • . (period) is short hand for the local repository as <remote>
    • sourceRef in your case, master with the latest changes
    • destinationRef in your case, ci_env where you want to sync the changes
    
    git fetch <remote> <sourceRef:destinationRef>
    
    git fetch . master:ci_env
    

    This only works for fast-forward merges, meaning no conflicts are expected and no outside commits are made on this branch except for this merge.