Understanding
the Workflow of
  Version Control
                                  ›
           presented by TOWER Version control with Git - made easy
                                      The Basics
                                                  1
                                                                Work on an Existing Project
          Start a New Project
                                                          $ git clone <remote-url>
$ git init
                                                      The “git clone” command is used to
Executing the “git init” command in the root          download a copy of an existing repository
folder of your new project creates a new and          from a remote server. When this is done, you
empty Git repository. You’re ready to start           have a full-featured version of the project on
getting your files under version control!             your local computer – including its complete
                                                      history of changes.
                      2
                                                                                   File Status
                                                                         Files that aren’t yet under version
          Work on Your Files
                                                           ?
                                                                         control are called “untracked”…
Modify, rename and delete files or add new                               …while files that your version
ones. Do all of this in your favorite editor /                           control system already knows
IDE / file browser – there‘s nothing to watch                            about are “tracked” files.
out for in this step!                                                    A tracked file can either be
                                                                         “unmodified” (meaning it wasn’t
                                                                         changed since the last commit)...
                                                          m
                                                                         ...or “modified” (meaning it has
                                                                         local changes since it was last
                                                                         committed).
                      3
                                                      $ git status
          Keep the Overview                           #
                                                      # Changes not staged for commit:
$ git status                                          #     modified:    about.html
                                                      #     deleted:     robots.txt
The “git status” command tells you what               #
happened since the last commit: which files           # Untracked files:
did you change? Did you create any new ones           #     login.html
or delete old ones?                                   #
                                                      no changes added to commit
  Add Files to the “Staging Area”                     $   git add about.html
                                                      #
$ git add <filename>                                  #   Changes to be committed:
                                                      #       modified:   about.html
Only because a file was changed doesn’t               #
mean it will be part of the next commit!              #   Changes not staged for commit:
Instead, you have to explicitly decide which          #       deleted:    robots.txt
changes you want to include. To do this, you          #
add them to the so-called “Staging Area”              #   Untracked files:
with the “git add” command.                           #       login.html
     Commit all Staged Changes
$ git commit -m "message"
                                                      $ git commit -m "Updated about page"
A commit wraps up all the changes you
previously staged with the “git add”                  [master 9d3f32b] Updated about page
command. To record this set of changes in             1 file changed, 29 insertions(+)
Git’s database, you execute the “git commit”
command with a short and informative
message.
          Keep the Overview
                                                      $ git status
$ git status                                          #
                                                      # Changes not staged for commit:
Running the “git status” command right after
                                                      #     deleted:     robots.txt
a commit proves to you: only the changes
                                                      #
that you added to the Staging Area were               # Untracked files:
committed.                                            #     login.html
All other changes have been left as local             #
changes: you can continue to work with them           no changes added to commit
and commit or discard them later.
    Inspect the Commit History
                                                      $ git log
$ git log
                                                      commit 9d3f32ba002110ee0022fe6d2c5308
The “git log” command lists all the commits           Author: Tobias Günther <tg@fournova.c
that were saved in chronological order. This          Date:   Mon Jul 8 09:56:33 2013 +0200
allows you to see which changes were made
in detail and helps you comprehend how the                     Updated about page
project evolved.
                        Branching & Merging
                                                  1
                                                                  Understanding Branches
                                                                                   BUGFIX #32         C4
                                                      FEATURE A         C1                  C3                  C5                C7
                                                                FEATURE B         C2                                      C6
         Start a New Feature                          We often have to work on multiple things
                                                      in parallel: feature X, bugfix #32, feature Y…
$ git branch <new-branch-name>                        This makes it all too easy to lose track of
                                                      where each change belongs. Therefore, it’s
Whenever you start a new feature, a new
                                                      essential to keep these contexts separate
experiment or a new bugfix, you should
                                                      from each other.
create a new branch. In Git, this is extremely
fast and easy: just call “git branch <new-            Grouping related changes in their own
branch-name>” and you have a new,                     context has multiple benefits: your
separate context.                                     coworkers can better understand what
                                                      happened because they only have to look at
Don’t be shy about creating new branches: it
                                                      code that really concerns them. And you can
costs you nothing.
                                                      stay relaxed, because when you mess up,
                                                      you mess up only this context.
                                                      Branches do just this: they provide a context
                                                      that keeps your work and your changes
                                                      separate from any other context.
                      2                                                           HEAD Branch
                                                                                                           C6        C7        feature-b
            Switch Contexts                               C1                           C4        C5        master
$ git checkout <new-branch-name>                                   C2        C3        feature-a       HEAD
To start working on a different context, you
                                                      At each point in time, you can only work in
need to tell Git that you want to switch to it.
                                                      one context – the context of the currently
You do this by “checking out” the branch with
                                                      checked out branch (which is also called the
the “git checkout” command.
                                                      “HEAD” branch in Git).
Every commit you make – until you switch
                                                      Your project’s working directory contains the
branches again – will be recorded in this
                                                      files that correspond to this branch. When
branch and kept separate from your other
                                                      you check out a different branch (make
contexts.
                                                      it “HEAD”), Git replaces the files in your
                                                      working directory with the ones that match
                                                      this branch.
           Integrate Changes
$ git merge <branch-to-integrate>
When your new feature is ready, you might
want to integrate it into another branch (e.g.
your production or testing branch).
First, switch to the branch that is supposed
to receive these changes. Then, call the “git
merge” command with the name of the
branch you want to integrate.
                        Sharing Work via
                       Remote Repositories
                                                  1
       Track a Remote Branch
                                                                     Publish a Local Branch
$ git checkout --track
  <remote/branch>                                         $ git push -u <remote>
                                                            <local-branch>
If there’s an interesting remote branch that
you want to work on, you can easily get               To share one of your local branches with
your own local copy. Use the “git checkout”           your teammates, you need to publish it on a
command and tell it which remote branch               remote server with the “git push” command.
you want your new local branch to base off.
                                                               Local & Remote Repositories
                      2                                    MODIFY, ADD &
                                                            DELETE FILES
                                                           MAKE COMMITS
                                                                 LOCAL                                                    REMOTE
          Stay Up-To-Date
                                                                                          SHARE WORK
                                                               COMPUTER                                                   SERVER
       About Remote Changes                                      LOCAL
                                                               REPOSITORY                COLLABORATE
                                                                                                                       REMOTE
                                                                                                                     REPOSITORY
                                                               VIEW HISTORY
$ git fetch <remote>                                           ADD & DELETE
                                                                 BRANCHES
When collaborating with others on a
project, you’ll want to stay informed about           As Git is a so-called “decentralized” version
their changes. The “git fetch” command                control system, a remote repository is
downloads new changes from a remote                   optional. In fact, everything we did until now
repository – but doesn’t integrate them into          happened on your local machine, in your local
your local working copy. It only informs you          repository – no internet/network connection
about what happened on the remote, leaving            was necessary.
the decision on what to integrate to you.             However, if you want to collaborate with
                                                      others, you need a remote repository on a
                                                      server. You don’t have to share all of your
                                                      work though: you can decide for each of your
                                                      local branches if you want to share it or not.
     Integrate Remote Changes
$ git pull
To integrate new changes from the remote
repository, you simply call “git pull”.
This will update your current HEAD branch
with new data from its counterpart branch
on the remote. The changes will be directly
merged into your local working copy.
        Upload Local Changes
        to the Remote Server
$ git push
To upload the local changes you made in your               Version control with Git - made easy
current HEAD branch, all you have to do is                     30-day free trial available at
call “git push”.                                                  www.git-tower.com