I have a Private Github repository on github.com and I have a Gitlab Instance, running in closed network, reachable only through VPN. The task is to pull code from Github.com, and periodically check if there's changes and if so - push changes to Gitlab Instance (script is working on VM in same network with GL Instance). Ive tried this script on my non-production repos:
#!bin/bash
PROJECT_DIR=------_test
# First iteration
if ! [ -d ./$PROJECT_DIR/ ]; then
git clone git@github.com:---------/--------_test.git
cd $PROJECT_DIR
git remote add gitlab git@gitlab.com:-------/-----------_test.git
echo "Cloned succesfully"
else
cd $PROJECT_DIR
echo "Repo is already cloned, looking for new commits..."
fi
git fetch
git remote update
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")
if [ $LOCAL = $REMOTE ]; then
echo "Up-to-date"
elif [ $LOCAL = $BASE ]; then
git pull origin
git push gitlab
elif [ $REMOTE = $BASE ]; then
echo "Need to push"
else
echo "Diverged"
fi
And it did well, but when trying this with 2 remote repos (Github and Gitlab Instance), which have different files changed and different last commits/HEAD this happens:
To ssh://s----.---:60022/root/---------.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'ssh://-----.m------.-----:60022/root/m------.git'
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.
Fast forward did not help. I tried to do git fetch, git pull remote-name and then merge for example origin/master to remote/master and it worked too but I believe there should be some much easier way to perform this action.
P.S. Using Gitlab CE so i cant perform pull mirroring.