What is git
What is git
Git is a distributed version control system (DVCS). "Distributed" means that all
control system is simply software that lets you effectively manage application
These capabilities listed above don't tell how Git actually works, however. In
all its complexity, Git works quite simply: you first need to create a local
repository in your project's root directory (folder). Afterwards, Git can track
project files and directories and add them to the repository. You may be
(Throughout the entire article we'll use the term directory, not folder.) You
can't see repositories in your filesystem as they're hidden. But you can still
repositories.
If you store your stuff (code) at home (on a computer with a Git directory), you
different building. You may have heard about remote repositories such as
GitHub, BitBucket, and GitLab. They're like storehouses for code. Thanks to
Git, you can copy your entire project to a remote repository while keeping it in
Why do we use local and remote repositories? Let's explain a little bit.
In the real world, you can't have exactly the same stuff at home and in a
storehouse. If your things disappear from home (God forbid!), you'll be able to
recover copies or clones (at best) from a storehouse. And you'll still lose some
different story.
same code that you have in your local repository (on your home computer). If
your code disappears from your local repository, you can restore absolutely
the same code from a remote repository. A remote repository also serves as a
Find out why GitHub is a great communication tool for web developers.
By now it should be clear what Git is and what repositories are. But we also
need to mention two methods to run Git commands. You can use programs
with graphical user interfaces for Git. But you can also run terminal commands
for Git. The terminal will be your paper on which you'll write Git commands.
For the purpose of this article, we'll use the terminal (also called the command
line) to run Git commands. The terminal is a basic tool that all developers
should understand.
Lastly, you need to install Git on your computer. Follow these instructions if
you haven't done that already. Once you have Git installed, you can move on
to basic Git commands with examples to make friends with Git. You can
Configuring Git
When you come to a bank for the first time and ask to store your money there,
they give you a bunch of paperwork to fill out. Before you can use banking
services, you have to register with the bank. Git wants you to do the same
To tell Git who you are, run the following two commands:
real name and email. If not, you can easily change them by running the same
commands once more, but using your real name and email this time.)
Let's quickly review the syntax of Git commands. You first need to type "git",
is "--global" in the code above. The option "--global" means that you set your
username and email for Git globally on your computer. No matter how many
projects with separate local repositories you create, Git will use the same
There's one thing to configure before you start using Git. Since you'll see the
output from many Git commands in the terminal, it's best to have some pretty
colors for the output. To turn on code highlighting, just run the following
command:
Again, this configuration will be applied globally and it tells Git to highlight
The last basic configuration command will let you view your Git configurations.
Running this command is the same as asking for a copy of your contract:
That's enough for a start. You've got the feeling of Git by running several
simple configuration commands. But you haven't actually used Git yet. Let's
We'll finish each section with a Git commands list. Here are the basic Git
open a new safe deposit box to store our effects (read: code). Assuming
you've already created an empty directory for your project, you need to
explicitly ask Git to create a safe deposit box – a repository – in that directory:
$ git init
Initialized empty Git repository in /home/dell/new-
folder/.git/
view rawcreate-new-repository-with-git.sh hosted with ❤ by GitHub
The "init" command stands for initialize. Once you run "git init", Git will initialize
a hidden directory called ".git" in the project's root directory. And you'll get a
confirmation that your deposit box is ready! What's next? You might want to
know the status of your box: does it store anything yet? To know the Git
$ git
status
view rawgit-status-command.sh hosted with ❤ by GitHub
You'll run the command "git status" quite often. It's the same as calling a bank
different vault.
$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to
track)
view rawgit-status-with-output.sh hosted with ❤ by GitHub
The message above presents a few new terms, so let's clarify what they
mean.
Git Branches
You can consider branches in Git as paths. Imagine that you explore a new
territory and you mark the main path to water with poles each 10 to 15 meters.
This main path is like the master branch, and the poles are like commits. We'll
talk more about branches in the last section of the article. For now, it's
sufficient to know that Git has a base branch called the master branch.
Git Commits
A commit to a repository is a snapshot of the current state of the project's root
Let's say you're working with a bunch of papers. You've written ten texts about
animals on separate sheets of paper and you want to note what texts they are
and when you wrote them. You take out another sheet of paper, call it a
"commit," and write on this commit paper: "I've written text #1. It's about birds.
I've written text #2. It's about dogs..." Then you create a copy of each text. The
last thing you do is you gather those ten copies, pin the commit paper on top
The next day you rewrite the original texts, then get the copies from your
drawer and compare the texts. This is basically what Git does. You create files
and write code in them. When you're ready, you commit your files to a
repository: you create copies of files and lay them in a drawer (a repository).
(Our inner nerd wants to specify that Git doesn't actually push copies of files to
the repository; Git creates a light representation of the project files for
performance benefits.)
Each day you write that commit message and add new texts. Git creates a
history of your commits, so you can trace back to the very beginning of the
project development to see what files have been changed or added, who
we commit any files to a local repository, Git wants to know what those files
are. Git only knows what to commit when it's tracking files.
We've explained three basic Git concepts you need to know, but we've also
moved far away from explaining Git commands. Nevertheless, it's crucial to
grasp Git's basic concepts to understand how Git commands work. Now that
we've explained the meaning of Git concepts, we can get back to the
commands.
Let's remind you what output you'll see after you run "git status" for the first
time:
$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to
track)
view rawgit-status-with-output.sh hosted with ❤ by GitHub
Since there are no files in the root directory yet, Git shows that there's nothing
we need to populate the root folder with at least one file. We've added my-
new-file.txt to the root directory. Now we can move on to the next step.
When you run "git status" once more (assuming you've added a file to the
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
my_new_file.txt
nothing added to commit but untracked files present (use "git add" to
track)
view rawgit-status-with-untracked-files.sh hosted with ❤ by GitHub
Note the "Untracked files" message with the file "my_new_file.txt". Git
conveniently informs us that we've added a new file to the project. But that
isn't enough for Git. As Git tells us, we need to track "my_new_file.txt". In
following section will uncover the basic Git commands for working with the
staging area.
Let's say you want to move some of your valuable effects to a lock box, but
you don't know yet what things you'll put there. For now, you just gather things
into a basket. You can take things out of the basket if you decide that they
aren't valuable enough to store in a lock box, and you can add things to the
basket as you wish. With Git, this basket is the staging area. When you move
files to the staging area in Git, you actually gather and prepare files for Git
To let Git track files for a commit, we need to run the following in the terminal:
$ git add
my_new_file.txt
view rawadding-files-to-the-git-staging-area hosted with ❤ by GitHub
That's it; you've added a file to the staging area with the "add" command.
Don't forget to pass a filename to this command so Git knows which file to
track.
But what has this "add" command actually done? Let's view an updated status
(we promised that you'll often run "git status", didn't we?):
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to
unstage)
new file: my_new_file.txt
view rawgit-status-with-current-output.sh hosted with ❤ by GitHub
The status has changed! Git knows that there's a newly created file in your
basket (the staging area), and is ready to commit the file. We'll get to
committing files in the next section. For now, we want to talk more about the
What if you create or change several files? With a basket as your staging
area, you have to put things into the basket one by one. Committing files to
the repository individually isn't convenient. What Git can do is provide
Let's assume you've added another three files to the root directory: my-file.ts,
another-file.js, and new_file.rb. Now you want to add all of them to the staging
area. Instead of adding these files separately, we can add them all together:
All you need to do is type file names separated by spaces following the "add"
command. When you run "git status" once more to see what has changed, Git
will output a new message listing all the files you've added:
$ git status
Changes to be committed:
(use "git rm --cached <file>..." to
unstage)
Adding several files to the staging area in one go is much more convenient!
But hold on a second. What if the project grows enormously and you have to
add more than three files? How can we add a dozen files (or dozens of files)
in one go? Git accepts the challenge and offers the following solution:
$ git add
.
view rawadding-all-files-to-staging-area.sh hosted with ❤ by GitHub
Instead of listing file names one by one, you can use a period – yes, a simple
dot – to select all files under the current directory. Git will grab all new or
changed files and shove them into the basket (the staging area) all at once.
That's even more convenient, isn't it? But Git can do even better.
There's a problem with the "git add ." command. Since we're currently working
in the root directory, "git add ." will only add files located in the root directory.
But the root directory may contain many other directories with files. How can
we add files from those other directories plus the files in the root directory to
$ git add --
all
view rawadding-all-files-to-staging-area.sh hosted with ❤ by GitHub
The option "--all" tells Git: "Find all new and updated files everywhere
throughout the project and add them to the staging area." Note that you can
also use the option "-A" instead of "--all". Thanks to this simple option, "-A" or
Remember when we told you that you can take things out of your imaginary
basket? Git can also take things out of its basket by removing files from the
staging area. To remove files from the staging area, use the following
command:
The "--cached" option indicates files in the staging area. Finally, we pass a file
that we want to unstage. Git will output the following message for us:
$ rm
'my_file.ts'
view rawgit-status-with-current-output.sh hosted with ❤ by GitHub
$ git status
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: another-file.js
new file: my_new_file.txt
new file: new_file.rb
Untracked files:
(use "git add <file>..." to include in what will be
committed)
my-file.ts
view rawgit-status-with-current-output.sh hosted with ❤ by GitHub
Git is no longer tracking my-file.ts. In this simple way, you can untrack files if
"reset" command:
We've provided enough Git commands to add and remove files to and from
the staging area. Now it's time to get familiar with committing files to the local
repository.
Here are the most common Git commands you've learned so far:
you should have at least one file tracked by Git (we have three). As we
commit them: we need to carry our basket with stuff to the lock box. There are
several useful Git commands to do (almost) the same: move (commit) files
from the staging area (an imaginary basket) to the repository (a lock box).
There's nothing difficult about committing to a repository. Just run the following
command:
$ git commit -m "Add three
files"
view rawcommitting-files-to-repository.sh hosted with ❤ by GitHub
"commit" command the "-m" option, which stands for "message". Lastly, type
in your commit message. We wrote "Add three files" for our example, but it's
recommended that you write more meaningful messages like "Add admin
panel" or "Update admin panel". Note that we didn't use the past tense! A
commit message must tell what your commit does – adds or removes files,
Once we've run "git commit -m 'Add three files'", we get the following output:
The message tells us that there have been three files added to the current
branch, which in our example is the master or the main branch. The "create
mode 100644" message tells us that these files are regular non-executable
files. The "0 insertions(+)" and "0 deletions(-)" messages mean we haven't
added any new code or removed any code from the files. We actually don't
need this information; it only confirms that the commit was successful.
So, what have we done so far? We added files to a project directory in the first
section. Then we added files to the staging area, and now we've committed
existing file.
Add files to the staging area by using the "git add" command and
<message>" command.
Repeat.
That's enough to get the idea of Git's flow. But let's get back to committing
When working on a project, chances are you'll modify some files and commit
them many times. Git's flow doesn't really change for adding modified files to a
new commit. In other words, every time you make changes you'll need to add
a modified file to the staging area and then commit. But this standard flow is
tedious. And why should you have to ask Git to track a file that was tracked
before?
The question is how can we add modified files to the staging area and commit
them at the same time. Git provides the following super command:
$ git commit -a -m "Do something once
more"
view rawadding-and-committing-files-to-repository.sh hosted with ❤ by GitHub
Note the "-a" option, which stands for "add". Git will react to this command like
this: "I'll just commit the files immediately. Don't forget to write a commit
message, though!" As we can see, this little trick lets us avoid running two
commands.
There will be times when you'll regret committing to a repository. Let's say
you've modified ten files, but committed only nine. How can you add that
remaining file to the last commit? And how can you modify a file if you've
already committed it? There are two ways out. First, you can undo the commit:
As you may recall, the "reset" command is the opposite of the "add"
command. This time, "reset" tells Git to undo the commit. What follows "reset"
is the "--soft" option. The "--soft" option means that the commit is canceled
and moved before HEAD. You can now add another file to the staging area
branches. Currently we're in the master branch, and HEAD points to this
master branch. When we switch to a different branch later, HEAD will point to
the latest commit is at the top of the branch (HEAD). In the command "git
reset --soft HEAD^" the last character "^" represents the last commit. We can
read "git reset --soft HEAD^" as "Undo the last commit in the current branch
Instead of resetting the HEAD and undoing the last commit, we can rectify a
add the remaining file to the staging area and then commit:
The "--amend" option lets you amend the last commit by adding a new file (or
multiple files). Using the "--amend" option, you can also overwrite the
Think of this command in this way: you took out the top stack of papers from
the drawer and "amended" them by simply unstapling the bunch of papers,
By this time, you've done some work with Git on your computer. You've
created files, added them to the staging area, and committed them. But these
actions only concern your local repository. When working in a team, you'll also
use a remote repository. What are the basic Git commands to work with
branch).
Other developers pull your commits to their computers to have the latest
You'll use several important Git commands to move (push) your code from a
local repository to a remote repository and to grab (pull) your team's collective
Git has a "remote" command to deal with remote repositories. But before we
First things first, you need to create a remote repository. We'll use GitHub for
this section. You can create an account on GitHub and create a new
repository for your project. Just start a project and give it a name. You then
need to grab the HTTPS link to this new repository. The link will look similar to
We tell Git to "add" a repository. The "origin" option is the default name for the
server on which your remote repository is located. Lastly, there's a link to your
project on GitHub.
this.
Once you run the command above, Git will connect your local and remote
repositories. But what does this liaison actually mean? Can you already
All you did for now is signed papers so that the remote lock box (GitHub) can
accept various items (your code) from your home drawer (local repository). To
actually copy your things to a remote lock box, you need to personally carry
them to it. With Git, copying your code to a remote repository looks like this:
It's obvious that the command "push" tells Git to push your files to a remote
repository. What we also specified is the server our local repo is connected to
(origin) and the branch we're pushing, which is master. There's also that
strange "-u" option. What it means is that we're lazy enough not to run a long
"git push -u origin master" command each time we push code to the cloud.
Once you've pushed changes to a remote repository, you can develop another
feature and commit changes to the local repository. Then you can push all
changes to the remote repository once again, but using only the "git push"
command this time around. As we can see, Git tries to simplify things as much
as possible.
Is this the happy ending? Not yet. Once you push code to a remote repository,
you have to enter your username and password registered with that remote
repository.
The current problem with "git push" is that you have to enter your credentials
each time you push code to GitHub. That's not convenient. The root of this
problem is the HTTPS link you used to connect repositories. Git offers a way
Let's get back to the page where GitHub offered a link to our project:
As we can see, there's an SSH option that we can use instead of HTTPS. If
you set up Git on your computer to work with SSH, then you won't have to
enter GitHub credentials every time you push code to GitHub. You'll only need
As you can see, to connect repositories via SSH we only changed the link.
Keep in mind that HTTPS is the default protocol used for connecting with
Now that you've added a remote repository, you can view the list of
$ git remote
-v
view rawlisting-remote-repositories-in-console.sh hosted with ❤ by GitHub
The "-v" option will list all remote repositories you've connected to. This is
origin https://github.com/YourName/some-app.git
(fetch)
origin https://github.com/YourName/some-app.git
(push)
view rawlist-of-remote-repositories.sh hosted with ❤ by GitHub
So far we've talked about how to move your things to a remote lock box. Let's
say your drawer with all your valuables has disappeared from your home. Now
you want to get your things back from the lock box, kind of like cloning them.
Git can clone an entire project from a remote repository. That's what the
project. We've used an SSH link, but you can use the HTTPS link with the
same command.
What "git clone" does is it copies the entire project to a directory on your
computer. The directory will be created automatically and will have the same
If you don't like the name of the repository you're cloning, just pass your
repository and cloned a remote repository. We haven't said anything about the
when other developers push their changes to a remote repository, you'll want
to see their changes on your computer. That is, you'll want to pull their code to
$ git
pull
view rawpulling-from-remote-repository.sh hosted with ❤ by GitHub
Create a remote repository called origin and point it to the URL you
pass.
The last item simply means that you don't need to run "git remote add
The "clone" command will add a remote origin automatically, and you can
Grab this a step-by-step guide to the RubyGarage git and release management to set
The "pull" command doesn't create a new directory with the project name. Git
will only pull updates to make sure that your the local repository is up to date.
Basically, that's all you need to know about pushing, pulling, and cloning with
Git. One set of basic Git commands is left, though. Remember how we
greatest feature of Git, and they're very helpful. Thanks to branches, you can
The reason why we use branches lies on the surface. If you have a stable,
working application, you don't want to break it when developing a new feature.
Therefore, it's best to have two branches: one branch with a stable app and
another one for developing features. Then again, when you complete a
feature and it seems to be working, some bug may still be there. And bugs
In the simplest terms, you'll use branches to store various versions of your
project: a stable app, an app for testing, an app for feature development, and
so on. Actually, what we've described is just one possible way (but certainly
developers should decide how and when to create branches and then follow
Managing branches in Git is simple. Let's first see our current branches:
$ git
branch
*master
view rawlisting-git-branches.sh hosted with ❤ by GitHub
That's it: one command, "branch", will ask Git to list all branches. In our app,
far from being complete, and we need to develop new features. Let's say we
want to add a user profile feature. To create this feature, we need to create a
new branch:
Again, it's very simple: the "branch" command creates a new branch with the
name we gave it: "user-profile". Can we write code for our new feature right
$ git
branch
view rawlist-all-branches.sh hosted with ❤ by GitHub
Note the asterisk to the left of "master." This asterisk marks the current branch
you're in. In other words, if you create a branch and start changing code right
away, you'll still be editing the previous branch, not the new one. After you've
created a new branch to develop a feature, you need to switch to the new
For switching branches in Git, you won't use a "switch" command, as you
Git also notifies you that you've switched to a different branch: "Switched to
branch 'user-profile'". Let's run "git branch" once more to prove that:
master
*user-
profile
view rawswitching-to-branch.sh hosted with ❤ by GitHub
Hooray! You can now freely change any file, create and delete files, add files
to the staging area, commit files, or even push files to a remote repository.
Whatever you do under the user-profile branch won't affect the master branch.
new branches:
2. Switch to the new branch from the main branch using "git checkout
<branch-name>".
You're stuck on the third step. What should you do next? The answer is
simple: you need to use the "merge" command. To merge a secondary branch
branch), first switch back to the main branch. In our case, we should checkout
$ git checkout
master
*master
user-profile
view rawswitching-between-branches.sh hosted with ❤ by GitHub
The current branch is now changed to master, and we can merge the user-
Keep in mind that you're in the main branch and you're merging another
branch into the main – not vice versa! Now that the user-profile feature is in
the master branch, we don't need the user-profile branch anymore. So let's
With the "-d" option, we can delete the now unnecessary "user-profile". By the
way, if you try to remove the branch you're in, Git won't let you:
Let's mention a simpler command for creating new branches than "git branch
<name>". Given that you're in the main branch and you need to create a new
But instead of running two commands you can run only one:
This one command will let you create a new "admin-panel" branch and switch
to that branch right away. Git earns another point for improving the workflow.
Here's an extensive list of the most used Git commands with examples:
A Quick Recap
Git is a great tool to aid your development process.
From the point of view of web developers, Git is a huge heap of commands.
There are dozens of Git commands you should know. But for a start, it's
enough to familiarize yourself with the most basic Git commands that we've
Undo commits
Solid knowledge of the basic commands for the features listed above is
enough for beginners. Try these basic Git commands for yourself and you'll