January 13, 2021

Getting Started with GitLab

What is the difference between Git and GitLab?

Git is a version control system used to locally check changes in a project or folder. Once we add Git to our project it will start tracking for changes and also push and pull changes from remote repositories like GitHub, BitBucket and GitLab.

GitLab provides a service that allows you to host a project or folder on a remote repository and has some additional features to help in software delivery life cycle and CI & CD. GitHub and BitBucket provide similar kinds of service as GitLab.

Some features include:

  • Managing code

  • Code sharing

  • Wiki or code documentation

  • Bug tracking

  • CI & CD

Creating an account on GitLab:
Link: https://gitlab.com/users/sign_up or alternatively you can login with your existing GitHub, Google account or other accounts available.

image

Create a new Project:

  1. Fill in project name.

  2. Change the visibility - private/public.

  3. Give description (optional).

  4. Click on create project.

image

After creating the project copy your project link or get it from a clone url.
Project link: https://gitlab.com/shubham-dota/my-first-project.git

Some Basic Git Terminologies:

git init – make an existing directory as a Git repository.
git clone [url] - retrieve an entire repository from a hosted location via URL.
git status - show changes in files in working directory, staged for your next commit.
git add [file] - add a file in the working directory to the staging area.
git commit -m “[commit message]” - commits staged content.
git pull - fetch and merge any commits from the tracking remote branch.
git push [alias] [branch] – pushes the code from local to remote repository.

There are many more commands in Git.
Refer : https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet

Getting Started with Git Commands

  1. Download Git for your OS.

  2. Install Git as per your OS. Click for Step by step process.

  3. After installing type < git --version > in CMD/terminal check if it’s installed.

  4. Config your Git

1
2
3
4
5
git--global user.name“ < GitLab UserName > ”(To add Git user)
git--global user.name(To check added user name)
git config--global user.email "<GitLab Email>"(To add GitLab mail)
git config--global user.email(To check added email)
git config--global– list(To check the values set)
  1. Create a new folder or project and add to Git.

1
2
3
4
5
6
7
8
9
10
Open terminal / CMD.
Change directory to the project location and follow the commands.
git init(To initialize Git)
git status(To display state of current project)
Now, create a file in the project directory and check
for git status.
git add < file name > or git add.(To add particular or all files)
git status
git commit– m“ commit message”
git push - u“ < url of repo > ”<branch name>	(To push files to GitLab)

image

Issues

Issues can be created for bugs, new features or anything related to the code in the repository. Issues are a kind of bug tracker in GitLab and are great to keep track of tasks, enhancements, and bugs for projects.

Labels

Labels are a way to categorize issues or tag issues for managing work on GitLab. Labels help to organize and tag work so that it can be tracked and we can work on the item we are interested in.

Steps to create issues:
  • Login to GitLab account.

  • Go to the repository/project you want to create an issue for.

  • Go to the issues tab then click on the new issue button to create a new issue.

  • Fill in the information such as title and description.

  • You can use markdown to fill description.

  • You can assign the label.

  • Click on the submit issue button and you will get an overview of an issue along with title and description.

image

  1. The title and description describes the issue.

  2. Labels help to categorize and filter issues.

  3. Milestones are a way to track issues by keeping time periods or by using phases like Beta launch or Release Candidate.

  4. Labels are the best way to organize different types of issues. Issues can have many labels and issues can be filtered by labels.

  5. Assignees - An issue can have an assignee that’s responsible for moving the issue forward.

Groups

In GitLab groups are the way to categorize and restrict the permission of multiple repositories.

Steps to create a group:
  • Login to GitLab account and click on the groups menu.

  • Click on the new group button to create a group.

  • Enter the group name, visibility level (private/public). Now click on the create group button

  • Group has been created successfully.

image

How to Fork a Project

A fork is a duplicate copy of a project. Forking a project or repository allows you to make changes without affecting the original project or repo. So whenever you want to make some changes without changing the original copy, we can create a fork and make changes. Later on it can be merged with the original project.

Steps:
  • Login to GitLab account.

  • Go to your project.

  • Click on the fork button and select the namespace.

  • If you are getting a namespace error you’ll need to create a group.

image

Merging the fork with the original copy
  • Once you are done with the changes on the forked copy, you can merge it with the original branch.

  • Click on merge request.

  • Select source branch and branch name and then the target branch.

  • Click on compare branch and continue.

  • Submit merge.

image

Setting up SSH key in GitLab :

This helps to connect to the GitLab server without using your username and password every time.

On Mac:
  • Open terminal and give command ssh-keygen.

  • Enter the location to save the key.

  • Press enter and you’ll get your key.

On Windows :

Use any ssh-client like putty or git bash
Give command ssh-keygen.
Enter the location to save the key.
Press enter and you’ll get your key.
Login to GitLab Account -> Settings -> SSH keys.
Add the key generated in id_rsa.pub file.
Verify SSH key.

image

GitLab Runner

It is an open source continuous integration service which is included in GitLab, used to run jobs and send back results to GitLab.

Installing GitLab Runner

For Mac

  • Go to terminal.

  • Give command brew install gitlab-runner to install.

  • To start runner run brew services start gitlab-runner.

Register GitLab Runner
  • Open terminal

  • Give gitlab-runner register

  • Enter GitLab instance URL.

  • Enter the token obtained to register the runner.

  • Enter description for the runner.

  • Input the tags associated with the runner, separated by commas.

  • Provide the runner executor.

image

Check if the runner is activated in the project

Go to project settings and the CI/CD, then runner and check for activated runner.

image

Note

  • To get gitlab-ci token, go to project settings and then under settings go to CI/CD. In the runner section you’ll find the token.

  • For installing runner on Windows follow : https://docs.gitlab.com/runner/register/

GitLab CI/CD

In earlier days we used to build a project and then deploy the entire project on the server. But now most developers follow the agile concept. With agile we use CI - continuous integration. Consider there are four members in a team, one developer is working on a module and the same module is used by another developer. As a team we develop the project and after completion we combine the entire project and deploy the project. If anything goes wrong it will be very difficult to find what it is, where the bug is and who made the mistake. With CI, you can build the project and push it to the server, therefore it will be continuous integration. If you are using an IDE you can just right click and push the data to the server and the server will be responsible for continuous deployment. So we have:

CI (Continuous Integration): pushing the code to the server.
CD (Continuous Deployment): delivery/deployment of code.

Steps:

  • Add .gitlab-ci.yml in the root folder on project/repository.

  • This file is used to configure pipeline and defines structure and order of the pipeline.

  • File gist : https://gist.github.com/shubham-dota/64760c6be1f1409bd9fafcece13bde30

  • Commit and push to repository.

  • Create GitLab runner for the project making sure the tags are the same as that provided in the yaml file.

  • Start the runner.

  • Now make any change to the project and then commit and push.

image

The gitlab-ci.yml will check for any commits and push in the project. You can check CI/CD jobs under your project getting triggered.

image

Group 9
Group 9

Recommended for you

Project Management on GitHub

Introduction Both git and GitHub have become indispensable tools in the lives of developers. Open-source proje...
Read More E4627031-A283-4694-8843-C0F351FBA3F8

GitHub Repositories With Competitive Programming Libraries

Everyone who has participated in algorithm competitions for at least a year knows that some algorithms and dat...
Read More E4627031-A283-4694-8843-C0F351FBA3F8