Git

Configuration

git config --global --add alias.st status
git config --global --add alias.co checkout
git config --global --add alias.ci commit
git config --global --add alias.bc branch
git config --global --add alias.cls clean -f
git config --global credential.helper wincred
git config --global --add core.safecrlf false
git config --global user.name "Sealion Hunter"
git config --global user.email "sealion.hunter@gmail.com"

client certification

openssl pkcs12 -in mycert.pfx -nocerts -nodes -out nocert.key
openssl pkcs12 -in mycert.pfx -clcerts -nokeys -out clcert.crt
openssl pkcs12 -in mycert.pfx -cacerts -nokeys -out cacert.crt
git config --local http.sslbackend openssl
git config --local http.sslCert "/mycert.crt"
git config --local http.sslKey "/mycert.key"
git config --local http.sslCaInfo "/cacert.crt"
git config --local http.sslVerify "false"

Useful commands

git diff --name-status HEAD~2
git diff --no-index
git archive -o target.zip --prefix=ARNO HEAD $(git diff --name-status --diff-filter=ACMRT HEAD~2)

git branch --contains
git log -Swhat --oneline

git --git-dir <workingfolder>

git remote prune --dry-run

git clone --config http.sslBackend openssl --config http.sslCert=my.crt --config http.sslKey=my.key --config http.sslCaInfo=ca.crt --config http.sslVerify=false --config loginaccount remotehost

Sync two remote git repository

@echo off
setlocal enabledelayedexpansion

SET ROOT=E:\gitsync
SET SRC_REPO_ROOT=http://github.com/group
SET DST_REPO_ROOT=http://192.168.200.250/group

SET PROJECT=%1

SET GIT_DIR=%ROOT%\%PROJECT%
SET SRC_GIT_REPO=%SRC_REPO_ROOT%/%PROJECT%.git
SET DST_GIT_REPO=%DST_GIT_REPO%/%PROJECT%.git

if not exist %GIT_DIR%/.git (
    mkdir %GIT_DIR%
    pushd %GIT_DIR%
    git init
    git remote add origin %SRC_GIT_REPO%
    git fetch origin
    popd
)

pushd %GIT_DIR%

git remote rm dest
git remote add dest %DST_GIT_REPO%

echo removing branches not exist in %SRC_GIT_REPO% any more...
for /f "delims=" %%i in ('git remote prune --dry-run origin ^| findstr "\* \[would prune\]"') do call :removeBranch "%%i"
goto :fetchdata

:removeBranch
set remote_branch=%1
set branch=%remote_branch:~25,-1%
git push dest -d %branch%
goto :eof

:fetchdata
echo fetching from source repository %SRC_GIT_REPO% ...
time /t
git fetch -f -p -v --tags --progress %SRC_GIT_REPO% +refs/heads/*:refs/remotes/origin/*
if errorlevel 1 (
    time /t
    echo fetch from source repository failed.
    exit 1
)
echo fetch from source repository successfully.
time /t

echo remove invalid HEAD branch
git branch -d -r origin/HEAD

echo pushing to destination repository %DEST_GIT_REPO% ...
time /t
git push -f --tags dest "refs/remotes/origin/*:refs/heads/*"
if errorlevel 1 (
    time /t
    echo push to destination repository failed.
    exit 1
)
echo push to destination reposition successfully.
time /t

endlocal

:eof

Gitlab backup repository

#!/bin/bash

REP_ROOT=/home/git/git-data/repositories
TEMP_ROOT=/home/git/tempbackup
BAK_ROOT=/mnt/gitbackup/repos

LAST_BAK=last_backup

datetime=`date +%Y%m%d-%H%M%S`

curr_date=$(date "+%F")
curr_time=$(date "+%F-%H%M%S")
curr_incre_time=$(date "+%F %H:%M:%S")

LOG_FILE=/mnt/gitbackup/backup_${curr_time}.log

pushd $REP_ROOT
for repo in `find . -maxdepth 3 -type d -name "*[^wiki.].git"`;  do
  dir=`dirname ${repo}`
  olddir=`pwd`

  reponame=`basename ${repo}`
  echo "Info: processing ${reponame}..." >> ${LOG_FILE}

  cd ${repo}
  this_repo=`realpath --relative-to="$REP_ROOT" "$PWD"`
  echo $PWD $this_repo >> "${LOG_FILE}"
  bak_repo=${BAK_ROOT}/${curr_date}/${this_repo}
  tmp_repo=$TEMP_ROOT/${this_repo}

  mkdir -p ${bak_repo}
  mkdir -p ${tmp_repo}

  if [ -f "${tmp_repo}/${LAST_BAK}temp" ]; then
    #do incremental backup;
    last_time=`cat "${tmp_repo}/${LAST_BAK}"`
    echo ${curr_incre_time} > "${tmp_repo}/${LAST_BAK}"
    if [ `git log --since="${last_time}" --until="${curr_incre_time}" --all --oneline | wc -l` -eq 0 ]; then
      echo "INFO: no changes since last backup!" >> "${LOG_FILE}"
      cd ${olddir}
      continue
    fi
    echo "INFO: exporting '${this_repo}' --since='${last_time}'  to ${tmp_repo}/${bundle-incre-${curr_time}.bundle}" >> "${LOG_FILE}"
    git bundle create "${tmp_repo}/bundle-incre-${curr_time}.bundle" --all --tags --since="${last_time}" --until="${curr_incre_time}"
    cp "$tmp_repo/bundle-incre-${curr_time}.bundle" "${bak_repo}/"
    rm -f "$tmp_repo/bundle-incre-${curr_time}.bundle"
  else
    if [ `git log --all -l 3 | wc -l` -eq 0 ]; then
      echo "Info: blank repos: ${this_repo}" >> "${LOG_FILE}"
      cd ${olddir}
      continue
    fi
    echo "INFO: exporting '${this_repo}' all  to ${tmp_repo}/bundle-all-${curr_time}.bundle" >> "${LOG_FILE}"
    echo ${curr_incre_time} > "${tmp_repo}/${LAST_BAK}"
    git bundle create "${tmp_repo}/bundle-all-${curr_time}.bundle" --all --tags >> "${LOG_FILE}" $2 >> "${LOG_FILE}"
    cp "${tmp_repo}/bundle-all-${curr_time}.bundle" "${bak_repo}/"
    rm -f "${tmp_repo}/bundle-all-${curr_time}.bundle"
  fi

  cd ${olddir}
done
popd