GitLab CLI

GitLab CLI (glab) cheatsheet — create MRs, manage issues, trigger pipelines. glab mr create, glab issue list, glab pipeline status. Full GitLab CLI reference.

5 min read

What it is

The GitLab CLI (glab) is a command-line tool for interacting with GitLab, allowing you to manage projects, issues, merge requests, pipelines, and more directly from your terminal.

Installation

Linux

curl -s https://gitlab.com/gitlab-org/cli/-/releases/v1.32.0/downloads/glab_1.32.0_Linux_x86_64.deb -o glab.deb
sudo dpkg -i glab.deb
# or for RPM based systems
curl -s https://gitlab.com/gitlab-org/cli/-/releases/v1.32.0/downloads/glab_1.32.0_Linux_x86_64.rpm -o glab.rpm
sudo rpm -i glab.rpm

macOS

brew install glab

Windows

Download the appropriate .msi file from the GitLab CLI releases page and run it.

Core Concepts

Authentication

glab uses a personal access token (PAT) for authentication. You can generate one in your GitLab user settings under "Access Tokens". It’s recommended to store this token securely.

Commands / Usage

Authentication & Configuration

  • Initialize glab:

    glab auth login
    

    This command guides you through setting up your GitLab instance URL and personal access token.

  • Configure glab:

    glab config set gitlab_url "https://gitlab.example.com"
    glab config set token "YOUR_PERSONAL_ACCESS_TOKEN"
    

    Manually set your GitLab instance URL and token.

  • View current configuration:

    glab config list
    

    Displays the current glab configuration.

Projects

  • List projects:

    glab repo list
    

    Lists projects you have access to.

  • Clone a project:

    glab repo clone gitlab-org/gitlab
    

    Clones the specified project to your local machine.

  • Create a project:

    glab repo create my-new-project --description "A cool project" --visibility public
    

    Creates a new project in your GitLab instance.

  • Get project information:

    glab repo view gitlab-org/gitlab
    

    Displays detailed information about a project.

Issues

  • List issues:

    glab issue list --project gitlab-org/gitlab
    

    Lists issues for a specific project.

  • Create an issue:

    glab issue create --project gitlab-org/gitlab --title "Bug in login page" --description "Users are unable to log in." --assignee @username --label "bug"
    

    Creates a new issue with a title, description, assignee, and labels.

  • View an issue:

    glab issue view gitlab-org/gitlab 123
    

    Displays details of a specific issue.

  • Close an issue:

    glab issue close gitlab-org/gitlab 123
    

    Closes a specified issue.

  • Reopen an issue:

    glab issue reopen gitlab-org/gitlab 123
    

    Reopens a closed issue.

  • Add a comment to an issue:

    glab issue comment gitlab-org/gitlab 123 --body "This is a follow-up comment."
    

    Adds a comment to an existing issue.

Merge Requests (MRs)

  • List merge requests:

    glab mr list --project gitlab-org/gitlab --state opened
    

    Lists opened merge requests for a project.

  • Create a merge request:

    glab mr create --project gitlab-org/gitlab --source-branch feature-branch --target-branch main --title "Implement new feature" --description "Adds awesome new functionality."
    

    Creates a merge request from a source branch to a target branch.

  • View a merge request:

    glab mr view gitlab-org/gitlab 456
    

    Displays details of a specific merge request.

  • Accept a merge request:

    glab mr accept gitlab-org/gitlab 456 --squash --remove-source-branch
    

    Accepts a merge request, optionally squashing commits and removing the source branch.

  • Close a merge request:

    glab mr close gitlab-org/gitlab 456
    

    Closes a merge request.

  • Approve a merge request:

    glab mr approve gitlab-org/gitlab 456
    

    Approves a merge request.

  • Add a comment to a merge request:

    glab mr comment gitlab-org/gitlab 456 --body "Looks good, LGTM."
    

    Adds a comment to an existing merge request.

Pipelines

  • List pipelines:

    glab pipeline list --project gitlab-org/gitlab
    

    Lists pipelines for a project.

  • View a pipeline:

    glab pipeline view gitlab-org/gitlab 789
    

    Displays details of a specific pipeline.

  • Trigger a pipeline:

    glab pipeline trigger --project gitlab-org/gitlab --ref main
    

    Triggers a pipeline for a specific branch.

  • Cancel a pipeline:

    glab pipeline cancel gitlab-org/gitlab 789
    

    Cancels a running pipeline.

Releases

  • List releases:

    glab release list --project gitlab-org/gitlab
    

    Lists releases for a project.

  • Create a release:

    glab release create --project gitlab-org/gitlab --tag-name v1.0.0 --name "Version 1.0.0" --description "Initial stable release."
    

    Creates a new release with a tag, name, and description.

Miscellaneous

  • Get help:
    glab --help
    glab issue --help
    
    Displays help information for glab or specific commands.

Common Patterns

  • Create a branch and open an MR directly:

    git checkout -b new-feature && glab mr create --fill --assignee @me
    

    This creates a new branch, commits your changes, and then opens an MR assigned to you, filling in as much information as possible from your Git history.

  • View the latest pipeline for the current branch:

    glab pipeline view --ref $(git rev-parse --abbrev-ref HEAD)
    

    Shows the details of the most recent pipeline run for the branch you are currently on.

  • Approve and merge a MR assigned to you:

    glab mr list --assignee @me --state opened | awk '{print $1}' | xargs -I {} glab mr approve your-project {} && glab mr list --assignee @me --state opened | awk '{print $1}' | xargs -I {} glab mr accept your-project {}
    

    This is a more complex example that first lists all opened MRs assigned to you, approves them, and then attempts to accept them. Use with caution.

  • Find and close an issue by title:

    ISSUE_ID=$(glab issue list --project my-project --search "Stale data in dashboard" --limit 1 | awk '{print $1}') && glab issue close my-project $ISSUE_ID
    

    Searches for an issue by title, extracts its ID, and then closes it.

Gotchas

  • Project Naming: When referring to projects, use the full path like group/subgroup/project. If you are in the project’s directory, you can often omit the project path.
  • Authentication Token Permissions: Ensure your personal access token has the necessary scopes (e.g., api, read_repository, write_repository) for the actions you intend to perform.
  • Default Project: If you don’t specify --project, glab will try to infer it from your current Git repository’s remote. If this fails or is ambiguous, you’ll need to provide it explicitly.
  • --fill Flag: The --fill flag for glab mr create is very convenient but relies on Git commit messages and branch names. If these are not descriptive, the generated MR title and description might be unhelpful.
  • Interactive Prompts: Many commands will prompt you for confirmation or additional details. If you want to automate these, look for non-interactive flags or ensure you provide all necessary arguments.