Let's set up
and see how does it work.
Git is a version control manager for your source code. When you are working on
some project, you will have multiple branches, lots of
versions, some co-workers that will modify the source code,
and you will need to handle it somehow.
The easiest way to describe why we need version control is to imagine
that you work on a project with your friend at the same time.
Both of you were editing the same file, on different computers. How are you going
to merge changes later into one file?
Another example is an experimental feature that you are
going to add to your project, that you want to ship
only to 20% of your customers. Making duplicate
folders for each version won't work well. Especially if
both branches will be maintained separately, and later you are going
to merge them into one project again.
helps you to do all of it. Git is a console tool.
However, you can find lots of graphic interfaces for it. For example
The terminal is enough in 90% for git - there are just 3-5 commands
user needs to know. Graphic Interface can be useful when you
want to see changes done to the code; browse commits history, etc.
To install git with Homebrew, just run next commands:
brew install git
Next, you may want to set up an account on some source code
hosting service like
You may need it in the future to upload your code, or to collaborate
with your colleagues. Account setup is very well explained on their
Ok, so Git is installed, let's try to learn how to use
it now with the terminal. Let's say we have three tasks:
- Create secret file with "INIT" text
- Add "My name is John" text to this file later
- Create a new "dev" branch, and add John's job title
- Merge changes to the master branch.
Let's setup this small project:
Create a new folder "git-lesson" somewhere. I will create
it in my Documents folder:
Change your current directory to freshly created "git-lesson":
Initialize git repository right inside of it. Repository
initialization is a one-time task that you need to do
for new projects:
Now you can open this folder in your favorite text editor, and
create a new file with "INIT" text. Or you can
do it in a terminal:
To make sure that our "git-lesson" folder has a new file you can use
echo "INIT" > secret_data
command in the terminal.
We can see what has been changed in our repository with
Or in Github Desktop application (make sure to add our
"git-lesson" repository into it.)
The first task
is finished now, and we need to save all changes somehow. We
are going to make a snapshot of a current project state.
we can select files that will be included in a current snapshot:
In case you want to track all files and folders
git add secret_data
If you are not familiar with Unix console, symbol
git add .
stands for all files and folders in your current directory.
At this moment, git knows what files it has to save. Now we are
ready to make a snapshot by committing all changes:
"Secret file added" is a commit's message. In the future, when you will
have hundreds of commits, this message will be the main thing
that will help you to navigate between your code versions, so make sure
to use short and easy to understand messages.
git commit -m "Secret file added"
Now we can see history of our commits with
In the second task, we need to add "My name is John" string
to the secret file.
As usual, we can do it in a console, or in a text editor. In console,
you would use command:
(notice double >> character - it will add text to the end of the file,
instead of creating a new file with given text)
echo "My name is John" >> secret_data
Let's see whether git knows about changes with
If you want more info on what has been changed, try
or with some graphical app for git:
Looks like the second task is finished as well. Adding files with changes
that have to be committed, and committing changes:
git add .
git commit -m "Added name"
Now we need to create a new branch in our project. Branches can
be created at any time, and from any commit, not necessary
the last one. Default git branch has name "master", and other branches
can have whatever name you choose. So, let's switch to new "dev"
Usually "git checkout" command is used to switch between branches
and commits. However, if you add "-b" argument, it will
create a new branch, and will make it active.
Let's check what branches we have at this moment:
git checkout -b "dev"
As you can see, there are only "dev" and "master" branches, and
"dev" is an active one. At this point, they are the same.
And let's add a new string to the end of the file. Again, you can
use the console or your text editor to do it.
When done, your "secret_data" file should
look like this:
Now we can check
output, or view changes in any git app:
As you can see, git knows about modified "secret_data" file.
It even highlights was part of it has been changed:
Let's commit changes, review history of commits,
and switch back to master branch:
git add .
git commit -m "Added job title"
git checkout master
Only one task left - we need to merge changes to our master branch.
Let's view the history of our commits, just to make sure that
the last commit on "master" branch was "Added name".
To merge branch "dev" into current branch,
use next command:
Done! Our "secret_data" file should have the latest version, and
include John's job title. If you run
git merge dev
you will see that latest commit from "dev" has been transferred
Obviously, these are not all git commands that you will use, but they
are most important ones. Later you will see that merging branches
can result in conflicts (when the same line in the same file
has been changed in both branches). You will learn how to resolve
those conflicts, will figure out how to switch between commits (
very easy actually, just use
git checkout XXX
command, and replace XXX with commit hash,
that you see in git log output), and much more.
You can try to experiment with git a little bit more: try to clone
existing repositories from
try to make some merge conflict on purpose, etc.
And whenever you feel ready, let's move on to the next chapter.
Book Index | Next