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
- On GitLab, go to Projects > New Project.
- Select Create blank project.
- Fill in Project name, Visibility level (public/private), and any optional details.
- 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
- Push your branch to GitLab.
- Go to your GitLab project, and it will suggest opening a merge request.
- Fill in the title and description, referencing issues if necessary (
Closes #issue-number
).
- Assign reviewers, add labels, and set any linked issues or milestones.
- Click Create merge request.
Review and Merge a Merge Request
- Go to the Merge Requests tab and open the MR.
- Review changes: Add comments, approve, or request changes.
- Once approved, click Merge to add changes to the target branch.
Managing Issues
Creating and Linking Issues
- Go to the Issues tab and click New Issue.
- Add the title and description of the issue.
- Assign it to team members, add labels, and set a milestone if relevant.
- 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
Command | Description |
---|
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 --all | Fetch updates from all branches without merging. |
git merge [branch-name] | Merge another branch into the current one. |
git log --oneline | View commit history in a single line per commit. |
git status | See changes staged, unstaged, and untracked. |
GitLab CI/CD (Pipelines)
Basic Pipeline Setup
- Create a
.gitlab-ci.yml
file in the root of your repo.
- 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
- Create a
.gitlab-ci.yml
file to define your GitLab Pages site.
- Add your static files (HTML, CSS, JavaScript) in a directory like
public
.
- Example
.gitlab-ci.yml
:
pages:
stage: deploy
script:
- mkdir .public
- cp -r * .public
artifacts:
paths:
- .public
only:
- main
- Go to Settings > Pages to find your GitLab Pages URL.
Forking and Contributing to Others’ Repos
Forking a Repository
- On GitLab, click Fork on the original repository.
- Clone your forked repo to your local machine:
git clone [forked-repo-URL]
Contributing Changes
- Make changes in a new branch, commit, and push to your fork.
- Go to the original repository and open a merge request from your forked branch.
- 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
- Use Descriptive Commit Messages: Clearly explain each change.
- Keep MRs Small: Make it easier to review by focusing on single changes or fixes.
- Automate with CI/CD: Set up pipelines to automate testing and deployment.
- Organize with Issues, Milestones, and Epics: Track progress and prioritize tasks.
- Document in Wiki: Use the project wiki to document code and processes for easier collaboration.