Core Concepts

  • Repository (Repo): A project’s main directory on GitLab that contains code, files, and version history.
  • Branch: A parallel version of the project, used to work on new features or fixes independently of the main codebase.
  • Merge Request (MR): GitLab’s version of a pull request, allowing code changes to be reviewed, discussed, and merged.
  • Pipeline: A set of CI/CD stages and jobs that automate testing, building, and deployment.
  • Issue: Used to track bugs, tasks, and feature requests within a repository.
  • Milestones and Epics: Organize issues into milestones for tracking progress and epics for larger, multi-issue tasks.

GitLab Workflow Basics

Create a New Repository

  1. On GitLab, go to Projects > New Project.
  2. Select Create blank project.
  3. Fill in Project name, Visibility level (public/private), and any optional details.
  4. Click Create project.

Clone a Repository

git clone [URL]
  • Downloads the repository from GitLab to your local machine.

Create and Switch Branches

# Create a new branch
git checkout -b [branch-name]
 
# Switch to an existing branch
git checkout [branch-name]

Commit Changes

# Stage all changes
git add .
 
# Commit with a message
git commit -m "Your message here"

Push Changes to GitLab

git push origin [branch-name]

Working with Merge Requests (MRs)

Open a Merge Request

  1. Push your branch to GitLab.
  2. Go to your GitLab project, and it will suggest opening a merge request.
  3. Fill in the title and description, referencing issues if necessary (Closes #issue-number).
  4. Assign reviewers, add labels, and set any linked issues or milestones.
  5. Click Create merge request.

Review and Merge a Merge Request

  1. Go to the Merge Requests tab and open the MR.
  2. Review changes: Add comments, approve, or request changes.
  3. Once approved, click Merge to add changes to the target branch.

Managing Issues

Creating and Linking Issues

  1. Go to the Issues tab and click New Issue.
  2. Add the title and description of the issue.
  3. Assign it to team members, add labels, and set a milestone if relevant.
  4. Link issues in merge requests or commits using #issue-number.

Closing Issues

  • Automatically close an issue by referencing it in an MR description or commit message with keywords like Closes #issue-number.

GitLab Commands Cheat Sheet

Basic Git Commands for GitLab

CommandDescription
git clone [URL]Clone a GitLab repo locally.
git checkout -b [branch-name]Create and switch to a new branch.
git checkout [branch-name]Switch to an existing branch.
git add [file-name] or git add .Stage changes for commit.
git commit -m "message"Commit staged changes with a message.
git push origin [branch-name]Push changes to GitLab.
git pull origin [branch-name]Update local branch with GitLab changes.
git fetch --allFetch updates from all branches without merging.
git merge [branch-name]Merge another branch into the current one.
git log --onelineView commit history in a single line per commit.
git statusSee changes staged, unstaged, and untracked.

GitLab CI/CD (Pipelines)

Basic Pipeline Setup

  1. Create a .gitlab-ci.yml file in the root of your repo.
  2. Define stages (e.g., build, test, deploy) and jobs for each stage.
stages:
  - build
  - test
  - deploy
 
build_job:
  stage: build
  script:
    - echo "Building the application..."
 
test_job:
  stage: test
  script:
    - echo "Running tests..."
 
deploy_job:
  stage: deploy
  script:
    - echo "Deploying the application..."

Common GitLab CI/CD Keywords

  • stages: Define the sequence of stages (e.g., build, test, deploy).
  • script: Commands to execute in each job.
  • only: Specify which branches or events trigger a job (e.g., only: [main]).
  • artifacts: Files or directories to retain after the job runs (e.g., logs or builds).
  • cache: Speed up builds by caching dependencies or frequently-used files.

Running Pipelines

  • Pipelines run automatically on push, pull requests, or manually triggered events.
  • To monitor pipeline progress, go to the CI/CD > Pipelines tab in your project.

GitLab Pages

  1. Create a .gitlab-ci.yml file to define your GitLab Pages site.
  2. Add your static files (HTML, CSS, JavaScript) in a directory like public.
  3. Example .gitlab-ci.yml:
pages:
  stage: deploy
  script:
    - mkdir .public
    - cp -r * .public
  artifacts:
    paths:
      - .public
  only:
    - main
  1. Go to Settings > Pages to find your GitLab Pages URL.

Forking and Contributing to Others’ Repos

Forking a Repository

  1. On GitLab, click Fork on the original repository.
  2. Clone your forked repo to your local machine:
    git clone [forked-repo-URL]

Contributing Changes

  1. Make changes in a new branch, commit, and push to your fork.
  2. Go to the original repository and open a merge request from your forked branch.
  3. Describe your changes, reference any issues, and create the MR.

GitLab Shortcuts and Tips

  • @mentions: Tag team members in issues, MRs, and comments using @username.
  • Markdown Support: Use Markdown for formatting text in issues, MRs, and comments (e.g., # Heading, **bold**, *italic*).
  • Reference Issues: Link issues or MRs using #issue-number or !MR-number in comments and descriptions.
  • Auto-close Issues: Use keywords like Closes #issue-number in MR descriptions or commit messages to close issues automatically.
  • Epics and Milestones: Organize and track larger projects with epics and milestones for better progress tracking.

GitLab Best Practices

  1. Use Descriptive Commit Messages: Clearly explain each change.
  2. Keep MRs Small: Make it easier to review by focusing on single changes or fixes.
  3. Automate with CI/CD: Set up pipelines to automate testing and deployment.
  4. Organize with Issues, Milestones, and Epics: Track progress and prioritize tasks.
  5. Document in Wiki: Use the project wiki to document code and processes for easier collaboration.