GitLab Developer FAQ

From pCT
Revision as of 10:48, 10 February 2021 by Mri083 (talk | contribs) (typo fixed)

Main Page -> Documentation -> GitLab Developer FAQ

This is a collection of frequently asked Gitlab questions for developers

Please edit this page and add your question, or send email to pCT@uib.no

Authentication

In order to clone non-public repositories and do synchronization, an authentication method is required. It is recommended to use SSH keys

Register SSH key

  1. Login to https://git.app.uib.no
  2. go to User settings -> SSH Keys (https://git.app.uib.no/-/profile/keys)
  3. paste public SSH key from your .ssh folder, something like id_*.pub
  4. select optionally a title and expiration date
  5. click Add

Create access token

  1. Login to https://git.app.uib.no
  2. go to User settings -> Access Tokens (https://git.app.uib.no/-/profile/personal_access_tokens)
  3. Choose name and expiration date and scopes
  4. click Create personal access token
  5. Store the token in a safe place or configure the relevant application for accessing the repository with this token immediately

Hints:

  • Create access tokens only for the scope with the minimal access permissions required for your purpose
  • Keep in mind: the token is only visible in the web interface after creation, you can not get it later
  • Unused tokens should be revoked as soon as possible

Create repository clone

A repository clone is your work copy, it is created using the git clone-command.

Find the links for the repository to be cloned

Pct-online-clone-menu.png

Clone using SSH

An SSH key needs to be configured -> Gitlab Developer FAQ#Register SSH key

  git clone git@git.app.uib.no:pct/pct-online.git

Note: this is an example for the pct-online repository

Clone using Access Token

An access token needs to be configured -> Gitlab Developer FAQ#Create access token

   git clone https://gitlab-ci-token:your-token-number@git.app.uib.no/pct/pct-online

Note: insert your token number

Working with forks

What is a fork?

Every logged-in user can access the main repository, however only a small group of administrators has write access. To contribute, a user creates a fork (see here) from the repository. This is a repository copy in the GitLab system where a single developer or a group of developers have full access. The fork is created in a user space.

It's important not to be confused by the terminology. In contrast to the fork which is still a remote repository on the GitLab web service, a local copy of the repository is required on the working machine in order to work on the project. This copy is referred to be a clone (see here).

How do I create a project fork?

A project fork or repository fork is a copy of the original repository where a user or a group of users has/have full control. All development in our project is carried out in the individual forks. Branches of project forks are merged back into the main repository by merge requests, preferably via fast forward merges. That requires developers to rebase project forks to the main repository and resolve all conflicts before requesting a merge.

See How to fork a project for some general introduction.

In the pCT project you can create a fork of all the main repositories in the GitLab pct group by clicking the fork button.

Pct-online-fork-button.png

Note: pct-online is an example, replace by repository you are working with.

Which rules should I obey?

Your repository fork is your sandbox, you can do whatever you want. Still its good to follow some rules, unless you have your own rules at hand right now, you can apply the following:

  • leave the master branch in sync with the main repository, do not make commits to it
  • commit your changes to the dev branch
  • if you start a new feature, and it's expected to take a while, make a feature branch, e.g. dev-feature and give the feature a name
  • synchronize regularly to the main repository by rebasing (tutorial coming soon)
  • add your colleagues as developers to share the code with them, or to simply give read access for the project members add group pct with Reporter-role.

GitLab-fork-members-illustration.png

How can I make a clone of a project in my fork?

Note


Procedure

git clone git@git.app.uib.no:User.Space/pct-online.git

I have a clone of the main repository and want to connect it now to my fork?

Note

  • All the examples are using access via SSH, refer to GitLab Developer FAQ#Clone using Access Token and adjust accordingly.
  • User.Space needs to be replaced by your user space, see the link in the web interface.
  • Replace pct-online by repository you are working with.


Procedure

Check the remotes of your repository

git remote -v

Should show you something like

   origin	git@git.app.uib.no:pct/pct-online.git (fetch)
   origin	git@git.app.uib.no:pct/pct-online.git (push)

Since origin should refer to your development proxy, we first rename the current origin to upstream:

git remote rename origin upstream

Add the fork as the new origin:

git remote add origin git@git.app.uib.no:User.Space/pct-online.git

Synchronize with the fork

git remote update

Make your branches to track the branches of your fork, e.g. for the dev branch:

git checkout dev
git branch --set-upstream-to origin/dev

Check the remotes:

git remote -v

With new result:

   origin	git@git.app.uib.no:User.Space/pct-online.git (fetch)
   origin	git@git.app.uib.no:User.Space/pct-online.git (push)
   upstream	git@git.app.uib.no:pct/pct-online.git (fetch)
   upstream	git@git.app.uib.no:pct/pct-online.git (push)

Working with clones

How many clones can I have?

Since a clone is just a local copy, you can have as many as you want. This is useful if you need to do some testing in a fresh repository or on a different machine.

How can I change the upstream link?

A remote has a name in the git clone, the default name is often origin

Note

  • The example is using access via SSH
  • User.Space needs to be replaced by your user space, see the link in the web interface.
  • Replace pct-online by repository you are working with.
  • If you are using a different name, replace origin accordingly


Procedure

git remote set-url origin git@git.app.uib.no:User.Space/pct-online.git

Can I have more then one upstream repository

Yes, as a more advanced option, multiple remote repositories can be added to the clone. All remote repositories are referenced through their names, e.g. origin pointing to the fork and upstream pointing to main repository. this is the recommended setup to work with both the developer fork and synchronize it to the main repository.

Note

  • The example is using access via SSH
  • To add the fork of a colleague, replace pct by appropriate user space
  • Replace pct-online by repository you are working with.
  • If you want a different name, replace upstream accordingly
  • After adding the new repository you need to update the remote.


Procedure

git remote add upstream git@git.app.uib.no:pct/pct-online.git
git remote update upstream

Merge request

All updates to the main repository are made via merge requests (github refers to them as pull requests). A merge request requires the code update to be in a mergable branch in a development fork.

Merge request are also used widely to share work-in-progress with your colleagues and for code reviews. Mark such Merge request with "WIP:".

Preparation

In order to avoid conflicts, first the fork has to be updated to the main repository. The idea behind this is that all potential conflicts can be resolved by the developer with the best knowledge about the matter, while the maintainer can simple merge fast-forward.

This description expects origin pointing to the developers fork. All contributions are done to branch dev, thus it must be updated with the latest version of branch dev of main repository.

  1. Fetch update from main repository
     git fetch upstream dev
    • the update is now stored in the local index, we have fetched branch dev of the main repository
    • check the log of this latest fetch
     git log FETCH_HEAD
    • FETCH_HEAD is a shorthand for the head of the last branch fetched and is valid only immediately after a fetch operation
  2. Rebase your development to the main repository, rebase means that all the new commits are done with respect to the tip of the specified branch. There can be merge conflicts which need to be resolved with appropriate changes and commits.
     git rebase FETCH_HEAD dev
    • keep in mind that FETCH_HEAD is only temporarily valid, so you should do this immediately after fetch (log does not change anything)
    • replace dev with appropriate branch name if other then dev is used; it is also possible to rebase a branch with other name to the remote branch, e.g.
     git rebase FETCH_HEAD feature-name
  3. Check your commits and commit messages, eventually squash (combine) or reword. This advanced option is described later.
  4. Push to fork. As the commit history has most likely been changed, the option -f (force) has to be used
     git push -f origin dev 

The branch dev in the fork is now ready for a merge request. The branch name can be adjusted, there is nothing preventing other branch names.

Example workflow for a Merge request

  1. Go to your gitlab user space at [1] (replace User.Space appropriately).
  2. Find the project fork, e.g. in the list of projects associated with you from the upper main menu.
  3. In the line with the many columns regarding the repository, click on the "+"-symbol on the right hand side and choose "New merge request"
  4. Select project and branch for both source and target, and click "Compare branches and continue". Remember: in almost all cases you have to merge to branch dev or other feature branch, only in very rare cases to branch master
  5. Review the list of commits in this merge request, give it a descriptive title and description, pick an assignee
  6. Submit the merge request