Understanding Git & GitLab Basics
- Nishant Nath
- Oct 7, 2023
- 5 min read
Introduction to GitLab and Version Control
Git is a Version Control Tool: Git is primarily focused on version control, tracking changes in code and files.
GitLab is a DevOps Platform: GitLab extends beyond version control to provide a comprehensive platform for managing the entire software development and delivery process.
Git Requires Self-Hosting: To use Git for collaboration, you need to set up your own Git server or use external hosting services.
GitLab Provides Built-in Hosting: GitLab offers built-in hosting, eliminating the need for users to set up their own Git servers. You can use GitLab.com or host GitLab on your own infrastructure.
What is version control and why it's important ?
History Tracking: Version control allows us to see all the changes made over time. We can go back to any previous version of our work if needed.
Collaboration: It's essential when working with others. Multiple people can edit files simultaneously, and version control merges their changes intelligently.
Backup: Our work is safely stored in a remote repository. Even if our computer crashes, we won't lose our files.
Experimentation: We can create different branches to experiment with new ideas without affecting the main project.
Git Install
We can download Git for free from the following website: https://www.git-scm.com/
The first thing we need to do, is to check if Git is properly installed:
git --version
Configure Git
Now let Git know who you are. This is important for version control systems, as each Git commit uses this information:
git config --global user.name "Enter your username"
git config --global user.email "Enter your email"
Creating Git Folder
Now, let's create a new folder for our project:
mkdir myproject
cd myproject
Initialize Git
Once we have navigated to the correct folder, we can initialize Git on that folder:
git init
Initialized empty Git repository in /Users/user/myproject/.git/
Git Adding New Files
We just created our first local Git repo. But it is empty.
So let's add some files, or create a new file using our favourite text editor.
vi index.html
Let's add some code now
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello world!</h1>
<p>This is the first file in my new Git Repo.</p>
</body>
</html>
Let's go back to the terminal and list the files in our current working directory:
ls
index.html
Then we check the git status and see if it is a part of our repo:
git status
On branch master
No commits yet
Untracked files: (use "git add ..." to include in what will be committed) index.html nothing added to commit but untracked files present (use "git add" to track)
Now Git is aware of the file, but has not added it to our repository!
Tracked - files that Git knows about and are added to the repository
Untracked - files that are in our working directory, but not added to the repository
When we first add files to an empty repository, they are all untracked. To get Git to track them, we need to stage them, or add them to the staging environment
Git Staging Environment
Staged files are files that are ready to be committed to the repository we are working.
For now, we are done working with index.html. So we can add it to the Staging Environment:
git add index.html
The file should be Staged. Let's check the status:
git status
On branch master
No commits yet
Changes to be committed: (use "git rm --cached ..." to unstage) new file: index.html
Git Add More than One File
We can also stage more than one file at a time.
Let's add a README.md file and a bluestyle.css file.
Now add all files in the current directory to the Staging Environment:
git add --all
Using --all instead of individual filenames will stage all changes (new, modified, and deleted) files.
Note: The shorthand command for git add --all is git add -A
Git Commit
Since we have finished our work, we are ready move from stage to commit for our repo.
Adding commits keep track of our progress and changes as we work.
When we commit, we should always include a message.
git commit -m "First release of Hello World!"
[master (root-commit) 221ec6e] First release of Hello World!
3 files changed, 26 insertions(+)
create mode 100644 README.md
create mode 100644 bluestyle.css
create mode 100644 index.html
Git Commit Log
To view the history of commits for a repository, we can use the log command:
git log
commit 09f4acd3f8836b7f6fc44ad9e012f82faf861803 (HEAD -> master)
Author: w3schools-test
Date: Fri Mar 26 09:35:54 2021 +0100
Updated index.html with a new line
commit 221ec6e10aeedbfd02b85264087cd9adc18e4b26
Author: w3schools-test
Date: Fri Mar 26 09:13:07 2021 +0100
First release of Hello World!
Working with Git Branches
In Git, a branch is a new/separate version of the main repository.
Branches allows us to work on different parts of a project without impacting the main branch and when the work is complete it can be merged with the main project.
Let's add some new features to our index.html page.
git branch feature/index.html
Let's confirm that we have created a new branch:
git branch
feature/index.html
* master
checkout is the command used to check out a branch. Moving us from the current branch, to the one specified at the end of the command:
git checkout feature/index.html
Switched to branch 'feature/index.html'
Let's confirm that we are in feature/index.html branch:
git branch
master
* feature/index.html
We can also create a branch
git checkout -b feature-2
Merge Branches
Let's merge the master and feature-2 branches.
First, we need to change to the master branch:
git checkout master
Switched to branch 'master'
Now we merge the current branch (master) with feature-2
git merge feature-2
Updating 09f4acd..dfa79db
Fast-forward
index.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
We can delete feature-2, as it is no longer needed:
git branch -d feature-2
Deleted branch feature-2 (was dfa79db).
GitLab Account
Go to GitLab and sign up for an account: https://gitlab.com/users/sign_up
Pulling to Keep up-to-date with Changes
When working as a team on a project, it is important that everyone stays up to date.
Any time we start working on a project, we should get the most recent changes to our local copy.
With Git, we can do that with pull. Pull is a combination of 2 different commands: fetch & merge.
git pull
fetch gets all the change history of a tracked branch/repo.
git fetch origin
We can also verify by showing the differences between our local master and origin/master:
git diff origin/master
Push Changes to GitLab
We can push from our local git and pushing them to GitLab.
git push origin <branch-name>
Fork a Repository
A fork is a copy of a repository. This is useful when we want to contribute to someone else's project or start our own project based on theirs.
Clone a Fork from GitLab
Now we have our own fork, but only on GitLab. We also want a clone on our local Git to keep working on it.
A clone is a full copy of a repository, including all logging and versions of files.
git clone < URL>
Repository for Projects: https://gitlab.com/devops1830941/git-basics
Comments