"Hello, Git! I hate you already!"
Most of us dislike Git on
the first try even after running the most basic Git commands. Having a Git
cheat sheet taped to our table doesn't help. Git is very complicated, as you
can't learn all its concepts by just using it.
Sadly, your life as a web
developer will also be complicated without Git.
What is
GIT?
Git is a
distributed version control system (DVCS). "Distributed" means that
all developers within a team have a complete version of the project. A version
control system is simply software that lets you effectively manage application
versions.
Git Tutorial
Before starting with the commands and operations let us
first understand the primary motive of Git.
The motive 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 Git repository. The repository is the core of Git.
To be very clear, a Git repository is the directory where
all of your project files and the related metadata resides.
Git
records the current state of the project by creating a tree graph from the
index. It is usually in the form of a Directed Acyclic Graph (DAG).
Operations & Commands
Some of the basic operations in Git are:
2. Add
3. Commit
4. Pull
5. Push
Some advanced Git operations are:
1. Branching
2. Merging
3. Rebasing
Let me first give you a brief idea about how these
operations work with the Git repositories. Take a look at the architecture of
Git below:
If you understand the
above diagram well and good, but if you don’t, you need not worry, I will be
explaining these operations in this Git Tutorial one by one. Let us begin with
the basic operations.
You need to install
Git on your system first. If you need help with the installation, click
here.
In this Git Tutorial,
I will show you the commands and the operations using Git Bash. Git Bash is a
text-only command line interface for using Git on Windows which provides
features to run automated scripts.
After installing
Git in your Windows system, just open your folder/directory where you want to
store all your project files; right click and select ‘Git Bash here’.
This will open up Git
Bash terminal where you can enter commands to perform various Git operations.
Now, the next task is
to initialize your repository.
Initialize
In order
to do that, we use the command git init. Please refer to the below
screenshot.
git init creates an empty Git repository or
re-initializes an existing one. It basically creates a .git directory
with sub directories and template files. Running a git init in
an existing repository will not overwrite things that are already there. It
rather picks up the newly added templates.
Now that my repository
is initialized, let me create some files in the directory/repository.
Let’s see if these
files are in my index or not using the command git status. The
index holds a snapshot of the content of the working tree/directory, and this
snapshot is taken as the contents for the next change to be made in the local
repository.
Git status
The git
status command lists all the modified files which are ready to be
added to the local repository.
Let us type in the
command to see what happens:
This shows that I have
two files which are not added to the index yet. This means I cannot commit
changes with these files unless I have added them explicitly in the index.
Add
This command updates
the index using the current content found in the working tree and then prepares
the content in the staging area for the next commit.
Thus, after making
changes to the working tree, and before running the commit command,
you must use the add command to add any new or modified files
to the index. For that, use the commands below:
git add
<directory>
or
git add <file>
Let me demonstrate
the git add for you so that you can understand it better.
I have created
directory. Let us add the files using the command git add -A. This
command will add all the files to the index which are in the directory but not
updated in the index yet.
Now that the new files are added to the index, you are
ready to commit them.
Commit
It
refers to recording snapshots of the repository at a given time. Committed
snapshots will never change unless done explicitly.
Here, C1 is the initial commit, i.e. the snapshot of the
first change from which another snapshot is created with changes named C2. Note
that the master points to the latest commit.
Now,
when I commit again, another snapshot C3 is created and now the master points
to C3 instead of C2.
Git aims
to keep commits as lightweight as possible. So, it doesn’t blindly copy the
entire directory every time you commit; it includes commit as a set of changes,
or “delta” from one version of the repository to the other. In easy words, it
only copies the changes made in the repository.
You can
commit by using the command below:
git commit
This
will commit the staged snapshot and will launch a text editor prompting you for
a commit message.
Or you
can use:
git commit -m “<message>”
Let’s
try it out.
As you can see above, the git commit command has committed the
changes in the four files in the local repository.
Now, if
you want to commit a snapshot of all the changes in the working directory at
once, you can use the command below:
git commit -a
I have
created my working directory blood donor. but they are not added to the
index yet.
Now I have made my
desired commits in my local repository.
Note that before you
affect changes to the central repository you should always pull changes from
the central repository to your local repository to get updated with the work of
all the collaborators that have been contributing in the central repository.
For that we will use the pull command.
Pull
The git pull command
fetches changes from a remote repository to a local repository. It merges upstream
changes in your local repository, which is a common task in Git based
collaborations.
But first, you need to
set your central repository as origin using the command:
git remote add origin
<link of your central repository>
Now that my origin is set, let us extract files from the
origin using pull. For that use the command:
git pull origin master
This
command will copy all the files from the master branch of remote repository to
your local repository.
Since my local repository was already updated with files
from master branch, hence the message is Already up-to-date. Refer to the
screen shot above.
Note: One can also try pulling files
from a different branch using the following command:
git pull origin
<branch-name>
Your
local Git repository is now updated with all the recent changes. It is time you
make changes in the central repository by using the push command.
Push
This
command transfers commits from your local repository to your remote repository.
It is the opposite of pull operation.
Pulling
imports commits to local repositories whereas pushing exports commits to the
remote repositories .
The use
of git
push is to publish your local changes to a central repository.
After you’ve accumulated several local commits and are ready to share them with
the rest of the team, you can then push them to the central repository by using
the following command:
git push <remote>
Note : This remote refers to the remote repository which
had been set before using the pull command.
This
pushes the changes from the local repository to the remote repository along
with all the necessary commits and internal objects. This creates a local
branch in the destination repository.
Let us now check if the changes took place in
my central repository.
Yes, it did. :-)
To
prevent overwriting, Git does not allow push when it results in a non-fast
forward merge in the destination repository.
Note: A non-fast forward merge means an upstream
merge i.e. merging with ancestor or parent branches from a child branch.
To
enable such merge, use the command below:
git push <remote> –force
The
above command forces the push operation even if it results in a non-fast
forward merge.
I hope
you have enjoyed this Git Tutorial and learned the commands and operations
in Git. Let me know if you want to know more about Git in the comments
section below :-)