GitLab: glab official CLI tool for repository operations

Gitlab has an official CLI tool that allows you to perform many of the operations you may currently perform in the web UI.

Among its many uses, the ‘glab‘ utility will allow you to fork repositories, manage issues, merge PR, and create releases all from the console for ease of use or automation.

Below I will describe its installation on Ubuntu and then examples of usage.

Install

Download the latest archive from the gitlab-org/cli release page.

# download binary
latest_version=v1.24.1
tar_file="glab_${latest_version//v}_Linux_x86_64.tar.gz"
tar_url="https://gitlab.com/$project/-/releases/$latest_version/downloads/$tar_file"
wget $tar_url -O /tmp/$tar_file

# extract binary, then move to PATH
tar xvfz /tmp/$tar_file bin/glab --strip=1
sudo chown root:root glab
sudo mv glab /usr/local/bin/.

# sanity test
glab --version

OR if you want to use the script I placed on github, it will do all the steps above including dynamically retrieving the latest release version.

wget https://raw.githubusercontent.com/fabianlee/blogcode/master/gitlab/install-glab-cli.sh
chmod +x install-glab-cli.sh
./install-glab-cli.sh

Gitlab Personal Access Token

The ‘glab’ utility requires authentication to reach the server.  Login to gitlab.com and from the top-right menu select “Edit Profile” > “Access Tokens“.

Check “api” and “write_repository” scopes, and then press “Create personal access token”.  Be sure to copy the value provided, because it cannot be retrieved again (only refreshed).

Authenticate

Provide ‘glab’ its configuration and authentication token using ‘glab auth login’.

$ glab auth login
? What GitLab instance do you want to log into? gitlab.com
- Logging into gitlab.com

Tip: you can generate a Personal Access Token here https://gitlab.com/-/profile/personal_access_tokens
The minimum required scopes are 'api' and 'write_repository'.
? Paste your authentication token: **************************
? Choose default git protocol HTTPS
? Authenticate Git with your GitLab credentials? Yes
- glab config set -h gitlab.com git_protocol https
✓ Configured git protocol
- glab config set -h gitlab.com api_protocol https
✓ Configured API protocol
✓ Logged in as mygitlabuser1

Create repo

# create public repo
$ glab repo create mytest123 --public
✓ Created repository Fabian Lee / mytest123 on GitLab: https://gitlab.com/fabianlee/mytest123

# list all personal repo
$ glab repo list
Showing 1 of 1 projects (Page 1 of 1)
fabianlee/mytest123 git@gitlab.com:fabianlee/mytest123.git

# clone locally and commit change
$ glab repo clone https://gitlab.com/fabianlee/mytest123
$ cd mytest123
$ echo "initial" > README.md
$ git add README.md
$ git commit -a -m "initial files"; git push

Delete repo

glab repo list
glab repo delete fabianlee/mytest123 -y

Create personal fork of public project

Fork my gitlab public project to your own personal fork.

# create personal fork of my test repo
$ glab repo fork https://gitlab.com/fabianlee/mytest123.git
- Forking fabianlee/mytest123
- scheduled
- finished
✓ Created fork flgmail999/mytest123
? Would you like to clone the fork? Yes
Cloning into 'mytest123'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Updating upstream
From https://gitlab.com/fabianlee/mytest123
 * [new branch]      master     -> upstream/master
✓ Cloned fork

# glab tool automatically assigns 'upstream' remote
$ cd mytest123
$ git remote -v

# list of all your personal git repo
$ glab repo list

Create Merge Request from personal fork to remotely owned repo

If you wanted to submit the changes made on your personal fork back to the upstream repository, you can create a merge request with “glab mr create”.  glab can have issues auto-sensing where this merge request should be submitted, so we have to fully specify the origin and upstream.

# make change and commit to personal fork
echo "$(date)" >> README.md
git commit -a -m "added line"; git push

# show 'origin' and 'upstream' remotes
# which will be used to define upcoming flags
git remote -v

# get origin and upstream owner/repo spec
origin_owner_repo=$(git remote get-url origin | cut -d'/' -f4- | sed 's/\.git'//)
upstream_owner_repo=$(git remote get-url upstream | cut -d'/' -f4- | sed 's/\.git'//)

# create Merge Request against upstream repository
current_branch=$(git branch --show-current)
glab mr create --fill --squash-before-merge --source-branch $current_branch --target-branch $current_branch --head $origin_owner_repo --repo $upstream_owner_repo -y

If external users get a 403 error when trying to submit a Merge Request to a public repo you own (even if it is public), navigate to Project Information > Members, and click “Invite Members”.  After adding the user, they should be able to submit the MR.

Create Merge Request from personal fork to personally owned repo

If you own both the personal fork as well as the parent repo, then glab is able to auto-sense the ‘head’ and ‘repo’ flags explicitly used in the section above.

# make change and commit to personal fork
echo "$(date)" >> README.md
git commit -a -m "added line"; git push

# create Merge Request against upstream repository
current_branch=$(git branch --show-current)
glab mr create --fill --squash-before-merge --source-branch $current_branch --target-branch $current_branch -y

If you get an error message ‘You must be on a different branch other than “xxx”‘, that means glab may not have auto-sensed your source and destination repositories correctly and you need to explicitly specify the ‘head’ and ‘repo’ flags as done in the section above.

 

REFERENCES

gitlab.com, install gh CLI tool

gitlab.com, GraphQL API

maxivanov.io, graphQL with curl

zaquestion/lab – alternative open-source CLI tool for gitlab

github.com issues – known problem with merge request creation and “you must be on a different branch”

git javascript visualizer – shows visual graph for branches, commits, merges, etc

 

NOTES

Get latest version of release using GraphQL call against gitlab

sudo apt install jq -y

latest_version=$(curl -s \
  -X POST \
  -H 'Content-Type: application/json' \
  --data-raw '[{"operationName":"allReleases","variables":{"fullPath":"gitlab-org/cli","first":10,"sort":"RELEASED_AT_DESC"},"query":"query allReleases($fullPath: ID!, $first: Int, $last: Int, $before: String, $after: String, $sort: ReleaseSort) {\n  project(fullPath: $fullPath) {\n    id\n    releases(\n      first: $first\n      last: $last\n      before: $before\n      after: $after\n      sort: $sort\n    ) {\n nodes {\n ...Release\n }\n }\n }\n}\n\nfragment Release on Release {\n  id\n  name\n }\n  \n"}]' \
  https://gitlab.com/api/graphql | jq -r ".[].data.project.releases.nodes[0].name")
echo "latest_version=$latest_version"

Create fork of one of your own repo (NOTE: possible with gitlab, but not with github)

glab repo fork https://gitlab.com/fabianlee/mytest123 --name myfork456 --path myfork456 --clone