GitLab Usage

[This is part 3/8 on GitLab Continuous Integration series]

To use GitLab as a Source Code Repository there is a work flow:

  1. You work in your machine in a directory of your preference.
  2. Add to that directory, control files so it becomes a local code repository.
  3. Use the commands so it can connect to a repository in a server (remote repository).
  4. You upload your code to the server (commit the added files and push the code).

Example

The first step to upload our code to our source control GitLab instance is creating the structure in the proper directory and then specifying the URL to our project in the server.

  1. Create content, like directories and files
# Position yourself into the directory
$ mkdir ~/Desarrollo/ci_cd/gittestproject
$ cd ~/Desarrollo/ci_cd/gittestproject

# Add 3 directories and 3 files
$ mkdir test1 test2 test3
$ touch test1/testing1 test2/testing2 test3/testing3
  1. Create a Local Code Repository
$ git init
# Answer
Initialized empty Git repository in ~/Desarrollo/gittestproject/.git/
  1. Register your GitLab Server user credentials
# This is for our GitLab:
$ git config --global user.name "devguy"
$ git config --global user.email "devguy@example.gitlab.com"

$ git config --global --list
  1. To upload our files we first need to set the remote location
    The notation to refer to a repository is
# Simple:
git@url:port:repo.git
   
# Full:
Protocol://user@password@url:port:repo.git

And the command to set the first URL using the default port 80 is:

# For the 'example.gitlab.com' domain and 'gittestproject' repository:
$ git remote add origin http://example.gitlab.com/devguy/gittestproject.git

The complete format to set the credentials for login:

# Add user:password
$ git remote add origin http://devguy:clave123@example.gitlab.com/devguy/gittestproject.git
  1. List the remote repository name:
$ git remote show
# Answer
origin
   
$ git remote -v
# Answer
origin  http://devguy:clave123@example.gitlab.com/devguy/gittestproject.git (fetch)
origin  http://devguy:clave123@example.gitlab.com/devguy/gittestproject.git (push)
  1. Add our files to the source control system:
  • ‘add .’ includes all files to the source control
  • ‘commit’ sends files to stagged area
  • ‘push’ sends the staged files to the server
$ git add .
$ git commit -a -m "Test directories and files added."

# Push local commits to the master branch of the origin remote:
# git push -u [remote] [branch]
$ git push origin master
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 424 bytes | 212.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote: The private project devguy/gittestproject was successfully created.
remote: To configure the remote, run:
remote:   git remote add origin http://gitlab.example.com/devguy/gittestproject.git
remote: To view the project, visit:
remote:   http://gitlab.example.com/devguy/gittestproject
To http://example.gitlab.com/devguy/gittestproject.git
 * [new branch]      master -> master

There is a graphical user interface tool:

$ gitk .

Obtain a copy of a remote repository

To get the code of a remote repository like ‘GitHub’ you can download a zip file and the create a repository as we did in las section but you loose the configuration to send updates. In case you are submitting changes or you can ‘clone’ and download the files and maintain the link to that repository:

# Get to a directory one level up to the desired workin directory, then clone a repository:
$ git clone http://gitlab.example.com/devguy/example.git

Make modifications in the new directory. In this example we will add an empty README.md file to the local repository and push it to the remote server

$ cd example
$ touch README.md
$ git add README.md
$ git commit -m "add README"
$ git push -u origin master

Branch examples

These are commands to illustrate branch management:

# To get code from a repository
$ git clone http://devguy:clave123@192.168.1.221:8081/devguy/gittestproject.git
$ git pull --all

# To create a branch but do not switch to it
$ git branch future-plans

# Switch to a the new branch you just created it
$ git checkout future-plans

# To create a brach & switch:
$ git checkout -b <name-of-branch>

# Status
$ git status On branch

# Switch back to master
$ git checkout master

# Merge changes into master
# combine multiple sequences of commits into one unified history
$ git merge future-plans

# Delete branch
$ git branch -d future-plans

# Check activities performed
$ git log -3
commit 15b3f2b192e1ab92375c4208673a939a3b3f309f (HEAD -> master, origin/master)
Author: fjmartinez <francisco.javier.martinez.guzman@gmail.com>
Date:   Fri Dec 4 16:16:33 2020 -0600
    Test directories and files added.

$ git log –graph –pretty=oneline

# Differences between local, unstaged changes and the repository versions
$ git diff

# Undo the most recent commit (Avoid)
$ git reset HEAD~1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s