I want to create a two-way sync between Azure DevOps and GitHub by doing the following:
- Create an Azure DevOps pipeline with a CI trigger that push changes from Azure DevOps repo to a branch in GitHub
- Create a second pipeline that listens for changes from GitHub and pulls them back to a branch in Azure DevOps
Here are the commands that I'm using for both pipeline jobs:
git clone --mirror <source>
cd <repo name>
git remote add --mirror=fetch target <target>
mkdir ../workdir
git fetch origin
git --work-tree=../workdir/ checkout <branch name>
git push target <branch name>
When I test the pipeline I end up with an error like this:
! [rejected] <branch name> -> <branch name> (fetch first)
error: failed to push some refs to 'git@github.com:<target>/<target repo>.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
I am by no means a git expert, so any help is very much appreciated.
UPDATE # 1
The captures below show the pipeline that I've built to push changes to GitHub (based on this answer), but it still isn't working.
This is the new error:
Cloning into '<repo>'...
Switched to a new branch '<branch name>'
Branch '<branch name>' set up to track remote branch '<branch name>' from 'origin'.
warning: adding embedded git repository: <repo>
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint: git submodule add <url> <repo>
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached <repo>
hint:
hint: See "git help submodule" for more information.
[<branch name> XXXXXXX] Update from Azure DevOps
1 file changed, 1 insertion(+)
create mode 160000 <repo>
To git@github.com:<org>/<repo>.git
! [rejected] <branch name> -> <branch name> (fetch first)
error: failed to push some refs to 'git@github.com:<org>/<repo>.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
UPDATE # 2
I updated the script (shown below) to pull changes from GitHub to Azure DevOps based on the updated answer, but it still isn't working.
git clone git@github.com:XXX/XXX.git
cd <repo folder>
git config --global user.email "XXX"
git config --global user.name "XXX"
git checkout <source branch>
git add .
git commit -m "Pull from GitHub"
git push https://{AzureDevopsPAT}@dev.azure.com/{org}/{pro}/_git/XXX.git <target branch>
This is the error:
Cloning into '<repo>'...
Warning: Permanently added the RSA host key for IP address '140.82.113.4' to the list of known hosts.
Already on '<source branch>'
Your branch is up to date with 'origin/<source branch>'.
On branch <source branch>
Your branch is up to date with 'origin/<source branch>'.
nothing to commit, working tree clean
error: src refspec <target branch> does not match any
error: failed to push some refs to '***'