SeriousM
4/9/2013 - 11:05 AM

This script is able to push the source from one repository/branch to another repository/branch.

This script is able to push the source from one repository/branch to another repository/branch.

#!/bin/bash

#./sync_repos_full.sh [path_to_work_in] [ssh/http-origin] [ssh/http-destination]

echo ScriptName is $0
echo ParamCount is $#

if [ $# -ne 3 ]; then
        echo The parameter count must be 3
        exit 1
fi

echo LocalGitPath is $1
echo GitRemoteOrigin is $2
echo GitRemoteDestination is $3

if [ ! -d "$1" ]; then
  echo Local Git path does not exist, creating it
  mkdir "$1"
fi

echo Changing into local git path
cd $1

if [ ! -d "$1/HEAD" ]; then
  echo Local Git Path contains no Git repository, init a new one

  git init --bare "$1"

  if [ $? -ne 0 ]; then
    echo Git init fails, exit
    exit 1
  fi
fi

echo Setting up the remotes
git remote rm origin
git remote rm destination
git remote add origin "$2"
git remote add destination "$3"

echo fetching orign "$2"
git fetch origin +refs/heads/*:refs/heads/* --force

LASTERROR=$?
if [ $LASTERROR -ne 0 ] && [ $LASTERROR -ne 128 ]; then #128 = no changes found
        echo git fetch operation fails, exit
        exit 1
fi

echo pushing branches to destination "$3"
git push destination --all --force -v

LASTERROR=$?
if [ $LASTERROR -ne 0 ] && [ $LASTERROR -ne 128 ]; then #128 = no changes found
        echo git push operation fails, exit
        exit 1
fi

echo pushing tags to destination "$3"
git push destination --tags --force -v

LASTERROR=$?
if [ $LASTERROR -ne 0 ] && [ $LASTERROR -ne 128 ]; then #128 = no changes found
        echo git push operation fails, exit
        exit 1
fi

echo done

exit 0
: ./sync_repos_full.bat [ssh/http-origin] [ssh/http-destination]
@ECHO OFF

SET sourceRepository=%1
SET targetRepository=%2

IF "%1"=="" (
  SET error_msg=sourceRepository is missing
  goto error
)

IF "%2"=="" (
  SET error_msg=targetRepository is missing
  goto error
)

IF EXIST .\HEAD (goto fetch) ELSE (goto init)

:init
call git init --bare .

:fetch
call git remote rm origin
call git remote rm destination
call git remote add origin "%sourceRepository%"
call git remote add destination "%targetRepository%"
call git fetch origin +refs/heads/*:refs/heads/* --force

:push
call git push destination --all --force -v
call git push destination --tags --force -v
goto exit

:error
echo %error_msg%

:exit
echo --DONE--
#!/bin/bash

#./sync_repos_1branch.sh [path_to_work_in] [ssh/http-origin] [ssh/http-destination] [origin-branch] [destination-branch]

echo ScriptName is $0
echo ParamCount is $#

if [ $# -ne 5 ]; then
        echo The parameter count must be 5
        exit 1
fi

echo LocalGitPath is $1
echo GitRemoteOrigin is $2
echo GitRemoteDestination is $3
echo GitBranchOrigin is $4
echo GitBranchDestination is $5

if [ ! -d "$1" ]; then
  echo Local Git path does not exist, creating it
  mkdir "$1"
fi

echo Changing into local git path
cd $1

if [ ! -d "$1/HEAD" ]; then
  echo Local Git Path contains no Git repository, init a new one

  git init --bare "$1"

  if [ $? -ne 0 ]; then
    echo Git init fails, exit
    exit 1
  fi
fi

echo Setting up the remotes
git remote rm origin
git remote rm destination
git remote add origin "$2"
git remote add destination "$3"

echo fetching orign branch "$4" from "$2"
git fetch origin $4:origin/$4 --force

LASTERROR=$?
if [ $LASTERROR -ne 0 ] && [ $LASTERROR -ne 128 ]; then #128 = no changes found
        echo git fetch operation fails, exit
        exit 1
fi

echo pushing orign branch "$4" to destination "$3" branch "$5"
git push destination origin/$4:$5 --force


LASTERROR=$?
if [ $LASTERROR -ne 0 ] && [ $LASTERROR -ne 128 ]; then #128 = no changes found
        echo git push operation fails, exit
        exit 1
fi

echo done

exit 0

Windows

On windows you can specify file repositories with file:///\\<computer>\<path> (ex: file:///\\homepc\repos\git-backup.git).

It is always a good practice to postfix the folder with .git to indicate that this folder is a bare repository.