In a GitLab team, nobody changes main directly. Instead you:
- make a branch,
- push it to GitLab,
- open a merge request (MR for short).
The MR is where the team looks at your change, the pipeline tests it, and someone clicks Merge. In this lesson you will do all of this from the terminal.
In simple words
- Merge request (MR): a request to merge one branch into another. It is where code is reviewed, discussed and tested before merging.
- SSH vs HTTPS: two ways to connect Git to GitLab: SSH uses a key, HTTPS uses a token.
- Merge method: how the MR is merged: a merge commit, a squash into one commit, or a fast-forward.
What you'll learn
- Make an SSH key and add it to GitLab
- Make a project and push code to it
- The branch → push → merge request steps
- Draft MRs,
Closes #N, and the merge options - How to update your laptop after a merge
SSH or HTTPS?
Git can talk to GitLab in two ways:
| SSH | HTTPS | |
|---|---|---|
| Link | git@gitlab.com:acme/storefront.git | https://gitlab.com/acme/storefront.git |
| How you log in | A key file on your computer | Your username and a token as the password |
| Best for | Your laptop: set it up once, no passwords | CI jobs, or networks that block SSH |
Most engineers use SSH on their laptop. Try to connect before you have a key:
ssh -T git@gitlab.com # fails: GitLab doesn't know your key yetgit@gitlab.com: Permission denied (publickey).GitLab doesn't know you yet. Make a key. Ed25519 is the modern type: short, fast and safe. Put your email at the end so you can recognise the key later:
ssh-keygen -t ed25519 -C "lynuxpro@example.com" -f ~/.ssh/id_ed25519 -N ""cat ~/.ssh/id_ed25519.pubOn a real laptop, leave out -N "". Then it asks you for a passphrase (a password for the key). This keeps the key safe if your laptop is stolen. Your computer remembers the passphrase, so you only type it once.
You now have two files:
- The file ending in
.pubis the public key. It is safe to share. Copy it into GitLab: click your picture, then Preferences > SSH Keys > Add new key. Set an end date and save. - The file without
.pubis the private key. Never share it or paste it anywhere.
Now the test works:
ssh -T git@gitlab.comWelcome to GitLab, @lynuxpro!Making a project
On the website: New project > Create blank project, choose the group (for example acme) and type a name. If you will push code you already have, don't tick Initialize repository with a README.
With glab it is one command:
glab repo create acme/storefront --private --description "Shop web front end"Then connect your local code to it and push. The storefront in the lab is already connected, but these are the commands for a new project:
git remote add origin git@gitlab.com:acme/storefront.gitgit push -u origin main-u means "remember this". After it, a plain git push or git pull on this branch knows to use origin/main.
Who can see the project (visibility) matters:
- Private: only members
- Internal: anyone logged in to your company's GitLab
- Public: everyone on the internet
Company code should be Private or Internal.
The merge request steps
Your task: issue #2 asks for more information in the /health page. First, make a branch. Give it a name that says what kind of change it is and what it does:
cd ~/storefrontgit switch -c feature/health-regiongrep -n "health" src/server.jsSwitched to a new branch 'feature/health-region'7:app.get("/health", (_req, res) => res.json({ status: "ok", version }));Make the change and commit it:
sed -i 's/status: "ok", version/status: "ok", version, region: "ap-south-1"/' src/server.jsgit diffgit commit -am "Report region in /health"diff --git a/src/server.js b/src/server.jsindex 052e9c6..be96c37 100644--- a/src/server.js+++ b/src/server.js@@ -4,7 +4,7 @@ const app = express(); const version = process.env.APP_VERSION ?? "dev";-app.get("/health", (_req, res) => res.json({ status: "ok", version }));+app.get("/health", (_req, res) => res.json({ status: "ok", version, region: "ap-south-1" })); app.get("/total/:amount", (req, res) => res.json({ total: total(Number(req.params.amount)) })); app.use(express.static("dist"));[feature/health-region 6867a23] Report region in /health 1 file changed, 1 insertion(+), 1 deletion(-)An MR needs the branch to be on GitLab first. So if you try before pushing, it fails:
glab mr create --title "Report region in /health" --description "Closes #2" # fails: branch not pushedERROR: 422 Source branch "feature/health-region" is not on GitLab yet. Push it first: git push -u origin feature/health-regionPush the branch. GitLab answers with a link to open an MR:
git push -u origin feature/health-regionEnumerating objects: 3, done.Counting objects: 100% (3/3), done.Writing objects: 100% (3/3), 1.02 KiB | 1.02 MiB/s, done.Total 3 (delta 1), reused 0 (delta 0), pack-reused 0remote:remote: To create a merge request for feature/health-region, visit:remote: https://gitlab.com/acme/storefront/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fhealth-regionremote:To https://gitlab.com/acme/storefront.git * [new branch] feature/health-region -> feature/health-regionbranch 'feature/health-region' set up to track 'origin/feature/health-region'.Now open the MR from the terminal:
glab mr create --title "Report region in /health" --description "Closes #2" --label featureglab mr listCreating merge request for feature/health-region into main in acme/storefront!12 Report region in /health (feature/health-region) https://gitlab.com/acme/storefront/-/merge_requests/12Showing 1 merge request on acme/storefront. (Page 1)!12 acme/storefront!12 Report region in /health (main) ← (feature/health-region)Merge requests use ! in their number (!12). Issues use # (#2).
Closes #2 in the description is a special phrase. When the MR is merged, GitLab closes issue #2 for you. Fixes #2 and Resolves #2 do the same. Related to #2 only adds a link and does not close it.
Reviewing the merge request
glab mr view 12Report region in /health !12Open • opened by @lynuxpro • feature/health-region → mainCloses #2Labels: featureApprovals: 0/0Pipeline: noneView this merge request on GitLab: https://gitlab.com/acme/storefront/-/merge_requests/12On the website, an MR has four tabs:
- Overview: the description, pipeline result, approvals and the Merge button.
- Commits: the commits on the branch.
- Pipelines: every pipeline that ran for the branch.
- Changes: the code that changed. Reviewers click a line to leave a comment. With Suggest changes they can propose an exact fix that the author applies with one click.
Draft MRs. If your work isn't finished, start the title with Draft: (or use glab mr create --draft). A draft can't be merged, so reviewers know to wait. When it is ready, run glab mr update 12 --ready.
Pipeline: none means the project has no .gitlab-ci.yml yet. From lesson 6, every MR shows its pipeline here. In lesson 4 you will make the pipeline required before merging.
Merging
Merge it, and delete the branch because it is finished:
glab mr merge 12 --remove-source-branch✓ Merged !12 into main✓ Deleted source branch feature/health-region✓ Closed issue #2Update your clone: git switch main && git pullGitLab made the merge on the server and closed issue #2. Your laptop doesn't have that change yet. Update it:
git switch maingit pullgit log --oneline -4Switched to branch 'main'Your branch is up to date with 'origin/main'.From https://gitlab.com/acme/storefront.git 2eb037f..ba106fe main -> origin/mainUpdating 2eb037f..ba106feFast-forward src/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)ba106fe (HEAD -> main, origin/main) Merge branch 'feature/health-region' into 'main'6867a23 (feature/health-region, origin/feature/health-region) Report region in /health2eb037f Add Dockerfile, Kubernetes manifests and smoke test8b08b86 Add pricing with 18% GST and the API serverThen delete your local branch: git branch -d feature/health-region.
Merge methods
You can choose how merging works in Settings > Merge requests > Merge method:
| Method | What happens | Why pick it |
|---|---|---|
| Merge commit | Adds a merge commit every time (what you just saw) | You can see when each MR was added |
| Merge commit with semi-linear history | Adds a merge commit, but only if the branch has the latest main | Fewer surprises |
| Fast-forward | No merge commits. The branch must be updated (rebased) first | A clean, straight history |
Squash is a separate option. It joins all the commits on a branch into one commit when merging (glab mr merge 12 --squash). Many teams squash so main has one tidy commit per MR.
Common mistakes
- Working on
mainand pushing to it. On a protectedmainthe push is refused (lesson 4), and nobody reviews your code. Always make a branch first. - Forgetting
git pullafter a merge. Your next branch starts from an oldmain, and you get conflicts that aren't yours. - Very big MRs. Nobody reads 2,000 lines carefully. Keep an MR small enough to review in about 15 minutes. Split big work into several MRs.
- Pasting the private key into GitLab. GitLab only needs the
.pubfile. If a private key leaks, delete it from GitLab and make a new one.
Quick reference
| Command | What it does |
|---|---|
ssh-keygen -t ed25519 -C "you@example.com" | Make an SSH key |
ssh -T git@gitlab.com | Test SSH |
git switch -c feature/x | Make a branch and move to it |
git push -u origin feature/x | Push a new branch |
glab mr create --title "..." --description "Closes #N" | Open a merge request |
glab mr create --draft / glab mr update N --ready | Draft MRs |
glab mr list / glab mr view N | See merge requests |
glab mr merge N --squash --remove-source-branch | Merge |
git switch main && git pull | Update your laptop after a merge |
Next you will organise the work with issues, labels, milestones and boards.
CHECK YOUR UNDERSTANDING
3 quick questions
1. Which SSH key file do you paste into GitLab?
2. Why does glab mr create fail before you push the branch?
3. What does 'Closes #2' in a merge request description do?
You've finished the free preview of GitLab CI/CD
Upgrade to Pro to unlock the remaining 18 lessons, every practice lab and a certificate when you finish.
See plans