Git Introduction

Git is a free and open source version control tool. It keeps the history of your project files, so you can see what changed, who changed it and when. If something goes wrong, you can go back to an older version.

Git was created by Linus Torvalds in 2005 to manage the source code of the Linux kernel. Today almost every software team in the world uses it, from one person projects to companies with thousands of developers.

Why should you use Git?

  • History: every change is saved with a message, so you always know why a file was changed.
  • Undo: you can bring back an old version of a file or of the full project.
  • Teamwork: many people can work on the same project at the same time, without overwriting the work of each other.
  • Branches: you can try a new feature in a separate line of work, and join it to the main code only when it is ready.
  • Backup: when you push your code to a server (like GitHub or GitLab), you have a copy of it outside your computer.

Git is distributed

Every person has the full copy of the project, with its full history, on their own computer. So most Git commands work without internet. You only need internet when you share your work with others (push) or take their work (pull).

Git and GitHub are not the same

This is a very common confusion. Git is the tool which is installed on your computer. GitHub, GitLab and Bitbucket are websites which host Git repositories, and add extra features like pull requests, issues and a web interface.

GitGitHub
A command line tool, installed on your computerA website (service) to host Git repositories
Tracks changes in filesStores the repository online and helps teams to work together
Works offlineNeeds internet
Free and open sourceHas free and paid plans

A quick look at Git

Here is the full life of a small change. We create a project, save one file and see the history. Do not worry if you do not understand each command yet, we will learn them one by one in the next chapters.

Terminal output

$ mkdir my-project
$ cd my-project
$ git init
Initialized empty Git repository in /home/robin/my-project/.git/
$ echo "Hello Git" > hello.txt
$ git add hello.txt
$ git commit -m "Add hello.txt"
[main (root-commit) c964195] Add hello.txt
1 file changed, 1 insertion(+)
create mode 100644 hello.txt
$ git log –oneline
c964195 (HEAD -> main) Add hello.txt

The hash (the code like a3f9c1e) in your own history will be different, because it depends on the time and your name.

What you will learn in this tutorial

  • Install and configure Git.
  • The basic commands: init, add, commit, status, log and diff.
  • Branches, merging and how to solve a merge conflict.
  • Undo things safely with restore, reset and revert.
  • Work with remote repositories: clone, push, fetch and pull.
  • More tools like stash, tag, rebase, cherry-pick and reflog.
Note: Every command output in this tutorial is taken from a real Git run, so you can type the same commands and compare. Next chapter: Version Control System.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Create a Git repository and make your first commit
Interview Questions & Answers
What is Git?
Why SSH key is used in Git?
How to create SSH key and use for GitLab?
What is the difference between Git and GitHub?
What is a repository in Git?