< GitLab

Introduction

This is an effort to document workflows for using our GitLab instance for Wikimedia development. Depending on the result of the consultation, it may evolve into a reference for production use.

See Gerrit to GitLab for pointers on how specific features and tasks translate across systems.

Registering an account on GitLab

Accounts on the gitlab instance are tied to Wikimedia developer accounts. For help creating a developer account, visit Help:Create a Wikimedia developer account.

Once you have a developer account, visit gitlab.wikimedia.org and click "Sign In" in the upper-righthand corner of the window. You'll be redirected to idp.wikimedia.org to enter your credentials, and then back to the GitLab instance once you've signed in.

Add an SSH key

Visit the SSH settings in your profile and add a public key. See the GitLab documentation for more details on this process.

GitLab also supports HTTPS remotes, so an SSH key is not strictly necessary. It is however assumed for purposes of this document.

Add two-factor authentication

You may be required to use 2-factor authentication to access most projects on GitLab.

Click on your user icon, then "Edit profile" or "Preferences", followed by "Account", and the "Manage two-factor authentication" button. From here you can set up an authenticator app, or use a hardware device (such as a YubiKey) as your second factor.

See the upstream GitLab docs on two-factor authentication for more detail.

Hosting a project on GitLab

See Hosting a project on GitLab for help getting a project onto the GitLab instance.

Making a GitLab Merge Request

UML diagram showing the trusted contributor GitLab workflow

Clone a repository from gitlab

Example:
user@machine:~/src/ $ git clone git@gitlab.wikimedia.org:repos/releng/blubber.git 
Cloning into 'blubber'...
remote: Enumerating objects: 823860, done.
remote: Counting objects: 100% (823860/823860), done.
remote: Compressing objects: 100% (121686/121686), done.
remote: Total 823860 (delta 699470), reused 823820 (delta 699435), pack-reused 0
Receiving objects: 100% (823860/823860), 250.40 MiB

Create a branch

Use the convention work/your_username/topic:

Example:
user@machine:~/src/blubber $ git checkout -b work/user/refactor-readme
Switched to a new branch 'work/user/T2000'

Make your changes

Write some code and save your changes.

Stage and commit your changes

Let's say you've edited the README.md in blubber. You should be able to see this using git status:

Example:
user@machine:~/src/blubber $ git status
On branch work/user/refactor-readme
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md

Stage the file and commit:

Example:
user@machine:~/src/blubber $ git add README.md

user@machine:~/src/blubber $ git commit -m ''
[work/user/refactor-readme 21c7972d3f8] Updated README.md
 1 file changed, 2 insertions(+)

You should now be able to see this commit in the log:

Example:
Flesh out 
commit 21c7972d3f819005dc00fa0f3f5cd196c3bb46b4 (HEAD -> work/user/refactor-readme)
Author: Some User <user@example.com>
Date:   Wed Sep 23 15:45:30 2020 -0600

    Updated README.md
    
    Change-Id: I67ce346df5781733081591c05cb980a753901a39

Fork the repository

UML diagram showing new volunteer GitLab workflow
If you have commit access, you can push your branches directly to origin without having to fork, and safely ignore this section.

If you don't have developer-level permissions on the project you'd like to contribute to, you'll first need to copy the repository to your own account. This is known as "forking". Visit the repository page, for example https://gitlab.wikimedia.org/repos/releng/blubber, and click "Fork" in the upper righthand corner.

Next, add a remote for your fork:

Example:
user@machine:~/src/blubber $ git remote add fork git@gitlab.wikimedia.org:user/blubber.git

If you've done this successfully, it should show up in your remotes:

Example:
# Check remotes:
user@machine:~/src/blubber $ git remote -v
fork    git@gitlab.wikimedia.org:user/blubber.git (fetch)
fork    git@gitlab.wikimedia.org:user/blubber.git (push)
origin  git@gitlab.wikimedia.org:repos/releng/blubber.git (fetch)
origin  git@gitlab.wikimedia.org:repos/releng/blubber.git (push)

Push your commit to GitLab

If you have commit access, you can push directly to a branch on the origin with git push origin work/user/refactor-readme.

If you're using a fork, push instead to the remote you added above with git push fork work/user/refactor-readme.

Example:
user@machine:~/src/blubber $ git push origin work/user/refactor-readme
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 20 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 352 bytes | 176.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: 
remote: ========================================================================
remote: 
remote:                       🚧 This instance is [under
remote:      construction](https://www.mediawiki.org/wiki/GitLab/Roadmap);
remote:   expect occasional downtime. Runners available in /repos. Questions?
remote:                       Ask in [#wikimedia-gitlab on
remote:    libera.chat](https://web.libera.chat/?channel=#wikimedia-gitlab),
remote:                                 or under
remote:    [GitLab](https://phabricator.wikimedia.org/project/board/5057/) on
remote:                               Phabricator.
remote: 
remote: ========================================================================
remote: 
remote: To create a merge request for work/user/refactor-readme, visit:
remote:   https://gitlab.wikimedia.org/repos/releng/blubber/-/merge_requests/new?merge_request%5Bsource_branch%5D=work%2Fuser%2Frefactor-readme
remote: 
To gitlab.mediawiki.org:repos/releng/blubber.git
 * [new branch]              work/user/refactor-readme -> work/user/refactor-readme

Create a Merge Request on GitLab

You may notice that GitLab responded to the push with a link to create a new merge request from your branch. You can follow that link directly, or visit the GitLab instance, navigate to the branch list for the repository, and click the "Merge request" button in the listing.

A screenshot of a GitLab Community Edition listing of branches on a repository.
If your branch is on a fork under your user account, remember to look there instead of on the origin repo.

Provide an informative title and description for the merge request, and @-mention the users you'd like to see review your code. You should be able to use the full range of syntax in GitLab Markdown.

Merge options

In the "Merge options" section:

  • Check "Delete source branch when merge request is accepted" to avoid cluttering the repository with already-merged feature branches. (This is probably the default.)
  • In the general case, you should use "Squash commits when merge request is accepted". (This is probably the default.)
    • This can be omitted if you plan to submit a merge request containing multiple commits.
    • If the logical structure of your change dictates multiple commits, you may use interactive rebase to achieve the desired history and force push to your work branch before merge. This is an advanced workflow, but supported.
  • Check "Allow commits from members who can merge to the target branch", unless you have a specific reason to prevent others from making changes.

Draft / WIP merge requests

If you want to let reviewers know that your change is a work in progress and prevent accidental merges, you can use a draft merge request. Prefix your merge request title with Draft: . This will display a notice and disable "merge" button for reviewers.

Adding commits to a merge request

Making new commits

Typically, you'll just make a new commit on your work branch, as usual, and push to the correct remote:

git push origin work/user/refactor-readme

Or:

git push fork work/user/refactor-readme

The new commit makes it very easy for reviewers to see what changed since they last saw the merge request, and will be squashed into the other commits when the pull request is merged, assuming that the pull request is set up to squash commits on merge (as recommended above).

Force pushing to a branch

If, instead, you wish to rewrite the history of the branch, for example by altering an existing commit or rebasing your work on top of upstream changes, GitLab allows force-pushing to a branch that's associated with a merge request. With this approach it is still very easy for reviewers to see what changed since the last push to the merge request.

Example:
user@machine:~/src/blubber $ git status        
On branch work/user/refactor-readme
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

# Add your changed file:
user@machine:~/src/blubber $ git add README.md

# Amend the previous commit:
user@machine:~/src/blubber $ git commit --amend
[work/user/refactor-readme b867605eb86] README.md: Add some whitespace and rework lists
 Date: Fri Sep 25 14:29:44 2020 -0600
 1 file changed, 8 insertions(+), 6 deletions(-)

# Push to your remote:
user@machine:~/src/blubber $ git push -f fork work/user/refactor-readme
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 20 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 403 bytes | 403.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: 
remote: ========================================================================
remote: 
remote:                       🚧 This instance is [under
remote:      construction](https://www.mediawiki.org/wiki/GitLab/Roadmap);
remote:   expect occasional downtime. Runners available in /repos. Questions?
remote:                       Ask in [#wikimedia-gitlab on
remote:    libera.chat](https://web.libera.chat/?channel=#wikimedia-gitlab),
remote:                                 or under
remote:    [GitLab](https://phabricator.wikimedia.org/project/board/5057/) on
remote:                               Phabricator.
remote: 
remote: ========================================================================
remote: 
remote: To create a merge request for work/user/refactor-readme, visit:
remote:   https://gitlab.wikimedia.org/user/blubber/-/merge_requests/new?merge_request%5Bsource_branch%5D=work%2Fuser%2Frefactor-readme
remote: 
To https://gitlab.wikimedia.org:brennen/blubber.git
 + 4b860bb6680...b867605eb86 work/user/refactor-readme -> work/user/refactor-readme (forced update)

This is mainly suitable for merge requests which are not meant to be squashed on merge, typically because they contain two or more commits that should remain separate. In that case, you clean up the history locally and then force-push it to update the merge request.

Getting code review

Once you've submitted a merge request, someone will need to review it. In addition to @-mentioning appropriate users in your merge request description or comments, you can assign a single reviewer. Do this by clicking "Edit" on "Assignees" in the sidebar at the right side of the page when viewing a merge request.

Please see Code review for documentation on reviewing code.

Local repository migration

Update your remote

When we migrate from Gerrit to GitLab you will need to update your remote repository url locally:

Remote url update:
$ git remote set-url origin git@gitlab.wikimedia.org:[repo-name].git
# or 
$ git remote set-url origin https://gitlab.wikimedia.org/[repo-name].git

Rename "master" to "main"

As part of the migration to GitLab, we are changing the mainline branch from master → main.

To update your local checkout (after changing your remote):

Branch renaming:
# Rename the branch locally
$ git branch -m master main

# Reset your upstream branch
$ git fetch
$ git branch --unset-upstream
$ git branch --set-upstream-to=origin/main

# Change the remote head
$ git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

This transition is documented on Phabricator on phab:T281593 (and phab:T254646).

Sources and further reading

There's a lot of quality documentation available for GitLab, although navigating it can be a bit tricky.

This article is issued from Mediawiki. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.