Git tutorial for beginners
What is Git?
Git is the most used version control system in the world today. It was developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel.
If you are looking for the best Git Tutorial for beginners you are in the right place!
The purpose of Git is to manage a project, or a set of files, as they change over time. Git stores this information in a data structure called a repository, commonly referred to as a repo.
Git maintains a full history of all changes made to a file.
A Git repository contains all the information relating to the changes to a file over time. These can then be used to track changes to a file or roll back changes that have been made in error.
I am going to teach you why you should be using version control and the new world of Git, Github, BitBucket, Gitlab, repositories, repo’s & branches.
Hi, my name is Roger Perkin, I am a network engineer and I use Git for Network Automation.
Check out my Git for Network Engineers course – coming soon
Should network engineers learn Git?
A lot of network engineers will take config files and save them to their desktop with a filename like routerbackup-18th-april-v2-osfp-change.txt
Then another engineer will make a change to a device and save another file to their machine called hubrouter-20thapril.txt
We are all guilty of this and it’s a hard habit to break. But with the world of network automation, we as network engineers can learn a lesson from the software development guys who have been using Git for years.
Having a system where you have a single file but can also review all the changes to that file makes a lot of sense in the world of computer networks.
With the advances in network automation network engineers are embracing Git & Github to take control of their code.
So the answer is Yes! Network Engineers do need to learn Git.
Typical use cases are:
- Network device configuration files
- Ansible playbooks
- Python scrips
In order to use Git to track your code you need to install it.
This is a very simple process. Depending on what device you are working with I have listed all the options below – or you can just watch the video.
5 Steps to using Git for the first time
- Install Git
- Tell Git some basic info about you
- Create a folder on your machine
- Initialise Git within that folder
- Add files to Git
- Commit files to the repository
Git Basics – command-line fundamentals
Whilst there are many ways to use Git with a GUI or within your code editor it is very beneficial to understand how Git works under the hood and to learn the command-line fundamentals.
Git vs Github
There is a common misconception that Git and Github are the same thing.
This is not the case.
Git and Github are two completely different things.
Git is a locally installed piece of software that allows you to version control your code on your device.
Github is a web based platform at https://github.com/ which is used to provide an online repository of your code. You connect your local Git installation to your remote Github repository and then sync your code. You can then collaborate with other developers / network engineers.
It’s like Google Drive for your code.
You can also easily provide access to your code to others.
Github provide free plans which give you a public repository, if you want private repositories you have to sign up for a paid plan.
Update: Since Jan 20019 Github now also provides unlimited private repositories with a limited number of collaborators.
Debian (e.g. Ubuntu)
apt-get install git
Fedora (e.g. Centos / Red Hat Linux)
yum install git
Basic Git Commands
So now you should have Git installed we can start getting into it with some basic Git commands.
For the rest of this post I will be using an Ubuntu installation, but the steps will be the same for all devices.
Check Git Version
To check Git is installed correctly and verify the version issue the command below:
roger@ubuntu:~$ git --version git version 2.17.1 roger@ubuntu:~$
Next we are going to create a folder for this demo. You will probably already have a folder with your code in that you want to version control we will cover that option later.
Once I have created the folder I enter the command git status and it tells you that Git is not enabled for this folder.
roger@ubuntu:~$ roger@ubuntu:~$ mkdir GITfolder roger@ubuntu:~$ git status fatal: not a git repository (or any of the parent directories): .git roger@ubuntu:~$
The reason you get the error fatal: not a git repository is because we have not initialised Git on the folder.
To do that enter the command git init
roger@ubuntu:~$ cd GITfolder roger@ubuntu:~/GITfolder$ git init Initialized empty Git repository in /home/roger/GITfolder/.git/ roger@ubuntu:~/GITfolder$
We have now enabled Git to start tracking changes to files within this folder.
So let’s create a file to start tracking, I will also enter a line of text into the file.
roger@ubuntu:~/GITfolder$ roger@ubuntu:~/GITfolder$ touch file1.txt roger@ubuntu:~/GITfolder$ echo "line of text 1" > file1.txt roger@ubuntu:~/GITfolder$ cat file1.txt line of text 1 roger@ubuntu:~/GITfolder$
If we now issue the git status command you will see the following.
This image shows us that we are:
- On the master branch (more on that later)
- There have been no commits
- We are not tracking file1.txt
So the next step is to add the file1.txt file into Git so we can start tracking changes to the file.
To add the file1.txt file to Git you just enter the command
git add <filename>
Our file, file1.txt is now being tracked by Git.
So let’s make a change to that file and see what Git detect.
roger@ubuntu:~/GITfolder$ echo "Changed the line of text" > file1.txt roger@ubuntu:~/GITfolder$ roger@ubuntu:~/GITfolder$ roger@ubuntu:~/GITfolder$ cat file1.txt Changed the line of text roger@ubuntu:~/GITfolder$
Now we will issue the command git status again to see what has changed.
You can see now the Git has detected that a change has been made to the file. It now has a modified status and is showing as red. So let’s add the file again and see what happens.
roger@ubuntu:~/GITfolder$ git add file1.txt roger@ubuntu:~/GITfolder$ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: file1.txt
However, at this time we have not committed the file into Git, the files have not been staged. So to commit the file you enter the command
git commit -m “First Commit”
You have to put a message with the commit with the -m flag and then any comment you want.
roger@ubuntu:~/GITfolder$ git commit -m "First Commit" [master (root-commit) c9ee375] First Commit 1 file changed, 1 insertion(+) create mode 100644 file1.txt roger@ubuntu:~/GITfolder$ git status On branch master nothing to commit, working tree clean roger@ubuntu:~/GITfolder$
You can see now after I enter the command git status that the working tree is clean. This means that all changes to our file have been committed to the Git repository. Because we did not commit the file on the first change it does not have any knowledge of the first text, so I will make one more change to the file and commit it again. Git will then have the 2 changes in it’s log.
One of the most used features of Git is Git clone – this enables anyone to pull down some code from a remote repository (typically github.com) and use it on their local machine.
For a more in depth list – check out my specific Git Commands post
Git pull request
Best way to learn Git
The best way to learn Git is to just get started, so what is stopping you, install Git and start using it today!
I am currently in the process of creating some git training to add to my network automation courses
Frequently asked questions
Is Git easy to learn?
It is very easy to learn the basics of Git if you are working on your own, you can track changes to your files and roll back with Git. Where it becomes a bit harder is when you are working with other developers. But as with all things, learning Git basics is not hard, but becoming a master takes a bit more time!
What is the best way to learn Git?
I would suggest you do a search on google for the best courses to learn Git and sign up for a basics overview, then once you understand how it works, install Git and just start using it for your day to day code.
How do I start learning Git?
To start learning Git all you need is to have an installation of Git on your device, type Git init within the folder you want to version control and start trying some of the commands above to see what they do. They best way to start learning Git is by doing it.
Is Git easy to learn?
Learning the basics of Git are very easy, if you just have your own files and are only tracking your own changes the process is very simple. Where it gets more complicated is when you have multiple users making changes to the same files at the same time and you have to start dealing with merge conflicts etc.
There are 1000’s of Git tutorials online, I hope this one has been some help to you.
Content to add
git delete branch
git rename branch
git for windows
git vs github
how to use git
what is git and github