logo
eng-flag

GitLab Cheat Sheet

Table of Contents

  1. Account Management
  2. Project Management
  3. Repository Operations
  4. Issues and Merge Requests
  5. CI/CD
  6. GitLab Pages
  7. GitLab Runner
  8. Collaboration Features
  9. GitLab Security
  10. GitLab API
  11. Best Practices

Account Management

Create a new account

  1. Go to https://gitlab.com
  2. Click "Register now"
  3. Fill in your details and click "Register"

Enable two-factor authentication (2FA)

  1. Go to Settings > Account
  2. Scroll to "Two-Factor Authentication" section
  3. Click "Enable two-factor authentication"

Add an SSH key

  1. Generate an SSH key pair
ssh-keygen -t ed25519 -C "your_email@example.com"
  1. Go to Settings > SSH Keys
  2. Paste your public key and click "Add key"

Project Management

Create a new project

  1. Click "New project" on your dashboard
  2. Choose "Create blank project"
  3. Fill in project details and click "Create project"

Import a project

  1. Click "New project" on your dashboard
  2. Choose "Import project"
  3. Select the source (e.g., GitHub, Bitbucket) and follow the prompts

Create a group

  1. Click "Groups" in the top menu
  2. Click "New group"
  3. Fill in group details and click "Create group"

Repository Operations

Clone a repository

git clone https://gitlab.com/username/repository.git

Create a new branch

git checkout -b new-branch-name

Push changes to GitLab

git push origin branch-name

Create a merge request from the command line

git push -u origin branch-name
gitlab mr create --source-branch branch-name --target-branch main

Issues and Merge Requests

Create a new issue

  1. Go to your project's "Issues" page
  2. Click "New issue"
  3. Fill in the title and description
  4. Click "Submit issue"

Create a merge request

  1. Go to your project's "Merge Requests" page
  2. Click "New merge request"
  3. Select the source and target branches
  4. Fill in the title and description
  5. Click "Submit merge request"

Review a merge request

  1. Go to the "Merge Requests" page
  2. Click on the merge request you want to review
  3. Leave comments, approve, or request changes

Resolve merge conflicts

  1. In the merge request, click "Resolve conflicts"
  2. Edit the conflicting files
  3. Stage the changes and commit
  4. Push the changes to resolve the conflicts

CI/CD

Set up CI/CD

  1. Create a .gitlab-ci.yml file in your repository root
  2. Define your pipeline stages and jobs

Example .gitlab-ci.yml:

stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - echo "Building the project..."

test_job:
  stage: test
  script:
    - echo "Running tests..."

deploy_job:
  stage: deploy
  script:
    - echo "Deploying to production..."
  only:
    - main

View pipeline results

  1. Go to your project's "CI/CD > Pipelines" page
  2. Click on a pipeline to see its details and job logs

GitLab Pages

Enable GitLab Pages

  1. Create a .gitlab-ci.yml file in your repository
  2. Add a job that generates your static site

Example for a Jekyll site:

pages:
  stage: deploy
  script:
    - bundle exec jekyll build -d public
  artifacts:
    paths:
      - public
  only:
    - main

Access your GitLab Pages site

GitLab Runner

Install GitLab Runner

Register a runner

gitlab-runner register

Start the runner

gitlab-runner start

Collaboration Features

Add project members

  1. Go to your project's "Settings > Members"
  2. Click "Invite members"
  3. Enter the user's name or email and select their role

Create a team

  1. Go to your group's page
  2. Click "Subgroups and projects"
  3. Click "New subgroup" to create a team

Code review settings

  1. Go to your project's "Settings > Merge requests"
  2. Configure approval rules and other merge request settings

GitLab Security

Enable Dependency Scanning

  1. Go to "Security & Compliance > Configuration"
  2. Enable "Dependency Scanning"

Set up Container Scanning

  1. Go to "Security & Compliance > Configuration"
  2. Enable "Container Scanning"

Create a security policy

  1. Create a SECURITY.md file in your repository
  2. Define your security policy and vulnerability reporting process

GitLab API

Generate a personal access token

  1. Go to Settings > Access Tokens
  2. Create a new token with the necessary scopes

Make an API request

Using curl:

curl --header "PRIVATE-TOKEN: your_access_token" "https://gitlab.com/api/v4/projects"

Using Python requests:

import requests

url = "https://gitlab.com/api/v4/projects"
headers = {"PRIVATE-TOKEN": "your_access_token"}
response = requests.get(url, headers=headers)
print(response.json())

Best Practices

  1. Use meaningful commit messages: Write clear, concise commit messages that explain the changes made.

  2. Utilize merge request templates: Create templates for merge requests to ensure consistent and comprehensive descriptions.

  3. Leverage GitLab CI/CD: Automate your testing, building, and deployment processes using GitLab CI/CD pipelines.

  4. Use GitLab Issues for project management: Organize your work using issues, labels, milestones, and boards.

  5. Implement code review guidelines: Establish and follow code review best practices within your team.

  6. Utilize GitLab Wiki: Document your project thoroughly using GitLab's built-in Wiki feature.

  7. Regular backups: Use GitLab's backup features to ensure your data is safe and recoverable.

  8. Monitor your GitLab instance: If self-hosting, use GitLab's monitoring tools to keep track of your instance's health.

  9. Use merge request approvals: Set up approval rules to ensure code quality and security.

  10. Leverage GitLab's built-in security features: Utilize features like SAST, dependency scanning, and container scanning to improve your project's security.

2024 © All rights reserved - buraxta.com