GITLAB for Beginners
Why to use GitLab?
GitLab is great way to manage git repositories on centralized server. GitLab gives you complete control over your repositories or projects and allows you to decide whether they are public or private for free.
Features
GitLab hosts your (private) software projects for free.
GitLab is a platform for managing Git repositories.
GitLab offers free public and private repositories, issue-tracking and wikis.
GitLab is a user-friendly web interface layer on top of Git, which increases the speed of working with Git.
GitLab provides its own Continuous Integration (CI) system for managing the projects and provides user interface along with other features of GitLab.
Installation of GitLab on Windows:
Step 1 − First create a folder called 'GitLab-Runner' in your system. For instance, you can create in C drive as C:\GitLab-Runner.
Step 2 − Now download the binary for x86 or amd64 and copy it in the folder created by you. Rename the downloaded binary to gitlab-runner.exe.
Step 3 − Open the command prompt and navigate to your created folder. Now type the below command and press enter.
C:\GitLab-Runner>gitlab-runner.exe register
Step 4 − After running the above command, it will ask to enter the gitlab-ci coordinator URL.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com
Step 5 − Enter the gitlab-ci token for the runner.
Please enter the gitlab-ci token for this runner:
xxxxx
- To get the token, log in to your GitLab account −
- Now go to your project −
- Click on the CI/CD option under Settings tab and expand the Runners Settings option.
- Under Runners Settings section, you will get the token as shown in the image below −
Step 6 − Enter the gitlab-ci description for the runner.
Please enter the gitlab-ci description for this runner:
[Admin-PC]: Hello GitLab Runner
Step 7 − It will ask to enter the gitlab-ci tags for the runner.
Please enter the gitlab-ci tags for this runner (comma separated):
tag1, tag2
You can change these tags in the GitLab's user interface later.
Step 8 − You can lock the Runner to current project by setting it to true value.
Whether to lock the Runner to current project [true/false]:
[true]: true
After completing above steps, you will get the successful message as 'Registering runner... succeeded'.
Step 9 − Now enter the Runner executor for building the project.
Please enter the executor: parallels, shell, docker+machine, kubernetes, docker-
ssh+machine, docker, docker-ssh, ssh, virtualbox:
docker
We have used the selector as 'docker' which creates build environment and manages the dependencies easily for developing the project.
Step 10 − Next it will ask for default image to be set for docker selector.
Please enter the default Docker image (e.g. ruby:2.1):
alpine:latest
Step 11 − After completing the above steps, it will display the message as 'Runner registered successfully'. The below image will describe the working flow of above commands −
Step 12 − Now go to your project, click on the CI/CD option under Settings section and you will see the activated Runners for the project.
You can see the GitLab Runner configuration in the config.toml file under the GitLab-Runner folder as shown below −
concurrent = 1
check_interval = 0
[[runners]]
name = "Hello GitLab Runner"
url = "https://gitlab.com"
token = "40ceed29eec231fa9e306629cae4d7"
executor = "docker"
[runners.docker]
tls_verify = false
image = "alpine:latest"
privileged = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
[runners.cache]
Installation of GitLab on Ubuntu
The GitLab can be installed on Ubuntu system by using Omnibus package which provides different services to run GitLab. The Omnibus package provides necessary components of GitLab, establishes the configurations and project metadata which can be used in user's system.
The following steps describe installation of GitLab on Ubuntu −
Step 1 − First, login to your GitLab server using SSH (Secure Shell).
Step 2 − Next, download the Omnibus package −
wget https://downloads-packages.s3.amazonaws.com/ubuntu-14.04/gitlab-ce_7.10.4~omnibus-1_amd64.deb
Step 3 − Install the postfix −
sudo apt-get install postfix
Postfix is a open source mail transfer agent used to deliver the email notifications.
Step 4 − While installing Postfix, it will ask type of installation; then select the Internet Site option. Next, it will show Postfix configuration along with the system mail name as shown in the image −
Step 5 − Install the dpkg (package manager for debian system) for managing the installed packages −
sudo dpkg -i gitlab-ce_7.10.4~omnibus-1_amd64.deb
Step 6 − To have the changes take effect, you need to reconfigure the GitLab by using the below command −
sudo gitlab-ctl reconfigure
Step 7 − Check the status of the GitLab services by using below command −
sudo gitlab-ctl status
If you want to install GitLab from the source, then install some dependencies on the server and need to setup the database by using the PostgreSQL. It is described in the Environment setup chapter. You can install the coordinator to build a web interface and control build instances. For more information, you can check the Installation of Coordinator chapter.
Description
Git commands are used for sharing and combining the code easily with other developers.
Git Commands
Following are the some basic Git commands can be used to work with Git −
The version of the Git can be checked by using the below command −
$ git --version
Add Git username and email address to identify the author while committing the information. Set the username by using the command as −
$ git config --global user.name "USERNAME"
After entering user name, verify the entered user name with the below command −
$ git config --global user.name
Next, set the email address with the below command −
$ git config --global user.email "email_address@example.com"
You can verify the entered email address as −
$ git config --global user.email
Use the below command to check the entered information −
$ git config --global --list
You can pull the latest changes made to the master branch by using the below command −
$ git checkout master
You can fetch the latest changes to the working directory with the below command −
$ git pull origin NAME-OF-BRANCH -u
Here, NAME-OF-BRANCH could be 'master' or any other existing branch.
Create a new branch with the below command −
$ git checkout -b branch-name
You can switch from one branch to other branch by using the command as −
$ git checkout branch-name
Check the changes made to your files with the below command −
$ git status
You will see the changes in red color and add the files to staging as −
$ git add file-name
Or you can add all the files to staging as −
$ git add *
Now send your changes to master branch with the below command −
$ git push origin branch-name
Delete the all changes, except unstaged things by using the below command −
$ git checkout .
You can delete the all changes along with untracked files by using the command as −
$ git clean -f
To merge the different branch with the master branch, use the below command −
$git checkout branch-name
$ git merge master
You can also merge the master branch with the created branch, by using the below command −
$git checkout master
$ git merge branch-name
Description
The SSH stands for Secure Shell or Secure Socket Shell used for managing the networks, operating systems and configurations and also authenticates to the GitLab server without using username and password each time. You can set the SSH keys to provide a reliable connection between the computer and GitLab. Before generating ssh keygen, you need to have Git installed in your system.
Creating SSH Key
Step 1 − To create SSH key, open the command prompt and enter the command as shown below −
C:\−ssh-keygen
It will prompt for 'Enter file in which to save the key (//.ssh/id_rsa):', just type file name and press enter. Next a prompt to enter password shows 'Enter passphrase (empty for no passphrase):'. Enter some password and press enter. You will see the generated SSH key as shown in the below image −
Step 2 − Now login to your GitLab account and click on the Settings option.
Step 3 − To create SSH key, click on the SSH keys tab at left side of the menu.
Step 4 − Now go to C drive, you will see the file with .pub extension which was generated in the first step.
Step 5 − Next open the key.pub file, copy the SSH key and paste it in the highlighted Key box as shown in the below image −
Step 6 − Click on the Add Key button, to add SSH key to your GitLab. You will see the fingerprint (it is a short version of SSH key), title and created date as shown in the image below −
In this chapter, we will discuss about how to create a new project in the GitLab.
Step 1 − To create new project, login to your GitLab account and click on the New project button in the dashboard −
Step 2 − It will open the New project screen as shown below in the image −
Enter the project name, description for the project, visibility level (accessing the project's visibility in publicly or internally) and click on the Create project button.
Step 3 − Next it will create a new project (here given the project name as first-gitlab-prjt) with successful message as shown below −
Push the Repository to Project
Step 4 − You can clone the repository to your local system by using the git-clone command −
The clone command makes a copy of repository into a new directory called first-gitlab-prjt.
Step 5 − Now go to your newly created directory and type the below command −
C:\>cd first-gitlab-prjt
C:\first-gitlab-prjt>touch README.md
The above command creates a README.md file in which you can put the information about your folder.
Step 6 − Add the README.md file to your created directory by using the below command −
C:\first-gitlab-prjt>git add README.md
Step 7 − Now store the changes to the repository along with the log message as shown below −
C:\first-gitlab-prjt>git commit -m "add README"
The flag -m is used for adding a message on the commit.
Step 8 − Push the commits to remote repository which are made on the local branch −
C:\first-gitlab-prjt>git push -u origin master
The below image depicts the usage of above commands in pushing the commits to remote repository −
Description
Fork is a duplicate of your original repository in which you can make the changes without affecting the original project.
Forking a Project
Step 1 − To fork a project, click on the Fork button as shown below −
Step 2 − After forking the project, you need to add the forked project to a fork group by clicking on it −
Step 3 − Next it will start processing of forking a project for sometime as shown below −
Step 4 − It will display the success message after completion of forking the project process −
Description
Branch is independent line and part of the development process. The creation of branch involves following steps.
Creating a Branch
Step 1 − Login to your GitLab account and go to your project under Projects section.
Step 2 − To create a branch, click on the Branches option under the Repository section and click on the New branch button.
Step 3 − In the New branch screen, enter the name for branch and click on the Create branch button.
Step 4 − After creating branch, you will get a below screen along with the created branch.
In this chapter, we will discuss about how to add a file to a project in the GitLab. We can add file in two ways −
Using Command Line Interface
Using Web Interface
Creating a file using Command Line Interface
Step 1 − To create a file by using command line interface, type the below command in your project directory −
Step 2 − Now go to your project directory and you will see the created file −
Creating a file using Web Interface
Step 1 − You can create a new file, by clicking on the '+' button which is at the right side of the branch selector in the dashboard −
Step 2 − Enter the file name, add some content in the editor section and click on the Commit changes button to create the file.
Step 3 − Now you will get a successful message after creating the file as shown below −
Description
Rebase is a way of merging master to your branch when you are working with long running branch.
Steps for Rebase Operation
Step 1 − Go to your project directory and create a new branch with the name rebase-example by using the git checkout command −
The flag -b indicates new branch name.
Step 2 − Now, create a new file and add some content to that file as shown below −
The content 'Welcome to Tutorialspoint' will be added to the rebase_file.md file.
Step 3 − Add the new file to working directory and store the changes to the repository along with the message (by using the git commit command) as shown below −
The flag -m is used for adding a message on the commit.
Step 4 − Now, switch to the 'master' branch. You can fetch the remote branch(master is a branch name) by using the git checkout command −
Step 5 − Next, create an another new file, add some content to that file and commit it in the master branch.
Step 6 − Switch to the rebase-branch to have the commit of master branch.
Step 7 − Now, you can combine the commit of master branch to rebase-branch by using the git rebase command −
Description
Squashing is a way of combining all commits into one when you are obtaining a merge request.
Steps for Squashing Commits
Step 1 − Go to your project directory and check out a new branch with the name squash-chapter by using the git checkout command −
The flag -b indicates new branch name.
Step 2 − Now, create a new file with two commits, add that file to working directory and store the changes to the repository along with the commit messages as shown below −
Step 3 − Now, squash the above two commits into one commit by using the below command −
$ git rebase -i HEAD~2
Here, git rebase command is used to integrate changes from one branch to another and HEAD~2 specifies last two squashed commits and if you want to squash four commits, then you need to write as HEAD~4. One more important point is, you need atleast two commits to complete the squash operation.
Step 4 − After entering the above command, it will open the below editor in which you have to change the pick word to squash word in the second line (you need to squash this commit).
Now press the Esc key, then colon(:) and type wq to save and exit from the screen.
Step 5 − Now push the branch to remote repository as shown below −
In this chapter, we will discuss about how to add users to your project in the GitLab.
Steps for Adding User
Step 1 − Login to your GitLab account and go to your project under Projects section.
Step 2 − Next, click on the Members option under Settings tab −
Step 3 − It will open the below screen to add the member to your project −
Step 4 − Now enter the user name, role permission, expiration date(optional) and click on Add to project button to add the user to project −
Step 5 − Next, you will get a successful message after adding user to the project.
The highlighted box in the above image indicates, a new user has been added to the project −
Step 6 − You can also add user to the project by clicking on the Import button −
Step 7 − Now select the project from which you want to add the user to your project and click on the Import project members button −
Step 8 − You will get a success message after importing user to the project −
Description
Creating group helps to connect multiple repositories and allows members to access the project by giving permissions on the group level.
Steps for Creating Group
Step 1 − Login to your GitLab account and click on the Groups menu −
Step 2 − Next, you will get the below screen and click on the New group button to create a group −
Step 3 − Enter the Group name, Description, visibility level(Private/Public/Internal) and also you can set the image for the group of your choice which should be within 200kb in size. Now click on the Create group button.
Step 4 − Next, it will display the success message after creating the group as shown below −
Step 5 − Now, go back to your Groups section and you will see the created group in the list −
In this chapter, we will discuss about how to remove users from a project in the GitLab.
Steps for Removing User
Step 1 − Login to your GitLab account and go to your project under Projects section −
Step 2 − Now, click on the Members option under Settings tab −
Step 3 − You will see the list of users under Existing members and groups section and click on the delete option at right side to remove the user from project −
Step 4 − After clicking remove button, it will display a pop-up window saying whether to remove the selected user from the project or not. Click on Ok button to remove the user.
Step 5 − Now, it will display the success message after removing the user from the project as shown in the image below −
In this chapter, we will discuss about user permissions in the project.
Steps for creating User Permissions
Step 1 − Login to your GitLab account and click on the Members option under Settings tab −
Step 2 − It will open the below screen to add the member to your project −
Step 3 − You will see the different types of permissions when you click on a dropdown under Choose a role permission section −
You can see the Adding users chapter for setting user permission and adding user to project. Here, we will briefly discuss about different user permissions which can be applied to projects.
The following table shows available permission levels for different types of users −
S.N. | Guest | Reporter | Developer | Master |
1 | Creates a new issue | Creates a new issue | Creates a new issue | Creates a new issue |
2 | Can leave comments | Can leave comments | Can leave comments | Can leave comments |
3 | Able to write on project wall | Able to write on project wall | Able to write on project wall | Able to write on project wall |
4 | - | Able to pull project code | Able to pull project code | Able to pull project code |
5 | - | Can download project | Can download project | Can download project |
6 | - | Able to write code snippets | Able to write code snippets | Able to write code snippets |
7 | - | - | Create new merge request | Create new merge request |
8 | - | - | Create new branch | Create new branch |
9 | - | - | Push and remove non protected branches | Push and remove non protected branches |
10 | - | - | Includes tags | Includes tags |
11 | - | - | Can create, edit, delete project milestones | Can create, edit, delete project milestones |
12 | - | - | Can create or update commit status | Can create or update commit status |
13 | - | - | Write a wiki | Write a wiki |
14 | - | - | Create new environments | Create new environments |
15 | - | - | Cancel and retry the jobs | Cancel and retry the jobs |
16 | - | - | Updates and removes the registry image | Updates and removes the registry image |
17 | - | - | - | Can add new team members |
18 | - | - | - | Push and remove protected branches |
19 | - | - | - | Can edit the project |
20 | - | - | - | Can manage runners, job triggers and variables |
21 | - | - | - | Add deploy keys to project |
22 | - | - | - | Able to manage clusters |
23 | - | - | - | Configure project hooks |
24 | - | - | - | Can enable/disable the branch protection |
25 | - | - | - | Able to rewrite or remove Git tags |