Git Tutorial

Git Tutorial for beginners to learn Git online for free.

Git is the popular version control software that is used to track changes in the project files. Git is very helpful to avoid conflicts when multiple persons working on the same project.

In this tutorial, you will learn more about Git commands with example.

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…

Version Control System

A version control system (VCS) is a tool that records the changes of files over time. You can call back a specific version later. It is also called source control or revision…

Install Git

Before we start, we need Git on our computer. The installation is simple and takes only a few minutes. First check if Git is already installed, because many Mac and Linux computers…

Git Configuration

Before the first commit, you must tell Git who you are. Git adds your name and email to every commit you make. We do this with the git config command. Set your…

Git Workflow

To use Git well, you must understand where your files live at each step. Git has three main areas, and a file moves from one to the next. The three areas of…

Git Init

The git init command creates a new Git repository. You run it one time inside a project folder, and from then Git can track the files of that folder. Syntax Create a…

Git Clone

The git clone command makes a full copy of an existing repository, including all files, branches and the history. You use it when you want to start working on a project which…

Git Status

The git status command shows the current state of your working directory and the staging area. It tells you which files are changed, which are staged and which are not tracked by…

Git Add

The git add command puts your changes in the staging area. Git will include only the staged changes in the next commit. If you do not add a file, it will not…

Git Commit

A commit is a snapshot of your project at one moment. The git commit command takes everything in the staging area and saves it in the repository with a message. Every commit…

Git Log

The git log command shows the history of commits, from the newest to the oldest. It is the way to answer “who changed this, when and why?”. Syntax Make a small history…

Git Diff

The git diff command shows the exact lines which are changed. You can compare your working files, the staging area, two commits or two branches. Syntax Prepare an example Changes not staged…

Git Ignore (.gitignore)

Not every file in your project should go in Git. Log files, passwords, installed packages and build output should stay out. The .gitignore file is a plain text file where you list…

Git rm and mv: Delete and Rename Files

When you delete or rename a file from the normal file manager, Git notices it but you still need to tell Git about it. The commands git rm and git mv do…

Git Restore

The git restore command undoes changes in files. It can discard changes of your working directory, remove files from the staging area, or bring a file from an older commit. It was…

Git Commit Amend

Did you make a spelling mistake in your last commit message? Did you forget to add a file? You do not need a new commit for it. The git commit –amend command…

Git Reset

The git reset command moves the current branch to another commit. It is mostly used to undo the last commits. It has three modes: –soft, –mixed (default) and –hard. They are the…

Git Revert

The git revert command undoes the changes of an old commit by creating a new commit that does the opposite. The old commit stays in the history. This is why revert is…

Git Branch

A branch is a separate line of work. With a branch you can build a new feature or fix a bug without disturbing the main code. When your work is ready, you…

Git Checkout and Switch

To start working on a branch, you must move to it. Git has two commands for it: the newer git switch (Git 2.23 and later) and the older git checkout. Both work,…

Git Merge

The git merge command joins the work of one branch into another. You first move to the branch which will receive the changes (usually main), and then merge the other branch into…

Git Merge Conflict

A merge conflict happens when two branches change the same lines of the same file, and Git cannot decide which version to keep. Do not be afraid of it. It is normal…

Git Rebase

Rebase is another way to bring the changes of one branch into another. Merge keeps the true history with a merge commit. Rebase takes your commits and replays them on top of…

Git Stash

Imagine you are in the middle of a task, and your boss asks you to fix a urgent bug on another branch. Your work is not ready to commit. The git stash…

Git Tag

A tag is a permanent name for a commit. It is mostly used to mark release versions like v1.0 or v2.3.1. Unlike a branch, a tag does not move when you make…

Git Remote

A remote is a copy of your repository in another place, usually on a server like GitHub, GitLab or Bitbucket. The git remote command manages the connections to those places. The default…

Git Push

The git push command uploads your local commits to a remote repository, like GitHub or GitLab. Until you push, your commits are only on your computer. Syntax Push for the first time…

Git Fetch and Pull

When other people push to the remote, your local repository does not know about it. You need to bring those changes to your computer. Git has two commands for it: git fetch…

Git Cherry Pick

Sometimes you do not want a full merge. You only need one commit from another branch, for example a bug fix. The git cherry-pick command copies a commit and applies it on…

Git Reflog

The reflog is Git’s safety net. It is a private diary which records every time the tip of your branches or HEAD moves: commits, resets, merges, switches and more. When you think…

Git SSH Key

There are two ways to connect to a remote repository: HTTPS and SSH. With SSH you use a pair of keys, and you do not enter a username and password for each…

Git Submodule

A submodule is a Git repository inside another Git repository. It lets you use the code of another project (like a shared library) in your own project, and still keep the two…

Git Hooks

Git hooks are scripts which Git runs automatically when something happens, like before a commit or after a push. You can use them to check your code, run tests, or check commit…

Git Branching Strategy

A branching strategy is the set of rules which a team follows for creating branches and merging them. Without rules, a big team can mess the code quickly. Here are the most…

Git and GitHub

GitHub is a website where you can store your Git repositories online, share them and work with other people. GitLab and Bitbucket are similar services. They all use Git underneath, so the…

Git Commands Cheat Sheet

This page is a quick reference for the Git commands we learned in this tutorial. Keep it open while you practice. Most commands link to their full chapter. Setup and start Command…