Skip to content
$ lynuxpro
← GitLab CI/CD

LESSON 2 OF 20 · BEGINNER · 24 MIN

Projects, SSH keys and merge requests

Free previewLesson 2 of the 2 free lessons in this Pro course.Unlock all 20

HOW IT WORKSBranch, push, merge request, merge

ssh -TSSH key~/.ssh/id_ed25519.pubYour laptopfeature/health-regionGitLabacme/storefrontMerge request !12Closes #2mainmerge commitIssue #2closed
1/6

Add your public key to GitLab once. Then Git can talk to GitLab with no password.

$ ssh -T git@gitlab.com
Welcome to GitLab, @lynuxpro!

In a GitLab team, nobody changes main directly. Instead you:

  1. make a branch,
  2. push it to GitLab,
  3. 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:

SSHHTTPS
Linkgit@gitlab.com:acme/storefront.githttps://gitlab.com/acme/storefront.git
How you log inA key file on your computerYour username and a token as the password
Best forYour laptop: set it up once, no passwordsCI 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 yet
SHELLbashutf-8 · 1L
git@gitlab.com: Permission denied (publickey).
OUTPUTterminalutf-8 · 1L

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.pub
SHELLbashutf-8 · 2L

On 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 .pub is 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 .pub is the private key. Never share it or paste it anywhere.

Now the test works:

ssh -T git@gitlab.com
SHELLbashutf-8 · 1L
Welcome to GitLab, @lynuxpro!
OUTPUTterminalutf-8 · 1L

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"
OUTPUTterminalutf-8 · 1L

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
OUTPUTterminalutf-8 · 2L

-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.js
SHELLbashutf-8 · 3L
Switched to a new branch 'feature/health-region'7:app.get("/health", (_req, res) => res.json({ status: "ok", version }));
OUTPUTterminalutf-8 · 2L

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"
SHELLbashutf-8 · 3L
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(-)
OUTPUTterminalutf-8 · 15L

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 pushed
SHELLbashutf-8 · 1L
ERROR: 422 Source branch "feature/health-region" is not on GitLab yet. Push it first: git push -u origin feature/health-region
OUTPUTterminalutf-8 · 1L

Push the branch. GitLab answers with a link to open an MR:

git push -u origin feature/health-region
SHELLbashutf-8 · 1L
Enumerating 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'.
OUTPUTterminalutf-8 · 11L

Now open the MR from the terminal:

glab mr create --title "Report region in /health" --description "Closes #2" --label featureglab mr list
SHELLbashutf-8 · 2L
Creating 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)
OUTPUTterminalutf-8 · 7L

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 12
SHELLbashutf-8 · 1L
Report 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/12
OUTPUTterminalutf-8 · 10L

On 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
SHELLbashutf-8 · 1L
✓ Merged !12 into main✓ Deleted source branch feature/health-region✓ Closed issue #2Update your clone: git switch main && git pull
OUTPUTterminalutf-8 · 4L

GitLab 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 -4
SHELLbashutf-8 · 3L
Switched 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 server
OUTPUTterminalutf-8 · 12L

Then delete your local branch: git branch -d feature/health-region.

Merge methods

You can choose how merging works in Settings > Merge requests > Merge method:

MethodWhat happensWhy pick it
Merge commitAdds a merge commit every time (what you just saw)You can see when each MR was added
Merge commit with semi-linear historyAdds a merge commit, but only if the branch has the latest mainFewer surprises
Fast-forwardNo merge commits. The branch must be updated (rebased) firstA 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 main and pushing to it. On a protected main the push is refused (lesson 4), and nobody reviews your code. Always make a branch first.
  • Forgetting git pull after a merge. Your next branch starts from an old main, 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 .pub file. If a private key leaks, delete it from GitLab and make a new one.

Quick reference

CommandWhat it does
ssh-keygen -t ed25519 -C "you@example.com"Make an SSH key
ssh -T git@gitlab.comTest SSH
git switch -c feature/xMake a branch and move to it
git push -u origin feature/xPush a new branch
glab mr create --title "..." --description "Closes #N"Open a merge request
glab mr create --draft / glab mr update N --readyDraft MRs
glab mr list / glab mr view NSee merge requests
glab mr merge N --squash --remove-source-branchMerge
git switch main && git pullUpdate your laptop after a merge

Next you will organise the work with issues, labels, milestones and boards.

CHECK YOUR UNDERSTANDING

3 quick questions

  1. 1. Which SSH key file do you paste into GitLab?

  2. 2. Why does glab mr create fail before you push the branch?

  3. 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