Git Basics
This tutorial provides an introduction to Git and GitLab. Once you learn how to use GitLab, it is not much different to use GitHub.
Git, like other version control (VC) software/system (see a Wikipedia list), tracks changes to a file system over time. It is typically used in software development but can be used to monitor changes in any file.
Git - a version control system that records the changes to a file or files which allows you to return to a previous version
📝 Note: This tutorial uses only the command line. After you have learned the basics of Git, you can explore a Git workflow in the command line, with the Atom text editor, and also common Git scenarios.
Git Resources
When we talk about Git, we say that a repository stores files. This term means that you have a folder that is currently being tracked by Git. It is common, although optional, to use one of the Git repository (repo) services (GitHub, GitLab, BitBucket, etc.). You could easily set up Git tracking on your local machine only, but one of the perks to using Git is that you can share your files with others and a team can edit files collaboratively. The ability to collaborate is one of the many reasons why hosted Git repos are so popular.
Repository - the Git data structure which contains files and folders, as well as how the files/folders have changed over time
Accessing GitLab
In your browser, navigate to https://gitlab.com/ and login using your credentials. Click on the green button at the top of the window that says
New project
.Choose the
Blank project
tab, create a name for the project, and select the "Visibility Level" that you prefer. Then clickCreate project
.Notice that GitLab has provided instructions to perform Git setup and initialization of your repository. We will follow those instructions.
(Optional) Prior to cloning the repository, consider adding your SSH key to your GitLab profile so you are not prompted for credentials after every commit. To add your public SSH key to GitLab:
Click on your user image in the top-right of the GitLab window.
Select
Settings
.On the left, click
SSH keys
.Paste your public SSH key in the box, provide a title, and save by clicking
Add key
.
Local Machine Setup
First, use the command line to see if Git is installed. (Windows users may check their list of currently installed programs.)
To install or update Git using your package manager:
CentOS, RedHat:
Debian, Ubuntu:
MacOS, use Homebrew:
Windows: download Git for Windows and install it. Also, this tutorial utilizes a Bash command line interface, therefore, you should use Git Bash, which is a part of the Git installation package for Windows.
Setup Git with your access credentials to GitLab with the following commands:
You can review the information that you entered during set-up:
git config --global --list
Now, navigate to the location where you'd like to place your repository. For example:
Clone the repository. A new folder is created, and Git starts tracking. Consult the repository information from the GitLab new repository window.
Clone - is the equivalent of making a local copy on your computer
GitLab also recommends the creation of a
README.md
file to describe the repository. (We will edit the contents of the README.md file later.)The next three steps consist of
adding
,committing
, andpushing
from your local machine to GitLab.Add - includes the added files in the content that you want to save Commit - creates a "snapshot" of the repository at that moment and uses the changes from the "added" files Push - moves/uploads the local changes (or snapshot) to the remote GitLab repository
(Optional) If you like, you can refresh your browser page, and you can see that the
README.md
file is now in your repository.
Using Branches to Make Changes
Branches are created as a way to separate content that is still under development. One way to think about a branch is as a copy of the content of a repository at a point in time. You'll then make your changes on the copy before then integrating the changes back into the original. For example, if you were using your GitLab repo to host a website, you probably would not want incomplete content shown to those who would visit your site. Instead, you can create a branch, make edits to the files there, then merge your development branch back into the master
branch, which is the default branch. Additionally, branches are commonly used when multiple individuals work out of a single repository.
Branch - a version of the repository that splits from the primary version Merge - using the changes from one branch and adding them to another
A branch checkout enables you to make changes to files without changing the content of the
master
branch. To create and checkout a branch called "add-readme":
Checkout - Git command to change branches
Now we edit the
README.md
file to add a description of the repository. The file needs to be opened with a text editor (nano, vim, emacs, etc.).Add your description.
README.md
is a markdown file. If you do not know how to use markdown, don't worry. Basic text works, too. However, if you would like to learn markdown, it is simple. Use this GitLab tutorial.To type in
vi
, pressi
for insert. Now you can add content.To save your changes and exit
vi
, press<esc>
to leave editing, then type:wq
which writes (saves) and quits.
As before, we need to add, commit, and push the changes to the GitLab repository.
In future pushes, you can simplify the last command by typing only
git push
. However, the first time you push to a new branch, you have to tell GitLab that you have created a new branch on your computer and the changes that you are pushing should be pushed to a new remote branch calledadd-readme
.
Merging Content from a Development Branch to the Master Branch
After completing the previous section, we have two branches: add-readme
and master
. We are ready to move the add-readme
content to the master
branch.
You can create a merge request using the GitLab GUI.
From the left menu panel in Gitlab, select
Merge Request
then the greenNew merge request
button.Select your branch in the "Source Branch" side (
add-readme
).Target branch is master.
Click
Compare branches and continue
.
You can add as much information to the next screen as you like, but the only thing needed is:
Assign to: < Project Owner, etc. >
In our case, we are the project owner, so we may assign the merge request to ourselves.
Click
Submit merge request
.
On the next page, click the green
Merge
button.
From the left menu panel in Gitlab, select Overview
to see the new README.md
content.
External Reference Material
Sometimes Git repository sites use different terminology, i.e., merge request vs. pull request. To reference the glossaries:
Ready to Learn More?
Last updated