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.

How To 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).

How To Setup an Access Token in Git?

In your GitLab account you can define Access Tokens, which can be used to share git access to your projects with some API or Server. This is preferred to storing you GitLab credentials. The best option is of course to use ssh-keys with a passphrase (even a simple one).

Steps:

  1. Account Settings
  2. Access tokens
  3. add new token
  4. Specify Token Name
  5. Specify Expiration date
  6. Specify scope, which can be full access (api) or just read (read_api). There are multiple options. docs
  7. Create
  8. Clone the repo with HTTPS url.
  9. Store this token for easy access:
    Bash
    1
    2
    3
    4
    5
    6
    # Store or cache (in .git-credentials)
    # or use an personal access token (can only be used for git operations)
    $ git config --global credential.helper store
    # this will ask your username 
    # Use the access token as password.
    $ git clone https://gitlab.phaidra.org/Group/RepositoryName.git
    

How To Sync a Branch?

When you create a branch on the commandline or in GitLab and you want to push local changes to the remote, you need to det the remote first:

Bash
 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
$ git branch test
# Modify something
$ vim README.md
$ git commit -a -m 'Updated the README.md'
$ git push
fatal: The current branch test has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin test

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
# What you need to do is run that command:
$ git push --set-upstream origin test
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: 
remote: To create a merge request for test, visit:
remote:   https://gitlab.phaidra.org/Group/RepositoryName/-/merge_requests/new?merge_request%5Bsource_branch%5D=test
remote: 
To gitlab.phaidra.org:Group/RepositoryName.git
 * [new branch]      test -> test
branch 'test' set up to track 'origin/test'.
# done
$ git push
Everything up-to-date

How To Configure Git?

There are a lot of options, but there are the basic options that you should set:

Bash
1
2
3
4
5
6
7
8
9
# set you username and mail address
$ git config --global user.name "Wind Cloudy"
$ git config --global user.email wcloudy@univie.ac.at
# set that you want to merge conflicts.
$ git config pull.rebase false
# set your default editor
$ git config core.editor [vim/nano/...]
# edit your Configuration
$ git config -e

Optional:

Bash
1

How To 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]

How To Sync Gitlab and GitHub Repositories?

You can set up some CI/CD yourself, but Gitlab will automatically do this for you:

  1. Go to "Settings > Repository > Mirroring repositories"
  2. Enter your Github repo with your username in front https://<github username>@github.com/path/to/your/repo.git
  3. In the password field, enter your Github token
  4. push is the only option for our GitLab
  5. Press Mirror repository

Whenever you push something to GitLab it will automatically sync that with GitHub, if it can. If there are different commit on both repos, then it does not do it.


Last update: April 2, 2025
Created: March 8, 2023