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
Git training
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.
https://github.blog/2019-01-07-new-year-new-github/
There are also many other options for places to put your code online e.g. ButBucket and you could even install your own GitLab server on site.
Windows
https://git-scm.com/download/win
MAC
https://git-scm.com/download/win
Debian (e.g. Ubuntu)
apt-get install git
Fedora (e.g. Centos / Red Hat Linux)
yum install git
For more information please checkout – Git vs Github or Gitlab vs Github which is better?
Git Tutorial
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.
Git add
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
Git commit
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.
Git clone
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.
Can I use Git without GitHub?
Uou can use Git without GitHub. Git and GitHub serve different purposes, and it’s important to distinguish between the two:
Git is a version control system that allows you to track changes in your code over time. You can use it to manage your projects locally on your computer. Git enables you to create repositories (repos), make changes to your files, commit those changes, and manage different versions of your project without the need for a remote service like GitHub.
GitHub is a cloud-based hosting service that lets you manage Git repositories. It provides a web-based graphical interface. It offers collaboration features such as bug tracking, feature requests, task management, and wikis for every project. GitHub makes it easier to collaborate on projects and share code with others on the internet, but it’s not required to use Git.
If you want to use Git without GitHub, you can do so by working with Git locally
Is Git written in Python?
No, Git is not written in Python. Git is primarily written in C, along with some shell scripts for its interactive components. C was chosen for Git to ensure performance and efficiency, especially important for a version control system that needs to handle large codebases efficiently. The choice of C also allows Git to run on various platforms and operating systems with minimal dependencies.
What is Git used for?
Git is used for version control, which is the practice of tracking and managing changes to software code or other sets of information over time. Its capabilities are essential for individual developers and teams alike, enabling a variety of tasks:
Source Code Management (SCM): Git helps in managing the source code history, allowing developers to see previous versions of a project, understand changes, and revert to earlier versions if necessary.
Collaboration: Git supports collaborative coding efforts, allowing multiple developers to work on the same project without interfering with each other’s changes. Developers can work on different features or sections of a project simultaneously, merge their changes into the main codebase, and resolve conflicts that may arise.
Branching and Merging: Git allows developers to create branches, enabling them to work on new features or bug fixes in isolation from the main codebase. Once work on a branch is complete, it can be merged back into the main branch, often the “master” or “main” branch, to make those changes part of the main project.
Change Tracking: With Git, every change to the codebase is tracked, including who made the change, what the change was, and when it was made. This detailed change history is crucial for understanding the development of a project, debugging issues, and auditing changes for security or compliance reasons.
Backup and Restore: The distributed nature of Git means that every developer’s copy of the codebase can act as a backup. This redundancy can protect against data loss. Developers can also revert their projects to previous states, offering a safety net against mistakes.
DevOps and Continuous Integration/Continuous Deployment (CI/CD): Git integrates seamlessly with CI/CD pipelines, automating the testing and deployment of software. This integration helps in ensuring that new code changes do not break or negatively affect the application.
Experimentation: Git encourages experimenting with new ideas in a controlled manner through branching. Developers can try out new features or changes without affecting the main codebase, and easily abandon the experiment if it doesn’t work out, or merge it if it’s successful.
Overall, Git is an indispensable tool for modern software development, offering a robust framework for managing code changes, facilitating teamwork, and automating software delivery processes.
Is Git free to use?
Yes, Git is free to use. It is an open-source version control system distributed under the GNU General Public License version 2 (GPLv2), which means you can use, modify, and distribute it without paying a license fee. This openness and flexibility have contributed to Git’s widespread adoption across individual projects, enterprises, and open-source initiatives. You can download and install Git from its official website or through package managers on various operating systems.
What language is Git written in?
Git is primarily written in C. This choice was made to ensure both portability and performance across various operating systems and computing environments. The use of C allows Git to efficiently handle the complexities of version control, such as managing file systems, directory structures, and the performance-intensive tasks required for large codebases. Additionally, some components and scripts of Git utilize shell scripting and Perl for auxiliary functions, such as interactive user interfaces and hooks.
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 rebase
git stash
git meaning
git delete branch
git rename branch
git merge
git clone
git for windows
Related Searches
git download
git gui
git tutorial
git vs github
git commands
how to use git
what is git and github
Leave a Reply