<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="gittutorial(7)">
<articleinfo>
    <title>gittutorial(7)</title>
        <indexterm>
                <primary>gittutorial(7)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>gittutorial - A tutorial introduction to git (for version 1.5.1 or newer)</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<simpara>git *</simpara>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>This tutorial explains how to import a new project into git, make
changes to it, and share changes with other developers.</simpara>
<simpara>If you are instead primarily interested in using git to fetch a project,
for example, to test the latest version, you may prefer to start with
the first two chapters of <ulink url="user-manual.html">The Git User&#8217;s Manual</ulink>.</simpara>
<simpara>First, note that you can get documentation for a command such as
<literal>git log --graph</literal> with:</simpara>
<literallayout>$ man git-log</literallayout>
<simpara>or:</simpara>
<literallayout>$ git help log</literallayout>
<simpara>With the latter, you can use the manual viewer of your choice; see
<xref linkend="git-help(1)"/> for more information.</simpara>
<simpara>It is a good idea to introduce yourself to git with your name and
public email address before doing any operation.  The easiest
way to do so is:</simpara>
<literallayout>$ git config --global user.name "Your Name Comes Here"
$ git config --global user.email you@yourdomain.example.com</literallayout>
</simplesect>
<simplesect id="_importing_a_new_project">
<title>Importing a new project</title>
<simpara>Assume you have a tarball project.tar.gz with your initial work.  You
can place it under git revision control as follows.</simpara>
<literallayout>$ tar xzf project.tar.gz
$ cd project
$ git init</literallayout>
<simpara>Git will reply</simpara>
<literallayout>Initialized empty Git repository in .git/</literallayout>
<simpara>You&#8217;ve now initialized the working directory&#8212;you may notice a new
directory created, named ".git".</simpara>
<simpara>Next, tell git to take a snapshot of the contents of all files under the
current directory (note the <emphasis>.</emphasis>), with <emphasis>git-add</emphasis>:</simpara>
<literallayout>$ git add .</literallayout>
<simpara>This snapshot is now stored in a temporary staging area which git calls
the "index".  You can permanently store the contents of the index in the
repository with <emphasis>git-commit</emphasis>:</simpara>
<literallayout>$ git commit</literallayout>
<simpara>This will prompt you for a commit message.  You&#8217;ve now stored the first
version of your project in git.</simpara>
</simplesect>
<simplesect id="_making_changes">
<title>Making changes</title>
<simpara>Modify some files, then add their updated contents to the index:</simpara>
<literallayout>$ git add file1 file2 file3</literallayout>
<simpara>You are now ready to commit.  You can see what is about to be committed
using <emphasis>git-diff</emphasis> with the --cached option:</simpara>
<literallayout>$ git diff --cached</literallayout>
<simpara>(Without --cached, <emphasis>git-diff</emphasis> will show you any changes that
you&#8217;ve made but not yet added to the index.)  You can also get a brief
summary of the situation with <emphasis>git-status</emphasis>:</simpara>
<literallayout>$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD &lt;file&gt;..." to unstage)
#
#       modified:   file1
#       modified:   file2
#       modified:   file3
#</literallayout>
<simpara>If you need to make any further adjustments, do so now, and then add any
newly modified content to the index.  Finally, commit your changes with:</simpara>
<literallayout>$ git commit</literallayout>
<simpara>This will again prompt you for a message describing the change, and then
record a new version of the project.</simpara>
<simpara>Alternatively, instead of running <emphasis>git-add</emphasis> beforehand, you can use</simpara>
<literallayout>$ git commit -a</literallayout>
<simpara>which will automatically notice any modified (but not new) files, add
them to the index, and commit, all in one step.</simpara>
<simpara>A note on commit messages: Though not required, it&#8217;s a good idea to
begin the commit message with a single short (less than 50 character)
line summarizing the change, followed by a blank line and then a more
thorough description.  Tools that turn commits into email, for
example, use the first line on the Subject: line and the rest of the
commit in the body.</simpara>
</simplesect>
<simplesect id="_git_tracks_content_not_files">
<title>Git tracks content not files</title>
<simpara>Many revision control systems provide an <literal>add</literal> command that tells the
system to start tracking changes to a new file.  Git&#8217;s <literal>add</literal> command
does something simpler and more powerful: <emphasis>git-add</emphasis> is used both for new
and newly modified files, and in both cases it takes a snapshot of the
given files and stages that content in the index, ready for inclusion in
the next commit.</simpara>
</simplesect>
<simplesect id="_viewing_project_history">
<title>Viewing project history</title>
<simpara>At any point you can view the history of your changes using</simpara>
<literallayout>$ git log</literallayout>
<simpara>If you also want to see complete diffs at each step, use</simpara>
<literallayout>$ git log -p</literallayout>
<simpara>Often the overview of the change is useful to get a feel of
each step</simpara>
<literallayout>$ git log --stat --summary</literallayout>
</simplesect>
<simplesect id="_managing_branches">
<title>Managing branches</title>
<simpara>A single git repository can maintain multiple branches of
development.  To create a new branch named "experimental", use</simpara>
<literallayout>$ git branch experimental</literallayout>
<simpara>If you now run</simpara>
<literallayout>$ git branch</literallayout>
<simpara>you&#8217;ll get a list of all existing branches:</simpara>
<literallayout>  experimental
* master</literallayout>
<simpara>The "experimental" branch is the one you just created, and the
"master" branch is a default branch that was created for you
automatically.  The asterisk marks the branch you are currently on;
type</simpara>
<literallayout>$ git checkout experimental</literallayout>
<simpara>to switch to the experimental branch.  Now edit a file, commit the
change, and switch back to the master branch:</simpara>
<literallayout>(edit file)
$ git commit -a
$ git checkout master</literallayout>
<simpara>Check that the change you made is no longer visible, since it was
made on the experimental branch and you&#8217;re back on the master branch.</simpara>
<simpara>You can make a different change on the master branch:</simpara>
<literallayout>(edit file)
$ git commit -a</literallayout>
<simpara>at this point the two branches have diverged, with different changes
made in each.  To merge the changes made in experimental into master, run</simpara>
<literallayout>$ git merge experimental</literallayout>
<simpara>If the changes don&#8217;t conflict, you&#8217;re done.  If there are conflicts,
markers will be left in the problematic files showing the conflict;</simpara>
<literallayout>$ git diff</literallayout>
<simpara>will show this.  Once you&#8217;ve edited the files to resolve the
conflicts,</simpara>
<literallayout>$ git commit -a</literallayout>
<simpara>will commit the result of the merge. Finally,</simpara>
<literallayout>$ gitk</literallayout>
<simpara>will show a nice graphical representation of the resulting history.</simpara>
<simpara>At this point you could delete the experimental branch with</simpara>
<literallayout>$ git branch -d experimental</literallayout>
<simpara>This command ensures that the changes in the experimental branch are
already in the current branch.</simpara>
<simpara>If you develop on a branch crazy-idea, then regret it, you can always
delete the branch with</simpara>
<literallayout>$ git branch -D crazy-idea</literallayout>
<simpara>Branches are cheap and easy, so this is a good way to try something
out.</simpara>
</simplesect>
<simplesect id="_using_git_for_collaboration">
<title>Using git for collaboration</title>
<simpara>Suppose that Alice has started a new project with a git repository in
/home/alice/project, and that Bob, who has a home directory on the
same machine, wants to contribute.</simpara>
<simpara>Bob begins with:</simpara>
<literallayout>bob$ git clone /home/alice/project myrepo</literallayout>
<simpara>This creates a new directory "myrepo" containing a clone of Alice&#8217;s
repository.  The clone is on an equal footing with the original
project, possessing its own copy of the original project&#8217;s history.</simpara>
<simpara>Bob then makes some changes and commits them:</simpara>
<literallayout>(edit files)
bob$ git commit -a
(repeat as necessary)</literallayout>
<simpara>When he&#8217;s ready, he tells Alice to pull changes from the repository
at /home/bob/myrepo.  She does this with:</simpara>
<literallayout>alice$ cd /home/alice/project
alice$ git pull /home/bob/myrepo master</literallayout>
<simpara>This merges the changes from Bob&#8217;s "master" branch into Alice&#8217;s
current branch.  If Alice has made her own changes in the meantime,
then she may need to manually fix any conflicts.</simpara>
<simpara>The "pull" command thus performs two operations: it fetches changes
from a remote branch, then merges them into the current branch.</simpara>
<simpara>Note that in general, Alice would want her local changes committed before
initiating this "pull".  If Bob&#8217;s work conflicts with what Alice did since
their histories forked, Alice will use her working tree and the index to
resolve conflicts, and existing local changes will interfere with the
conflict resolution process (git will still perform the fetch but will
refuse to merge --- Alice will have to get rid of her local changes in
some way and pull again when this happens).</simpara>
<simpara>Alice can peek at what Bob did without merging first, using the "fetch"
command; this allows Alice to inspect what Bob did, using a special
symbol "FETCH_HEAD", in order to determine if he has anything worth
pulling, like this:</simpara>
<literallayout>alice$ git fetch /home/bob/myrepo master
alice$ git log -p HEAD..FETCH_HEAD</literallayout>
<simpara>This operation is safe even if Alice has uncommitted local changes.
The range notation HEAD..FETCH_HEAD" means "show everything that is reachable
from the FETCH_HEAD but exclude anything that is reachable from HEAD.
Alice already knows everything that leads to her current state (HEAD),
and reviewing what Bob has in his state (FETCH_HEAD) that she has not
seen with this command</simpara>
<simpara>If Alice wants to visualize what Bob did since their histories forked
she can issue the following command:</simpara>
<literallayout>$ gitk HEAD..FETCH_HEAD</literallayout>
<simpara>This uses the same two-dot range notation we saw earlier with <emphasis>git log</emphasis>.</simpara>
<simpara>Alice may want to view what both of them did since they forked.
She can use three-dot form instead of the two-dot form:</simpara>
<literallayout>$ gitk HEAD...FETCH_HEAD</literallayout>
<simpara>This means "show everything that is reachable from either one, but
exclude anything that is reachable from both of them".</simpara>
<simpara>Please note that these range notation can be used with both gitk
and "git log".</simpara>
<simpara>After inspecting what Bob did, if there is nothing urgent, Alice may
decide to continue working without pulling from Bob.  If Bob&#8217;s history
does have something Alice would immediately need, Alice may choose to
stash her work-in-progress first, do a "pull", and then finally unstash
her work-in-progress on top of the resulting history.</simpara>
<simpara>When you are working in a small closely knit group, it is not
unusual to interact with the same repository over and over
again.  By defining <emphasis>remote</emphasis> repository shorthand, you can make
it easier:</simpara>
<literallayout>alice$ git remote add bob /home/bob/myrepo</literallayout>
<simpara>With this, Alice can perform the first part of the "pull" operation alone using the
<emphasis>git-fetch</emphasis> command without merging them with her own branch,
using:</simpara>
<literallayout>alice$ git fetch bob</literallayout>
<simpara>Unlike the longhand form, when Alice fetches from Bob using a
remote repository shorthand set up with <emphasis>git-remote</emphasis>, what was
fetched is stored in a remote tracking branch, in this case
<literal>bob/master</literal>.  So after this:</simpara>
<literallayout>alice$ git log -p master..bob/master</literallayout>
<simpara>shows a list of all the changes that Bob made since he branched from
Alice&#8217;s master branch.</simpara>
<simpara>After examining those changes, Alice
could merge the changes into her master branch:</simpara>
<literallayout>alice$ git merge bob/master</literallayout>
<simpara>This <literal>merge</literal> can also be done by <emphasis>pulling from her own remote
tracking branch</emphasis>, like this:</simpara>
<literallayout>alice$ git pull . remotes/bob/master</literallayout>
<simpara>Note that git pull always merges into the current branch,
regardless of what else is given on the command line.</simpara>
<simpara>Later, Bob can update his repo with Alice&#8217;s latest changes using</simpara>
<literallayout>bob$ git pull</literallayout>
<simpara>Note that he doesn&#8217;t need to give the path to Alice&#8217;s repository;
when Bob cloned Alice&#8217;s repository, git stored the location of her
repository in the repository configuration, and that location is
used for pulls:</simpara>
<literallayout>bob$ git config --get remote.origin.url
/home/alice/project</literallayout>
<simpara>(The complete configuration created by <emphasis>git-clone</emphasis> is visible using
<literal>git config -l</literal>, and the <xref linkend="git-config(1)"/> man page
explains the meaning of each option.)</simpara>
<simpara>Git also keeps a pristine copy of Alice&#8217;s master branch under the
name "origin/master":</simpara>
<literallayout>bob$ git branch -r
  origin/master</literallayout>
<simpara>If Bob later decides to work from a different host, he can still
perform clones and pulls using the ssh protocol:</simpara>
<literallayout>bob$ git clone alice.org:/home/alice/project myrepo</literallayout>
<simpara>Alternatively, git has a native protocol, or can use rsync or http;
see <xref linkend="git-pull(1)"/> for details.</simpara>
<simpara>Git can also be used in a CVS-like mode, with a central repository
that various users push changes to; see <xref linkend="git-push(1)"/> and
<xref linkend="gitcvs-migration(7)"/>.</simpara>
</simplesect>
<simplesect id="_exploring_history">
<title>Exploring history</title>
<simpara>Git history is represented as a series of interrelated commits.  We
have already seen that the <emphasis>git-log</emphasis> command can list those commits.
Note that first line of each git log entry also gives a name for the
commit:</simpara>
<literallayout>$ git log
commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
Author: Junio C Hamano &lt;junkio@cox.net&gt;
Date:   Tue May 16 17:18:22 2006 -0700

    merge-base: Clarify the comments on post processing.</literallayout>
<simpara>We can give this name to <emphasis>git-show</emphasis> to see the details about this
commit.</simpara>
<literallayout>$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7</literallayout>
<simpara>But there are other ways to refer to commits.  You can use any initial
part of the name that is long enough to uniquely identify the commit:</simpara>
<literallayout>$ git show c82a22c39c   # the first few characters of the name are
                        # usually enough
$ git show HEAD         # the tip of the current branch
$ git show experimental # the tip of the "experimental" branch</literallayout>
<simpara>Every commit usually has one "parent" commit
which points to the previous state of the project:</simpara>
<literallayout>$ git show HEAD^  # to see the parent of HEAD
$ git show HEAD^^ # to see the grandparent of HEAD
$ git show HEAD~4 # to see the great-great grandparent of HEAD</literallayout>
<simpara>Note that merge commits may have more than one parent:</simpara>
<literallayout>$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
$ git show HEAD^2 # show the second parent of HEAD</literallayout>
<simpara>You can also give commits names of your own; after running</simpara>
<literallayout>$ git tag v2.5 1b2e1d63ff</literallayout>
<simpara>you can refer to 1b2e1d63ff by the name "v2.5".  If you intend to
share this name with other people (for example, to identify a release
version), you should create a "tag" object, and perhaps sign it; see
<xref linkend="git-tag(1)"/> for details.</simpara>
<simpara>Any git command that needs to know a commit can take any of these
names.  For example:</simpara>
<literallayout>$ git diff v2.5 HEAD     # compare the current HEAD to v2.5
$ git branch stable v2.5 # start a new branch named "stable" based
                         # at v2.5
$ git reset --hard HEAD^ # reset your current branch and working
                         # directory to its state at HEAD^</literallayout>
<simpara>Be careful with that last command: in addition to losing any changes
in the working directory, it will also remove all later commits from
this branch.  If this branch is the only branch containing those
commits, they will be lost.  Also, don&#8217;t use <emphasis>git-reset</emphasis> on a
publicly-visible branch that other developers pull from, as it will
force needless merges on other developers to clean up the history.
If you need to undo changes that you have pushed, use <emphasis>git-revert</emphasis>
instead.</simpara>
<simpara>The <emphasis>git-grep</emphasis> command can search for strings in any version of your
project, so</simpara>
<literallayout>$ git grep "hello" v2.5</literallayout>
<simpara>searches for all occurrences of "hello" in v2.5.</simpara>
<simpara>If you leave out the commit name, <emphasis>git-grep</emphasis> will search any of the
files it manages in your current directory.  So</simpara>
<literallayout>$ git grep "hello"</literallayout>
<simpara>is a quick way to search just the files that are tracked by git.</simpara>
<simpara>Many git commands also take sets of commits, which can be specified
in a number of ways.  Here are some examples with <emphasis>git-log</emphasis>:</simpara>
<literallayout>$ git log v2.5..v2.6            # commits between v2.5 and v2.6
$ git log v2.5..                # commits since v2.5
$ git log --since="2 weeks ago" # commits from the last 2 weeks
$ git log v2.5.. Makefile       # commits since v2.5 which modify
                                # Makefile</literallayout>
<simpara>You can also give <emphasis>git-log</emphasis> a "range" of commits where the first is not
necessarily an ancestor of the second; for example, if the tips of
the branches "stable-release" and "master" diverged from a common
commit some time ago, then</simpara>
<literallayout>$ git log stable..experimental</literallayout>
<simpara>will list commits made in the experimental branch but not in the
stable branch, while</simpara>
<literallayout>$ git log experimental..stable</literallayout>
<simpara>will show the list of commits made on the stable branch but not
the experimental branch.</simpara>
<simpara>The <emphasis>git-log</emphasis> command has a weakness: it must present commits in a
list.  When the history has lines of development that diverged and
then merged back together, the order in which <emphasis>git-log</emphasis> presents
those commits is meaningless.</simpara>
<simpara>Most projects with multiple contributors (such as the Linux kernel,
or git itself) have frequent merges, and <emphasis>gitk</emphasis> does a better job of
visualizing their history.  For example,</simpara>
<literallayout>$ gitk --since="2 weeks ago" drivers/</literallayout>
<simpara>allows you to browse any commits from the last 2 weeks of commits
that modified files under the "drivers" directory.  (Note: you can
adjust gitk&#8217;s fonts by holding down the control key while pressing
"-" or "+".)</simpara>
<simpara>Finally, most commands that take filenames will optionally allow you
to precede any filename by a commit, to specify a particular version
of the file:</simpara>
<literallayout>$ git diff v2.5:Makefile HEAD:Makefile.in</literallayout>
<simpara>You can also use <emphasis>git-show</emphasis> to see any such file:</simpara>
<literallayout>$ git show v2.5:Makefile</literallayout>
</simplesect>
<simplesect id="_next_steps">
<title>Next Steps</title>
<simpara>This tutorial should be enough to perform basic distributed revision
control for your projects.  However, to fully understand the depth
and power of git you need to understand two simple ideas on which it
is based:</simpara>
<itemizedlist>
<listitem>
<simpara>
The object database is the rather elegant system used to
    store the history of your project&#8212;files, directories, and
    commits.
</simpara>
</listitem>
<listitem>
<simpara>
The index file is a cache of the state of a directory tree,
    used to create commits, check out working directories, and
    hold the various trees involved in a merge.
</simpara>
</listitem>
</itemizedlist>
<simpara>Part two of this tutorial explains the object
database, the index file, and a few other odds and ends that you&#8217;ll
need to make the most of git. You can find it at <xref linkend="gittutorial-2(7)"/>.</simpara>
<simpara>If you don&#8217;t want to continue with that right away, a few other
digressions that may be interesting at this point are:</simpara>
<itemizedlist>
<listitem>
<simpara>
<xref linkend="git-format-patch(1)"/>, <xref linkend="git-am(1)"/>: These convert
    series of git commits into emailed patches, and vice versa,
    useful for projects such as the Linux kernel which rely heavily
    on emailed patches.
</simpara>
</listitem>
<listitem>
<simpara>
<xref linkend="git-bisect(1)"/>: When there is a regression in your
    project, one way to track down the bug is by searching through
    the history to find the exact commit that&#8217;s to blame.  Git bisect
    can help you perform a binary search for that commit.  It is
    smart enough to perform a close-to-optimal search even in the
    case of complex non-linear history with lots of merged branches.
</simpara>
</listitem>
<listitem>
<simpara>
<ulink url="everyday.html">Everyday GIT with 20 Commands Or So</ulink>
</simpara>
</listitem>
<listitem>
<simpara>
<xref linkend="gitcvs-migration(7)"/>: Git for CVS users.
</simpara>
</listitem>
</itemizedlist>
</simplesect>
<simplesect id="_see_also">
<title>SEE ALSO</title>
<simpara><xref linkend="gittutorial-2(7)"/>,
<xref linkend="gitcvs-migration(7)"/>,
<xref linkend="gitcore-tutorial(7)"/>,
<xref linkend="gitglossary(7)"/>,
<xref linkend="git-help(1)"/>,
<ulink url="everyday.html">Everyday git</ulink>,
<ulink url="user-manual.html">The Git User&#8217;s Manual</ulink></simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite.</simpara>
</simplesect>
</article>
