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:
Account Settings
Access tokens
add new token
Specify Token Name
Specify Expiration date
Specify scope, which can be full access (api) or just read (read_api). There are multiple options. docs
Create
Clone the repo with HTTPS url.
Store this token for easy access:
Bash
123456
# Store or cache (in .git-credentials)# or use an personal access token (can only be used for git operations)
$gitconfig--globalcredential.helperstore
# this will ask your username # Use the access token as password.
$gitclonehttps://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 910111213141516171819202122232425
$gitbranchtest# Modify something
$vimREADME.md
$gitcommit-a-m'Updated the README.md'
$gitpush
fatal:Thecurrentbranchtesthasnoupstreambranch.
Topushthecurrentbranchandsettheremoteasupstream,use
gitpush--set-upstreamorigintest
Tohavethishappenautomaticallyforbrancheswithoutatracking
upstream,see'push.autoSetupRemote'in'git help config'.
# What you need to do is run that command:
$gitpush--set-upstreamorigintest
Total0(delta0),reused0(delta0),pack-reused0(from0)
remote:
remote:Tocreateamergerequestfortest,visit:
remote:https://gitlab.phaidra.org/Group/RepositoryName/-/merge_requests/new?merge_request%5Bsource_branch%5D=test
remote:
Togitlab.phaidra.org:Group/RepositoryName.git
*[newbranch]test->test
branch'test'setuptotrack'origin/test'.
# done
$gitpush
Everythingup-to-date
How To Configure Git?
There are a lot of options, but there are the basic options that you should set:
Bash
123456789
# set you username and mail address
$gitconfig--globaluser.name"Wind Cloudy"
$gitconfig--globaluser.emailwcloudy@univie.ac.at
# set that you want to merge conflicts.
$gitconfigpull.rebasefalse# set your default editor
$gitconfigcore.editor[vim/nano/...]# edit your Configuration
$gitconfig-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:
You have already a local copy of either of the repositories (e.g. GitLab or GitHub)
#!/bin/bash# author: https://github.com/adidik/git-repos-sync# sync two git repositories (github and gitlab)# modified: 23.4.2021 (MB)# License: MITif["$#"-ne3];then>&2echo"Usage: git-repos-sync <repository URL> <repository URL> <branch-to-sync>"exit1fi# Check if you are at root level (fails if not)if!gitrev-parse--show-toplevel;then>&2echo"Git repo found"if[$PWD==$(gitrev-parse--show-toplevel)];thenecho"Not at root of local repo"echo$(gitrev-parse--show-toplevel)exit1fifiif[!-d".git"];thenecho"Git repo for syncronization is not found, creating one..."gitinit
gitfetch$1gitcheckout-bmasterFETCH_HEADechofiif!gitdiff-index--quietHEAD--;then>&2echo"Local modifications found, looks like your in the conflict resolution. Resolve a conflict and commit. Then rerun script."exit1fiecho"Left: $1"echo"Right: $2"echoecho"Fetch latest commits from branch $3 in $1"if!gitfetch-u$1$3:left/$3;then>&2echo"Fatal: unable to fetch from $1, rerun the script as soon as connnection restored."exit1fiecho"Fetch latest commits from branch $3 in $2"if!gitfetch-u$2$3:right/$3;then>&2echo"Fatal: unable to fetch from $2, rerun the script as soon as connnection restored."exit1fiifgitcheckout--quiet-bsync-$3right/$3;thenecho"Merge branches from left and right if necessary."if!gitmerge-m"Merge to sync between $1 and $2"--logleft/$3;then>&2echo"Merge conflict. Solve it manually, commit and rerun script."exit1fielseecho"Rerun after merge conflict resolution or restored connection."gitcheckout--quietsync-$3echo"Try to merge with right first"if!gitmerge-m"Merge to sync between $1 and $2"--logright/$3;then>&2echo"Merge conflict. Solve it manually, commit and rerun script."exit1fiif!gitmerge-m"Merge to sync between $1 and $2"--logleft/$3;then>&2echo"Merge conflict. Solve it manually, commit and rerun script."exit1fifiecho"Push merged changes in $3 to $2"read-p"[USER] continue (y/n)"REPLY
# echo "REPLY: $REPLY"if["$REPLY"=="y"];thenif!gitpush$2HEAD:$3;then>&2echo"Fatal: unable to push to $2, rerun the script as soon as connection restored."exit1fielseecho"Abort not pushed to $2"fiecho"Push merged changes in $3 to $1"read-p"[USER] continue (y/n)"REPLY
# echo "REPLY: $REPLY"if["$REPLY"=="y"];thenif!gitpush$1HEAD:$3;then>&2echo"Fatal: unable to push to $1, rerun the script as soon as connection restored."exit1fielseecho"Abort not pushed to $1"fi
gitcheckout--quietmaster
gitbranch-D--quietsync-$3echo"Done."
Case 1
Bash
1234567
cddir-of-repo
# copy the script there
wgethttps://gitlab.phaidra.org/imgw/computer-resources/-/raw/master/Git/git-repos-sync
# make executable
chmod+xgit-repos-sync
# execute the script
./git-repos-sync[URL][URL][Branch]
Case 2
Bash
12345678
# Create a new folder to do the sync, can be any name
mkdirsync-repos
# copy the script there
wgethttps://gitlab.phaidra.org/imgw/computer-resources/-/raw/master/Git/git-repos-sync
# make executable
chmod+xgit-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:
Go to "Settings > Repository > Mirroring repositories"
Enter your Github repo with your username in front https://<github username>@github.com/path/to/your/repo.git
In the password field, enter your Github token
push is the only option for our GitLab
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.