Skip to content

Git Tipps and Tools

There are a lot of resources in the web, how to learn git.

Some examples:

Don't be overwhelmed. Everyone learns as they go along, but make sure you have a nice setup and understand the basics.

HowTo add a specific ssh-key for your git account

Sometimes it might be useful to have different ssh-keys per project or have a special ssh-key just for github/gitlab.

Steps:

  1. Generate a ssh-keygen
  2. Add ssh-key to GitHub or GitLab account under settings
  3. Write a ~/.ssh/config file with a content like this, other examples are in ECMWF.
INI
1
2
3
4
5
6
7
8
9
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_for_git

Host gitlab.com
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_rsa_for_gitlab

Make sure that you adjust the Hostname accordingly and be sure that you use ssh in your git repo:

Bash
1
2
3
$ git remote -v
origin  git@github.com:USER/repo.git (fetch)
origin  git@github.com:USER/repo.git (push)

These urls should not show https, if there is a https then you will need to sign in with your user credentials for gitlab (username and password).

Every git repository can be cloned using https, but for ssh-key access you need the git@... version (usually there are two options: ssh, and https).

HowTo Sync a GitHub and a GitLab repository

It is easy to import a GitHub repo into GitLab and the otherway around. However, if you want to make sure you can have both repos at the same state, you need to syncronize them.

How to call: ./git-repos-sync [URL1] [URL2] [Branch]

This means:

  • URL1 - Address of the first remote repository
  • URL2 - Address of the second remote repository
  • The order of URL1 or URL2 does not matter.
  • Branch is usually master

Different use cases:

  1. You have already a local copy of either of the repositories (e.g. GitLab or GitHub)
  2. You have no local copy of either repository.
syncronize git repos
git-repos-sync
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
# author: https://github.com/adidik/git-repos-sync
# sync two git repositories (github and gitlab)
# modified: 23.4.2021 (MB)
# License: MIT

if [ "$#" -ne 3 ]; then
    >&2 echo "Usage: git-repos-sync <repository URL> <repository URL> <branch-to-sync>"
    exit 1
fi

# Check if you are at root level (fails if not)
if ! git rev-parse --show-toplevel; then
    >&2 echo "Git repo found"
    if [ $PWD == $(git rev-parse --show-toplevel) ]; then
        echo "Not at root of local repo"
        echo $(git rev-parse --show-toplevel)
        exit 1
    fi
fi

if [ ! -d ".git" ]; then
    echo "Git repo for syncronization is not found, creating one..."
    git init
    git fetch $1
    git checkout -b master FETCH_HEAD   
    echo
fi

if ! git diff-index --quiet HEAD --; then
    >&2 echo "Local modifications found, looks like your in the conflict resolution. Resolve a conflict and commit. Then rerun script."
    exit 1
fi

echo "Left: $1"
echo "Right: $2"
echo 
echo "Fetch latest commits from branch $3 in $1"
if ! git fetch -u $1 $3:left/$3; then 
    >&2 echo "Fatal: unable to fetch from $1, rerun the script as soon as connnection restored."
    exit 1
fi  
echo "Fetch latest commits from branch $3 in $2"
if ! git fetch -u $2 $3:right/$3; then
    >&2 echo "Fatal: unable to fetch from $2, rerun the script as soon as connnection restored."
    exit 1
fi
if git checkout --quiet -b sync-$3 right/$3; then
    echo "Merge branches from left and right if necessary."
    if ! git merge -m "Merge to sync between $1 and $2" --log left/$3; then
                >&2 echo "Merge conflict. Solve it manually, commit and rerun script."
                exit 1
    fi
else
    echo "Rerun after merge conflict resolution or restored connection."
    git checkout --quiet sync-$3
    echo "Try to merge with right first"
    if ! git merge -m "Merge to sync between $1 and $2" --log right/$3; then
                >&2 echo "Merge conflict. Solve it manually, commit and rerun script."
                exit 1
    fi
    if ! git merge -m "Merge to sync between $1 and $2" --log left/$3; then
                >&2 echo "Merge conflict. Solve it manually, commit and rerun script."
                exit 1
    fi
fi

echo "Push merged changes in $3 to $2"
read -p "[USER] continue (y/n)" REPLY
# echo "REPLY: $REPLY"
if [ "$REPLY" == "y" ]; then
    if ! git push $2 HEAD:$3; then
        >&2 echo "Fatal: unable to push to $2, rerun the script as soon as connection restored."
        exit 1
    fi
else
    echo "Abort not pushed to $2"
fi  

echo "Push merged changes in $3 to $1"
read -p "[USER] continue (y/n)" REPLY
# echo "REPLY: $REPLY"
if [ "$REPLY" == "y" ]; then
    if ! git push $1 HEAD:$3; then
        >&2 echo "Fatal: unable to push to $1, rerun the script as soon as connection restored."
        exit 1
    fi
else
    echo "Abort not pushed to $1"
fi  

git checkout --quiet master
git branch -D --quiet sync-$3
echo "Done."

Case 1

Bash
1
2
3
4
5
6
7
cd dir-of-repo
# copy the script there
wget https://gitlab.phaidra.org/imgw/computer-resources/-/raw/master/Git/git-repos-sync
# make executable
chmod +x git-repos-sync
# execute the script
./git-repos-sync [URL] [URL] [Branch]

Case 2

Bash
1
2
3
4
5
6
7
8
# Create a new folder to do the sync, can be any name
mkdir sync-repos
# copy the script there
wget https://gitlab.phaidra.org/imgw/computer-resources/-/raw/master/Git/git-repos-sync
# make executable
chmod +x git-repos-sync
# execute the script
./git-repos-sync [URL] [URL] [Branch]

Last update: February 1, 2024
Created: October 14, 2021