Git Tutorial for Beginners with Examples (2024)

Today I would like to bring Git Tutorial for beginners with examples. Here we cover the following

What is a Version Control System

In software engineering, version control systems are a category of software tools that help a software team (multiple developers, designers, and team members) to work together on the same project and allow them to manage changes to computer programs, documents, websites, and other information over time.

Version Control Systems maintains all the edits and historic versions of the project. So that teams can recall specific versions later.

Version Control is useful if you are a software engineer or if you are into software development.

Let’s say that we are working on a source code. in this case, we add/edit/delete lines of code. Every time we make a change and save the file, we lose the previous changes. Tracking revisions over time becomes more complex when we are working on a large project with multiple files and multiple developers. We not only want to record changes but also want to record who made the changes and when.

Version Control System is required to manage revisions at this level.

Version Control is a technology that allows software engineers to control the version of the piece of code. Version control helps in collaboration among the developers/users to help them work independently on their modules as well as be updated with the changes happening on the other modules of the projects.

It’s safe to have a version control while developing a software. It’s always risky to develop without having a version control system as not having backups. As time progresses, there is a significant amount of evolution on the applications on which the developers are working on. So they need to have different versions of the application so that in the future if they want to revert some functionality they can revisit the previous version and do intended behavior.

Types of Version Control Systems

Local version control systems:

Git Tutorial for Beginners with Examples (1)

The files are copied into another directory (most likely a time-stamped directory). It does not record or maintain any information regarding why we made changes. It is quite simple but at the same time incredibly error-prone. This model will work only if one person is working on one project.

Centralized version control systems:

Git Tutorial for Beginners with Examples (2)

A centralized version control system is used when multiple people are working on the same project. If a person is working on Computer A and another person is working on Computer B and both are working on the same project, both of them can check out the code. But the problem with this system is that if both the persons change the code on the same line then whose code will be updated in the central database will depend on who added it in the last.

Distributed version control systems:

Git Tutorial for Beginners with Examples (3)

In this system, multiple people working on their own computers on a single project communicate with the central database as well as they can also communicate with each other. Using this model, all the changes done by everyone can be stored and no data will be lost.

What is Git?

Git is one of the most popular version control systems. It is one of the distributed version control systems. It is an open-source project which is compatible with many operating systems and IDEs.
It allows us to track changes in an application, or in a folder, or in a single file over time across different users, and different computers. Git takes a snapshot of all files whenever we create a new commit. Every committer on a project always has a copy of the whole repository on their machine. So, we can commit to the Git file system also in offline mode.

Git was created by Linus Torvalds in 2005 for the development of the Linux Kernel, with other kernel developers contributing to its initial development.

Git Installation

Check out this Step By Step Guide on How To Install Git on Windows Machine.

After installation, we need to let the Git know who we are. To do that we need to configure Git is to add a username or a name and email.

This is not used to log in anywhere. This is only used to track who made what changes.

git config --global "Github_Username"git config --global ""

Note: ReplaceGithub_Username and Your_Email@Domain.comwith your GitHub credentials.

What is GitHub and why use GitHub?

GitHub is a cloud-based Git repository hosting service. It lets individuals and teams work together on projects.

Git is a command-line tool whereas GitHub comes with a web-based graphical interface.

GitHub is an application allowing you to store remote repositories on their servers. It also provides a user-friendly platform to interact with and manage your repositories. It is a public platform that allows millions of users to share their projects with the world.

The main purpose is to allow people to collaborate together to build projects. But it’s not just limited to collaboration. On top of that, it can be used as a portfolio for your best work. One of the most useful features of GitHub is being able to access your repository from any location. Also, it’s an industry standard for hosting Git repositories.

Git vs GitHub:

We have learned what is Git and What is GitHub so far. Check this post where we have mentioned the difference between Git and GitHub in detail.

Git Lifecycle

Git Tutorial for Beginners with Examples (4)

Working Directory: Stores all the project files

Staging Area: Acts an intermediate storage area

Repository: Holds all the committed files

Before Git tracks a change, it goes through a long chain of operations and tasks. Many of these tasks are user-controlled and are required for changes to be tracked correctly.

Before moving further, let’s talk about Repositories.

What is a Repository in GitHub?

Repositories, usually called ‘repos’, store the full history and source control of a project. They can either be hosted locally or on a shared server such as GitHub. Most repositories are stored on GitHub, while core contributors make copies of the repository on their machine and update the repository using the push/pull system. Any repository stored somewhere other than locally is called a ‘remote repository’.

First of all, pull all the information from your remote repository and make a direct copy of it in your local machine.

The working directory is your project at its current state and time. It contains all your files and everything at its current state. Let’s say you made a new file ( and wrote some code in this file. So now you want to change or take this and take a snapshot of it and put it in your source control.

So after making the changes, you add your file to the staging area and commit your changes. Commit is just pretty much taking a snapshot and putting it in source code. The staging area is the bundle of all the modifications to the project that are going to be committed. So after you add your file to the staging area and commit, you then put them in the local repository.

So, you have made a bunch of changes and you have changed the local repository. Now local and remote repositories don’t match. Now you would push it to the remote repository and now they both matched up.

How to setup GitHub account:

1. Go to and signup for a GitHub account using username, email, and password.

Git Tutorial for Beginners with Examples (5)

2. Once you fill out your information, you will see the verify account screen. Verify the captcha and click the Join a free plan button.

Git Tutorial for Beginners with Examples (6)

3. Now you need to fill out some other information related to programming experience and kind of work etc.

Git Tutorial for Beginners with Examples (7)

4.Now your GitHub account is created.

How to create a GitHub repository

1. Go to the GitHub home page.

Git Tutorial for Beginners with Examples (8)

2. Click on Create repository. Add repo name, description and you can choose Private or Public. You can’t choose Private unless you have a paid plan.

Git Tutorial for Beginners with Examples (9)

3. For now, let’s initialize this repository “myrepo” with a README file. You can add a license too. There are many different licenses for different things. For now, let’s choose an MIT license because this is the license that allows people to freely distribute it and copy it however they want.

Git Tutorial for Beginners with Examples (10)

4. This is what a repository looks like. There are a bunch of things we can see. There are a number of total commits, then we have the number of branches, number of releases, contributors, and license.

Git Tutorial for Beginners with Examples (11)

5. There are a bunch of other tabs at the top of the page.

Issues: Issues can be labeled by contributors. Anyone who is working on your project can actually find an issue and list it. What you and other contributors can do is go and look through and find these issues and try and fix them. So it’s a really useful feature on Github.

Pull Request: To request access to pull your project onto their local machine.

Projects: This is just a bunch of workflow management. Projects can be managed in the same place you keep your code.

Wiki: It is actually a bigger place to layout something like a summary. It’s just a kind of quick and concise place to list your project. You can list documentation of some of the application libraries or other things.

Insights: Insights allow you to see a lot of different features of your repository. It is just like Google Analytics or any other analytics program. It includes merged pull requests, open pull requests, and a bunch of different metrics.

Settings: In settings, we can change some of the things about our repository. You can change the name of your repo, delete your repository. You can invite collaborators from here. There is an option to switch between the default branches and other branches.

Fundamental Git commands:

1. git –help: When this command is hit in our terminal, Git will give a list of its most common commands. If it is followed by a specific command then it will give you a full detailed description of that particular command.

2. git config: With this command, we have to first set username and email address. Then we have to tell Git how it should handle the line endings, carriage return, and line feed on our machine, depending on the operating system we have. This is achieved with the configuration auto control line feed (autocrlf) and safe control line feed (safecrlf).
With the option –system, –global, or — local, we tell Git for which scope on our machine we want to set a specific configuration.

–system contains configurations for every user on all their repositories on one machine.

–global gives the configuration for one particular user

–local contains the configurations for a specific repository

The command is :

git config --system/global/localgit config --global "Github_Username"git config --global ""core.autocrlf input(true)core.safecrlf warncore.editor "code --wait"

Initializing a Git Repository

To start using Git, the first step is to initialize a repository, a location for files, and their revision history.

Let’s create a new repository now. Create a folder or a directory “MySoftware”. Then switch to that directory. Once inside the folder, use the git init command to convert the directory into an empty Git repository.

With this command, a hidden directory named “.git” is generated. This is created by Git and it’s where it stores all of the information about our repository. Note that the master branch is always the default branch when we initialize a Git project.

To see the files generated by git init, run the following command: ls -a

First, change Directory. Here I am changing the directory to MySoftware folder in my case.

Git Tutorial for Beginners with Examples (12)
Git Tutorial for Beginners with Examples (13)

Adding & Removing files:

Now we are going to be working with the staging area. In general, files in a repository can have the following statuses:

1. Not tracked
2. Staged
3. Committed

To find out the actual status of files in a repository, the git status command is used. Let’s say we have created a file ‘readme.txt’ in our repository.

Git Tutorial for Beginners with Examples (14)

– Running git status will have the following output:

Git Tutorial for Beginners with Examples (15)

– As you can see the file readme.txt is untracked. Untracked means that the file is not being tracked by Git for changes. We should explicitly say which files it should follow/track.

– The git add ‘file name’ command tells Git to track the file. This step is called staging. Let’s add our readme.txt file:

Git Tutorial for Beginners with Examples (16)

– Now, our file is in the staging area. Let’s say we have more untracked files. If now we will do git status, we can see there are untracked files. To add all files in the staging area together, we use “git add .” command

Git Tutorial for Beginners with Examples (17)

Note: Thetouch command in Git is the easiest way to create new, empty files.

– Now let’s say we want to delete a file ‘’. If we run the git status command, it shows 3 files to be committed and 1 file deleted.

Git Tutorial for Beginners with Examples (18)
Git Tutorial for Beginners with Examples (19)

– Run “git add .” to update files in the staging area.

Git Tutorial for Beginners with Examples (20)

.gitignore file :

If you don’t want Git to track some specific files, you can ignore them. That is done using the .gitignore file. Anything listed in the .gitignore file is ignored by Git and won’t be visible in the repository. Let’s create a .gitignore file and password.txt file.

Write a sample text inside the password.txt and edit the content of .gitignore adding the file name (“password.txt”) we want to be ignored.

Git Tutorial for Beginners with Examples (21)

Running git status shows that only the .gitignore file is untracked. The files listed here are in the Staging area and they are not in our repository yet. To add them to the repository, we need to commit them.

Git Commit :

The git commit command saves the state of your project by adding snapshots of staged files to the repository. This command includes the -m flag with a message describing what we have changed.

The command is: git commit -m “message”

Executing git status after a git commit verifies that tracked files are up to date. Only changes to staged files(added using the git add command) will be added to the repository with the git commit command.

If you want to see the history of your commits, use the git log command. This command inversely lists your commits, their dates, authors, and some other related information about each commit.

Git Tutorial for Beginners with Examples (22)

Cloning and Pushing :

Now after committing the changes, the subsequent step is to push the local repository to the Git server on a remote location (such as GitHub or Bitbucket).

After creating a remote repository we have the following options:

1. Clone the repository and start making changes.

2. Initialize a local repository and then connect it with the remote repository.

Cloning should be done at the very start of the project. The clone command is nothing but downloading a remote repository:

Go to your GitHub repository. In the top right above the list of files, click on the Clone or download the drop-down menu.

Copy the URL for cloning over HTTPS

git clone

If you have already initialized a local repository, you can connect it to the remote repository using the following command:

git remote add origin

Git Tutorial for Beginners with Examples (23)

Pushing Remotely

After making our local changes and commits, it’s time to push the changes to the remote repository. The push command lets you transfer commits from local to the remote repository.

git push -u origin master

Git Tutorial for Beginners with Examples (24)

Let’s suppose the name of our remote is origin and the default local branch name is master. The -u indicates that parameters need to be remembered so that next time we can simply run git push, Git will know what to do.


git pull command simply downloads the latest content from the remote repository and copies the latest changes to the local repository.

The command to pull new changes on our local repository is:

git pull origin master

Git Tutorial for Beginners with Examples (25)

To check what is different from our last commit, use git diff command.

git diff HEAD

Reset & Checkout:

To check changes within files that have already been staged, run git diff –staged

You can even reset the staging area using the reset command:

git reset ‘file name’

This reverts the file from the staged status and all the changes will still remain in the file. You can use the checkout command to reset the file to the latest committed version:

git checkout — ‘file name’


Branches are a very important part of Git. Branching allows you to make a copy of your working project and change it without affecting the main branch (master branch), giving an opportunity to work on the same project with different commits.

We create a new branch whenever we want to work on a new feature or fix a bug. After the work is done on that branch, it is then merged with the master branch.

Creating a new branch is done using the branch command:

git branch new_branch

Then we need to switch to the branch using the checkout command:

git checkout new_branch

Git Tutorial for Beginners with Examples (26)

There is a shortcut to create and switch to a new branch:

git checkout -b new_branch

Now, every change made in the new branch called new_branch won’t affect the master branch. This means you can safely work on the project without breaking anything. Each branch has its own history, staging area, and working directory.

In order to see the list of your branches, run the git branch command in the project directory.


The git stash command temporarily caches any changes you have made to your working copy so you can switch to something else, and then come back and recover them later.

After stashing you can make any changes and perform other git operations such as create new commits and switch branches. Then you can come back and reapply your stash.

The stash stays locally on your Git repository. They will not be transferred to the server when you push.

There are 2 ways to re-load (re-apply) the stashed changes:

git stash pop

pop will remove stashed changes from the stashed state.

git stash apply

apply applies the same stashed changes to multiple branches.

Fetching (git fetch):

We get all the data by fetching. This means all the remote branches and all its objects, commits, snapshots, files are fetched into the local repository.

The command is: git fetch

Merging (git merge) :

We can merge those changes with merging which we have fetched into our local repository into the current active branch in the working directory, thereby also the staging area. This simply means our index file is updated.

The command is: git merge

In order to remove a branch that you don’t need anymore, the command is:

git branch -d branchname

Hope you have learnt basics of Git from this Git Tutorial.

If you have any questions, please comment below.

Related Posts:

  • Git Installation on Windows
  • Git vs GitHub: What’s the difference
Git Tutorial for Beginners with Examples (2024)


How to use Git for beginners? ›

Any important git and GitHub terms are in bold with links to the official git reference materials.
  1. Step 0: Install git and create a GitHub account. ...
  2. Step 1: Create a local git repository. ...
  3. Step 2: Add a new file to the repo. ...
  4. Step 3: Add a file to the staging environment. ...
  5. Step 4: Create a commit. ...
  6. Step 5: Create a new branch.

What are the 5 basic commands with Git? ›

The five basic Git commands beginners should learn first are init, config, add, status and commit.

What is Git with example? ›

Git is a DevOps tool used for source code management. It is a free and open-source version control system used to handle small to very large projects efficiently. Git is used to tracking changes in the source code, enabling multiple developers to work together on non-linear development.

What is the easiest Git tool? ›

To make the process easier, we've listed our top recommendations.
  • QGit.
  • Gitg.
  • Git Force.
  • Sourcetree.
  • GitHub Desktop.
  • TortoiseGit.
  • GitUp.
  • Fork.
Feb 29, 2024

Is Git easy to learn? ›

There are no special skills and experience required for anyone to start using Git in small and medium scale projects. Git tool uses only a few basic commands for creating and managing repositories. Users with little or no experience can learn this tool to scale their career in the right direction.

What's the difference between Git and GitHub? ›

The key difference between Git and GitHub is that Git is a free, open source version control tool that developers install locally on their personal computers, while GitHub is a pay-for-use online service built to run Git in the cloud. Git is a piece of software. GitHub is an online SaaS service.

Which is the mostly used git command? ›

Essential Git Commands and Its Examples
  • Git init. Initialize a new Git repository in the current directory. ...
  • Git commit. Record staged changes in the repository's history. ...
  • Git branch. List, create, or delete branches. ...
  • Git checkout. Switch between branches or commits. ...
  • Git merge. ...
  • Git pull. ...
  • Git remote. ...
  • Git clone.
Apr 27, 2023

How to write git commands? ›

  1. Git add. Moves changes from the working directory to the staging area. ...
  2. Git branch. This command is your general-purpose branch administration tool. ...
  3. Git checkout. ...
  4. Git clean. ...
  5. Git clone. ...
  6. Git commit. ...
  7. git commit --amend. ...
  8. git config.

How does Git work for dummies? ›

With Git, every time you commit, or save the state of your project, Git basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot.

Why is Git difficult to learn? ›

The challenges of learning Git can vary. For experienced programmers, understanding Git's elaborate documentation and collaboration tools may be challenging, while for inexperienced programmers, learning to code in tandem with Git might be the most difficult aspect.

How long does it take to learn Git? ›

Students with coding experience can learn the basic features of Git in as little as twenty minutes and become proficient in only a few hours of guided study. Students without coding experience will need to learn the skills that Git utilizes to learn Git, which will take much longer.

How to use Git for the first time? ›

Download and install git. Set up git with your user name and email. (Don't type the $ ; that just indicates that you're doing this at the command line.) The first of these will enable colored output in the terminal; the second tells git that you want to use emacs.

How to use Git properly? ›

How Git works
  1. Create a "repository" (project) with a git hosting tool (like Bitbucket)
  2. Copy (or clone) the repository to your local machine.
  3. Add a file to your local repo and "commit" (save) the changes.
  4. "Push" your changes to your main branch.
  5. Make a change to your file with a git hosting tool and commit.

Should beginners use Git? ›

2. Track your progress. Understanding the main concepts and commands of the git, you can track your own progress. You can go some back because you noticed that you may have to master branch something wrong or because you has done something right in the past, but removed it because you thought it was wrong.

Is it easy to use Git? ›

Important Git and GitHub commands

Git itself is very simple to work with. Beginners are often surprised to find out there are only about 12 git commands developers use on a regular basis, including the following: git pull to get changes from the server. git push to update a remote repo.

What is the difference between Git and GitHub? ›

The key difference between Git and GitHub is that Git is a free, open source version control tool that developers install locally on their personal computers, while GitHub is a pay-for-use online service built to run Git in the cloud. Git is a piece of software. GitHub is an online SaaS service.

How long does it take to learn basics of Git? ›

Students with coding experience can learn the basic features of Git in as little as twenty minutes and become proficient in only a few hours of guided study. Students without coding experience will need to learn the skills that Git utilizes to learn Git, which will take much longer.


Top Articles
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 5825

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.