b89b9dbc0f1cadb2010a4ca746a81dff53e8957b
   1Git User's Manual (for version 1.5.1 or newer)
   2______________________________________________
   3
   4This manual is designed to be readable by someone with basic unix
   5command-line skills, but no previous knowledge of git.
   6
   7<<repositories-and-branches>> and <<exploring-git-history>> explain how
   8to fetch and study a project using git--read these chapters to learn how
   9to build and test a particular version of a software project, search for
  10regressions, and so on.
  11
  12People needing to do actual development will also want to read
  13<<Developing-with-git>> and <<sharing-development>>.
  14
  15Further chapters cover more specialized topics.
  16
  17Comprehensive reference documentation is available through the man
  18pages.  For a command such as "git clone", just use
  19
  20------------------------------------------------
  21$ man git-clone
  22------------------------------------------------
  23
  24See also <<git-quick-start>> for a brief overview of git commands,
  25without any explanation.
  26
  27Also, see <<todo>> for ways that you can help make this manual more
  28complete.
  29
  30
  31[[repositories-and-branches]]
  32Repositories and Branches
  33=========================
  34
  35[[how-to-get-a-git-repository]]
  36How to get a git repository
  37---------------------------
  38
  39It will be useful to have a git repository to experiment with as you
  40read this manual.
  41
  42The best way to get one is by using the gitlink:git-clone[1] command
  43to download a copy of an existing repository for a project that you
  44are interested in.  If you don't already have a project in mind, here
  45are some interesting examples:
  46
  47------------------------------------------------
  48        # git itself (approx. 10MB download):
  49$ git clone git://git.kernel.org/pub/scm/git/git.git
  50        # the linux kernel (approx. 150MB download):
  51$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
  52------------------------------------------------
  53
  54The initial clone may be time-consuming for a large project, but you
  55will only need to clone once.
  56
  57The clone command creates a new directory named after the project
  58("git" or "linux-2.6" in the examples above).  After you cd into this
  59directory, you will see that it contains a copy of the project files,
  60together with a special top-level directory named ".git", which
  61contains all the information about the history of the project.
  62
  63In most of the following, examples will be taken from one of the two
  64repositories above.
  65
  66[[how-to-check-out]]
  67How to check out a different version of a project
  68-------------------------------------------------
  69
  70Git is best thought of as a tool for storing the history of a
  71collection of files.  It stores the history as a compressed
  72collection of interrelated snapshots (versions) of the project's
  73contents.
  74
  75A single git repository may contain multiple branches.  It keeps track
  76of them by keeping a list of <<def_head,heads>> which reference the
  77latest version on each branch; the gitlink:git-branch[1] command shows
  78you the list of branch heads:
  79
  80------------------------------------------------
  81$ git branch
  82* master
  83------------------------------------------------
  84
  85A freshly cloned repository contains a single branch head, by default
  86named "master", with the working directory initialized to the state of
  87the project referred to by that branch head.
  88
  89Most projects also use <<def_tag,tags>>.  Tags, like heads, are
  90references into the project's history, and can be listed using the
  91gitlink:git-tag[1] command:
  92
  93------------------------------------------------
  94$ git tag -l
  95v2.6.11
  96v2.6.11-tree
  97v2.6.12
  98v2.6.12-rc2
  99v2.6.12-rc3
 100v2.6.12-rc4
 101v2.6.12-rc5
 102v2.6.12-rc6
 103v2.6.13
 104...
 105------------------------------------------------
 106
 107Tags are expected to always point at the same version of a project,
 108while heads are expected to advance as development progresses.
 109
 110Create a new branch head pointing to one of these versions and check it
 111out using gitlink:git-checkout[1]:
 112
 113------------------------------------------------
 114$ git checkout -b new v2.6.13
 115------------------------------------------------
 116
 117The working directory then reflects the contents that the project had
 118when it was tagged v2.6.13, and gitlink:git-branch[1] shows two
 119branches, with an asterisk marking the currently checked-out branch:
 120
 121------------------------------------------------
 122$ git branch
 123  master
 124* new
 125------------------------------------------------
 126
 127If you decide that you'd rather see version 2.6.17, you can modify
 128the current branch to point at v2.6.17 instead, with
 129
 130------------------------------------------------
 131$ git reset --hard v2.6.17
 132------------------------------------------------
 133
 134Note that if the current branch head was your only reference to a
 135particular point in history, then resetting that branch may leave you
 136with no way to find the history it used to point to; so use this command
 137carefully.
 138
 139[[understanding-commits]]
 140Understanding History: Commits
 141------------------------------
 142
 143Every change in the history of a project is represented by a commit.
 144The gitlink:git-show[1] command shows the most recent commit on the
 145current branch:
 146
 147------------------------------------------------
 148$ git show
 149commit 2b5f6dcce5bf94b9b119e9ed8d537098ec61c3d2
 150Author: Jamal Hadi Salim <hadi@cyberus.ca>
 151Date:   Sat Dec 2 22:22:25 2006 -0800
 152
 153    [XFRM]: Fix aevent structuring to be more complete.
 154    
 155    aevents can not uniquely identify an SA. We break the ABI with this
 156    patch, but consensus is that since it is not yet utilized by any
 157    (known) application then it is fine (better do it now than later).
 158    
 159    Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca>
 160    Signed-off-by: David S. Miller <davem@davemloft.net>
 161
 162diff --git a/Documentation/networking/xfrm_sync.txt b/Documentation/networking/xfrm_sync.txt
 163index 8be626f..d7aac9d 100644
 164--- a/Documentation/networking/xfrm_sync.txt
 165+++ b/Documentation/networking/xfrm_sync.txt
 166@@ -47,10 +47,13 @@ aevent_id structure looks like:
 167 
 168    struct xfrm_aevent_id {
 169              struct xfrm_usersa_id           sa_id;
 170+             xfrm_address_t                  saddr;
 171              __u32                           flags;
 172+             __u32                           reqid;
 173    };
 174...
 175------------------------------------------------
 176
 177As you can see, a commit shows who made the latest change, what they
 178did, and why.
 179
 180Every commit has a 40-hexdigit id, sometimes called the "object name" or the
 181"SHA1 id", shown on the first line of the "git show" output.  You can usually
 182refer to a commit by a shorter name, such as a tag or a branch name, but this
 183longer name can also be useful.  Most importantly, it is a globally unique
 184name for this commit: so if you tell somebody else the object name (for
 185example in email), then you are guaranteed that name will refer to the same
 186commit in their repository that it does in yours (assuming their repository
 187has that commit at all).  Since the object name is computed as a hash over the
 188contents of the commit, you are guaranteed that the commit can never change
 189without its name also changing.
 190
 191In fact, in <<git-internals>> we shall see that everything stored in git
 192history, including file data and directory contents, is stored in an object
 193with a name that is a hash of its contents.
 194
 195[[understanding-reachability]]
 196Understanding history: commits, parents, and reachability
 197~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 198
 199Every commit (except the very first commit in a project) also has a
 200parent commit which shows what happened before this commit.
 201Following the chain of parents will eventually take you back to the
 202beginning of the project.
 203
 204However, the commits do not form a simple list; git allows lines of
 205development to diverge and then reconverge, and the point where two
 206lines of development reconverge is called a "merge".  The commit
 207representing a merge can therefore have more than one parent, with
 208each parent representing the most recent commit on one of the lines
 209of development leading to that point.
 210
 211The best way to see how this works is using the gitlink:gitk[1]
 212command; running gitk now on a git repository and looking for merge
 213commits will help understand how the git organizes history.
 214
 215In the following, we say that commit X is "reachable" from commit Y
 216if commit X is an ancestor of commit Y.  Equivalently, you could say
 217that Y is a descendent of X, or that there is a chain of parents
 218leading from commit Y to commit X.
 219
 220[[history-diagrams]]
 221Understanding history: History diagrams
 222~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 223
 224We will sometimes represent git history using diagrams like the one
 225below.  Commits are shown as "o", and the links between them with
 226lines drawn with - / and \.  Time goes left to right:
 227
 228
 229................................................
 230         o--o--o <-- Branch A
 231        /
 232 o--o--o <-- master
 233        \
 234         o--o--o <-- Branch B
 235................................................
 236
 237If we need to talk about a particular commit, the character "o" may
 238be replaced with another letter or number.
 239
 240[[what-is-a-branch]]
 241Understanding history: What is a branch?
 242~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 243
 244When we need to be precise, we will use the word "branch" to mean a line
 245of development, and "branch head" (or just "head") to mean a reference
 246to the most recent commit on a branch.  In the example above, the branch
 247head named "A" is a pointer to one particular commit, but we refer to
 248the line of three commits leading up to that point as all being part of
 249"branch A".
 250
 251However, when no confusion will result, we often just use the term
 252"branch" both for branches and for branch heads.
 253
 254[[manipulating-branches]]
 255Manipulating branches
 256---------------------
 257
 258Creating, deleting, and modifying branches is quick and easy; here's
 259a summary of the commands:
 260
 261git branch::
 262        list all branches
 263git branch <branch>::
 264        create a new branch named <branch>, referencing the same
 265        point in history as the current branch
 266git branch <branch> <start-point>::
 267        create a new branch named <branch>, referencing
 268        <start-point>, which may be specified any way you like,
 269        including using a branch name or a tag name
 270git branch -d <branch>::
 271        delete the branch <branch>; if the branch you are deleting
 272        points to a commit which is not reachable from the current
 273        branch, this command will fail with a warning.
 274git branch -D <branch>::
 275        even if the branch points to a commit not reachable
 276        from the current branch, you may know that that commit
 277        is still reachable from some other branch or tag.  In that
 278        case it is safe to use this command to force git to delete
 279        the branch.
 280git checkout <branch>::
 281        make the current branch <branch>, updating the working
 282        directory to reflect the version referenced by <branch>
 283git checkout -b <new> <start-point>::
 284        create a new branch <new> referencing <start-point>, and
 285        check it out.
 286
 287The special symbol "HEAD" can always be used to refer to the current
 288branch.  In fact, git uses a file named "HEAD" in the .git directory to
 289remember which branch is current:
 290
 291------------------------------------------------
 292$ cat .git/HEAD
 293ref: refs/heads/master
 294------------------------------------------------
 295
 296[[detached-head]]
 297Examining an old version without creating a new branch
 298------------------------------------------------------
 299
 300The git-checkout command normally expects a branch head, but will also
 301accept an arbitrary commit; for example, you can check out the commit
 302referenced by a tag:
 303
 304------------------------------------------------
 305$ git checkout v2.6.17
 306Note: moving to "v2.6.17" which isn't a local branch
 307If you want to create a new branch from this checkout, you may do so
 308(now or later) by using -b with the checkout command again. Example:
 309  git checkout -b <new_branch_name>
 310HEAD is now at 427abfa... Linux v2.6.17
 311------------------------------------------------
 312
 313The HEAD then refers to the SHA1 of the commit instead of to a branch,
 314and git branch shows that you are no longer on a branch:
 315
 316------------------------------------------------
 317$ cat .git/HEAD
 318427abfa28afedffadfca9dd8b067eb6d36bac53f
 319$ git branch
 320* (no branch)
 321  master
 322------------------------------------------------
 323
 324In this case we say that the HEAD is "detached".
 325
 326This is an easy way to check out a particular version without having to
 327make up a name for the new branch.   You can still create a new branch
 328(or tag) for this version later if you decide to.
 329
 330[[examining-remote-branches]]
 331Examining branches from a remote repository
 332-------------------------------------------
 333
 334The "master" branch that was created at the time you cloned is a copy
 335of the HEAD in the repository that you cloned from.  That repository
 336may also have had other branches, though, and your local repository
 337keeps branches which track each of those remote branches, which you
 338can view using the "-r" option to gitlink:git-branch[1]:
 339
 340------------------------------------------------
 341$ git branch -r
 342  origin/HEAD
 343  origin/html
 344  origin/maint
 345  origin/man
 346  origin/master
 347  origin/next
 348  origin/pu
 349  origin/todo
 350------------------------------------------------
 351
 352You cannot check out these remote-tracking branches, but you can
 353examine them on a branch of your own, just as you would a tag:
 354
 355------------------------------------------------
 356$ git checkout -b my-todo-copy origin/todo
 357------------------------------------------------
 358
 359Note that the name "origin" is just the name that git uses by default
 360to refer to the repository that you cloned from.
 361
 362[[how-git-stores-references]]
 363Naming branches, tags, and other references
 364-------------------------------------------
 365
 366Branches, remote-tracking branches, and tags are all references to
 367commits.  All references are named with a slash-separated path name
 368starting with "refs"; the names we've been using so far are actually
 369shorthand:
 370
 371        - The branch "test" is short for "refs/heads/test".
 372        - The tag "v2.6.18" is short for "refs/tags/v2.6.18".
 373        - "origin/master" is short for "refs/remotes/origin/master".
 374
 375The full name is occasionally useful if, for example, there ever
 376exists a tag and a branch with the same name.
 377
 378As another useful shortcut, the "HEAD" of a repository can be referred
 379to just using the name of that repository.  So, for example, "origin"
 380is usually a shortcut for the HEAD branch in the repository "origin".
 381
 382For the complete list of paths which git checks for references, and
 383the order it uses to decide which to choose when there are multiple
 384references with the same shorthand name, see the "SPECIFYING
 385REVISIONS" section of gitlink:git-rev-parse[1].
 386
 387[[Updating-a-repository-with-git-fetch]]
 388Updating a repository with git fetch
 389------------------------------------
 390
 391Eventually the developer cloned from will do additional work in her
 392repository, creating new commits and advancing the branches to point
 393at the new commits.
 394
 395The command "git fetch", with no arguments, will update all of the
 396remote-tracking branches to the latest version found in her
 397repository.  It will not touch any of your own branches--not even the
 398"master" branch that was created for you on clone.
 399
 400[[fetching-branches]]
 401Fetching branches from other repositories
 402-----------------------------------------
 403
 404You can also track branches from repositories other than the one you
 405cloned from, using gitlink:git-remote[1]:
 406
 407-------------------------------------------------
 408$ git remote add linux-nfs git://linux-nfs.org/pub/nfs-2.6.git
 409$ git fetch linux-nfs
 410* refs/remotes/linux-nfs/master: storing branch 'master' ...
 411  commit: bf81b46
 412-------------------------------------------------
 413
 414New remote-tracking branches will be stored under the shorthand name
 415that you gave "git remote add", in this case linux-nfs:
 416
 417-------------------------------------------------
 418$ git branch -r
 419linux-nfs/master
 420origin/master
 421-------------------------------------------------
 422
 423If you run "git fetch <remote>" later, the tracking branches for the
 424named <remote> will be updated.
 425
 426If you examine the file .git/config, you will see that git has added
 427a new stanza:
 428
 429-------------------------------------------------
 430$ cat .git/config
 431...
 432[remote "linux-nfs"]
 433        url = git://linux-nfs.org/pub/nfs-2.6.git
 434        fetch = +refs/heads/*:refs/remotes/linux-nfs/*
 435...
 436-------------------------------------------------
 437
 438This is what causes git to track the remote's branches; you may modify
 439or delete these configuration options by editing .git/config with a
 440text editor.  (See the "CONFIGURATION FILE" section of
 441gitlink:git-config[1] for details.)
 442
 443[[exploring-git-history]]
 444Exploring git history
 445=====================
 446
 447Git is best thought of as a tool for storing the history of a
 448collection of files.  It does this by storing compressed snapshots of
 449the contents of a file heirarchy, together with "commits" which show
 450the relationships between these snapshots.
 451
 452Git provides extremely flexible and fast tools for exploring the
 453history of a project.
 454
 455We start with one specialized tool that is useful for finding the
 456commit that introduced a bug into a project.
 457
 458[[using-bisect]]
 459How to use bisect to find a regression
 460--------------------------------------
 461
 462Suppose version 2.6.18 of your project worked, but the version at
 463"master" crashes.  Sometimes the best way to find the cause of such a
 464regression is to perform a brute-force search through the project's
 465history to find the particular commit that caused the problem.  The
 466gitlink:git-bisect[1] command can help you do this:
 467
 468-------------------------------------------------
 469$ git bisect start
 470$ git bisect good v2.6.18
 471$ git bisect bad master
 472Bisecting: 3537 revisions left to test after this
 473[65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6]
 474-------------------------------------------------
 475
 476If you run "git branch" at this point, you'll see that git has
 477temporarily moved you to a new branch named "bisect".  This branch
 478points to a commit (with commit id 65934...) that is reachable from
 479v2.6.19 but not from v2.6.18.  Compile and test it, and see whether
 480it crashes.  Assume it does crash.  Then:
 481
 482-------------------------------------------------
 483$ git bisect bad
 484Bisecting: 1769 revisions left to test after this
 485[7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings
 486-------------------------------------------------
 487
 488checks out an older version.  Continue like this, telling git at each
 489stage whether the version it gives you is good or bad, and notice
 490that the number of revisions left to test is cut approximately in
 491half each time.
 492
 493After about 13 tests (in this case), it will output the commit id of
 494the guilty commit.  You can then examine the commit with
 495gitlink:git-show[1], find out who wrote it, and mail them your bug
 496report with the commit id.  Finally, run
 497
 498-------------------------------------------------
 499$ git bisect reset
 500-------------------------------------------------
 501
 502to return you to the branch you were on before and delete the
 503temporary "bisect" branch.
 504
 505Note that the version which git-bisect checks out for you at each
 506point is just a suggestion, and you're free to try a different
 507version if you think it would be a good idea.  For example,
 508occasionally you may land on a commit that broke something unrelated;
 509run
 510
 511-------------------------------------------------
 512$ git bisect visualize
 513-------------------------------------------------
 514
 515which will run gitk and label the commit it chose with a marker that
 516says "bisect".  Chose a safe-looking commit nearby, note its commit
 517id, and check it out with:
 518
 519-------------------------------------------------
 520$ git reset --hard fb47ddb2db...
 521-------------------------------------------------
 522
 523then test, run "bisect good" or "bisect bad" as appropriate, and
 524continue.
 525
 526[[naming-commits]]
 527Naming commits
 528--------------
 529
 530We have seen several ways of naming commits already:
 531
 532        - 40-hexdigit object name
 533        - branch name: refers to the commit at the head of the given
 534          branch
 535        - tag name: refers to the commit pointed to by the given tag
 536          (we've seen branches and tags are special cases of
 537          <<how-git-stores-references,references>>).
 538        - HEAD: refers to the head of the current branch
 539
 540There are many more; see the "SPECIFYING REVISIONS" section of the
 541gitlink:git-rev-parse[1] man page for the complete list of ways to
 542name revisions.  Some examples:
 543
 544-------------------------------------------------
 545$ git show fb47ddb2 # the first few characters of the object name
 546                    # are usually enough to specify it uniquely
 547$ git show HEAD^    # the parent of the HEAD commit
 548$ git show HEAD^^   # the grandparent
 549$ git show HEAD~4   # the great-great-grandparent
 550-------------------------------------------------
 551
 552Recall that merge commits may have more than one parent; by default,
 553^ and ~ follow the first parent listed in the commit, but you can
 554also choose:
 555
 556-------------------------------------------------
 557$ git show HEAD^1   # show the first parent of HEAD
 558$ git show HEAD^2   # show the second parent of HEAD
 559-------------------------------------------------
 560
 561In addition to HEAD, there are several other special names for
 562commits:
 563
 564Merges (to be discussed later), as well as operations such as
 565git-reset, which change the currently checked-out commit, generally
 566set ORIG_HEAD to the value HEAD had before the current operation.
 567
 568The git-fetch operation always stores the head of the last fetched
 569branch in FETCH_HEAD.  For example, if you run git fetch without
 570specifying a local branch as the target of the operation
 571
 572-------------------------------------------------
 573$ git fetch git://example.com/proj.git theirbranch
 574-------------------------------------------------
 575
 576the fetched commits will still be available from FETCH_HEAD.
 577
 578When we discuss merges we'll also see the special name MERGE_HEAD,
 579which refers to the other branch that we're merging in to the current
 580branch.
 581
 582The gitlink:git-rev-parse[1] command is a low-level command that is
 583occasionally useful for translating some name for a commit to the object
 584name for that commit:
 585
 586-------------------------------------------------
 587$ git rev-parse origin
 588e05db0fd4f31dde7005f075a84f96b360d05984b
 589-------------------------------------------------
 590
 591[[creating-tags]]
 592Creating tags
 593-------------
 594
 595We can also create a tag to refer to a particular commit; after
 596running
 597
 598-------------------------------------------------
 599$ git tag stable-1 1b2e1d63ff
 600-------------------------------------------------
 601
 602You can use stable-1 to refer to the commit 1b2e1d63ff.
 603
 604This creates a "lightweight" tag.  If you would also like to include a
 605comment with the tag, and possibly sign it cryptographically, then you
 606should create a tag object instead; see the gitlink:git-tag[1] man page
 607for details.
 608
 609[[browsing-revisions]]
 610Browsing revisions
 611------------------
 612
 613The gitlink:git-log[1] command can show lists of commits.  On its
 614own, it shows all commits reachable from the parent commit; but you
 615can also make more specific requests:
 616
 617-------------------------------------------------
 618$ git log v2.5..        # commits since (not reachable from) v2.5
 619$ git log test..master  # commits reachable from master but not test
 620$ git log master..test  # ...reachable from test but not master
 621$ git log master...test # ...reachable from either test or master,
 622                        #    but not both
 623$ git log --since="2 weeks ago" # commits from the last 2 weeks
 624$ git log Makefile      # commits which modify Makefile
 625$ git log fs/           # ... which modify any file under fs/
 626$ git log -S'foo()'     # commits which add or remove any file data
 627                        # matching the string 'foo()'
 628-------------------------------------------------
 629
 630And of course you can combine all of these; the following finds
 631commits since v2.5 which touch the Makefile or any file under fs:
 632
 633-------------------------------------------------
 634$ git log v2.5.. Makefile fs/
 635-------------------------------------------------
 636
 637You can also ask git log to show patches:
 638
 639-------------------------------------------------
 640$ git log -p
 641-------------------------------------------------
 642
 643See the "--pretty" option in the gitlink:git-log[1] man page for more
 644display options.
 645
 646Note that git log starts with the most recent commit and works
 647backwards through the parents; however, since git history can contain
 648multiple independent lines of development, the particular order that
 649commits are listed in may be somewhat arbitrary.
 650
 651[[generating-diffs]]
 652Generating diffs
 653----------------
 654
 655You can generate diffs between any two versions using
 656gitlink:git-diff[1]:
 657
 658-------------------------------------------------
 659$ git diff master..test
 660-------------------------------------------------
 661
 662Sometimes what you want instead is a set of patches:
 663
 664-------------------------------------------------
 665$ git format-patch master..test
 666-------------------------------------------------
 667
 668will generate a file with a patch for each commit reachable from test
 669but not from master.  Note that if master also has commits which are
 670not reachable from test, then the combined result of these patches
 671will not be the same as the diff produced by the git-diff example.
 672
 673[[viewing-old-file-versions]]
 674Viewing old file versions
 675-------------------------
 676
 677You can always view an old version of a file by just checking out the
 678correct revision first.  But sometimes it is more convenient to be
 679able to view an old version of a single file without checking
 680anything out; this command does that:
 681
 682-------------------------------------------------
 683$ git show v2.5:fs/locks.c
 684-------------------------------------------------
 685
 686Before the colon may be anything that names a commit, and after it
 687may be any path to a file tracked by git.
 688
 689[[history-examples]]
 690Examples
 691--------
 692
 693[[checking-for-equal-branches]]
 694Check whether two branches point at the same history
 695~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 696
 697Suppose you want to check whether two branches point at the same point
 698in history.
 699
 700-------------------------------------------------
 701$ git diff origin..master
 702-------------------------------------------------
 703
 704will tell you whether the contents of the project are the same at the
 705two branches; in theory, however, it's possible that the same project
 706contents could have been arrived at by two different historical
 707routes.  You could compare the object names:
 708
 709-------------------------------------------------
 710$ git rev-list origin
 711e05db0fd4f31dde7005f075a84f96b360d05984b
 712$ git rev-list master
 713e05db0fd4f31dde7005f075a84f96b360d05984b
 714-------------------------------------------------
 715
 716Or you could recall that the ... operator selects all commits
 717contained reachable from either one reference or the other but not
 718both: so
 719
 720-------------------------------------------------
 721$ git log origin...master
 722-------------------------------------------------
 723
 724will return no commits when the two branches are equal.
 725
 726[[finding-tagged-descendants]]
 727Find first tagged version including a given fix
 728~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 729
 730Suppose you know that the commit e05db0fd fixed a certain problem.
 731You'd like to find the earliest tagged release that contains that
 732fix.
 733
 734Of course, there may be more than one answer--if the history branched
 735after commit e05db0fd, then there could be multiple "earliest" tagged
 736releases.
 737
 738You could just visually inspect the commits since e05db0fd:
 739
 740-------------------------------------------------
 741$ gitk e05db0fd..
 742-------------------------------------------------
 743
 744Or you can use gitlink:git-name-rev[1], which will give the commit a
 745name based on any tag it finds pointing to one of the commit's
 746descendants:
 747
 748-------------------------------------------------
 749$ git name-rev --tags e05db0fd
 750e05db0fd tags/v1.5.0-rc1^0~23
 751-------------------------------------------------
 752
 753The gitlink:git-describe[1] command does the opposite, naming the
 754revision using a tag on which the given commit is based:
 755
 756-------------------------------------------------
 757$ git describe e05db0fd
 758v1.5.0-rc0-260-ge05db0f
 759-------------------------------------------------
 760
 761but that may sometimes help you guess which tags might come after the
 762given commit.
 763
 764If you just want to verify whether a given tagged version contains a
 765given commit, you could use gitlink:git-merge-base[1]:
 766
 767-------------------------------------------------
 768$ git merge-base e05db0fd v1.5.0-rc1
 769e05db0fd4f31dde7005f075a84f96b360d05984b
 770-------------------------------------------------
 771
 772The merge-base command finds a common ancestor of the given commits,
 773and always returns one or the other in the case where one is a
 774descendant of the other; so the above output shows that e05db0fd
 775actually is an ancestor of v1.5.0-rc1.
 776
 777Alternatively, note that
 778
 779-------------------------------------------------
 780$ git log v1.5.0-rc1..e05db0fd
 781-------------------------------------------------
 782
 783will produce empty output if and only if v1.5.0-rc1 includes e05db0fd,
 784because it outputs only commits that are not reachable from v1.5.0-rc1.
 785
 786As yet another alternative, the gitlink:git-show-branch[1] command lists
 787the commits reachable from its arguments with a display on the left-hand
 788side that indicates which arguments that commit is reachable from.  So,
 789you can run something like
 790
 791-------------------------------------------------
 792$ git show-branch e05db0fd v1.5.0-rc0 v1.5.0-rc1 v1.5.0-rc2
 793! [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
 794available
 795 ! [v1.5.0-rc0] GIT v1.5.0 preview
 796  ! [v1.5.0-rc1] GIT v1.5.0-rc1
 797   ! [v1.5.0-rc2] GIT v1.5.0-rc2
 798...
 799-------------------------------------------------
 800
 801then search for a line that looks like
 802
 803-------------------------------------------------
 804+ ++ [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
 805available
 806-------------------------------------------------
 807
 808Which shows that e05db0fd is reachable from itself, from v1.5.0-rc1, and
 809from v1.5.0-rc2, but not from v1.5.0-rc0.
 810
 811
 812[[Developing-with-git]]
 813Developing with git
 814===================
 815
 816[[telling-git-your-name]]
 817Telling git your name
 818---------------------
 819
 820Before creating any commits, you should introduce yourself to git.  The
 821easiest way to do so is to make sure the following lines appear in a
 822file named .gitconfig in your home directory:
 823
 824------------------------------------------------
 825[user]
 826        name = Your Name Comes Here
 827        email = you@yourdomain.example.com
 828------------------------------------------------
 829
 830(See the "CONFIGURATION FILE" section of gitlink:git-config[1] for
 831details on the configuration file.)
 832
 833
 834[[creating-a-new-repository]]
 835Creating a new repository
 836-------------------------
 837
 838Creating a new repository from scratch is very easy:
 839
 840-------------------------------------------------
 841$ mkdir project
 842$ cd project
 843$ git init
 844-------------------------------------------------
 845
 846If you have some initial content (say, a tarball):
 847
 848-------------------------------------------------
 849$ tar -xzvf project.tar.gz
 850$ cd project
 851$ git init
 852$ git add . # include everything below ./ in the first commit:
 853$ git commit
 854-------------------------------------------------
 855
 856[[how-to-make-a-commit]]
 857How to make a commit
 858--------------------
 859
 860Creating a new commit takes three steps:
 861
 862        1. Making some changes to the working directory using your
 863           favorite editor.
 864        2. Telling git about your changes.
 865        3. Creating the commit using the content you told git about
 866           in step 2.
 867
 868In practice, you can interleave and repeat steps 1 and 2 as many
 869times as you want: in order to keep track of what you want committed
 870at step 3, git maintains a snapshot of the tree's contents in a
 871special staging area called "the index."
 872
 873At the beginning, the content of the index will be identical to
 874that of the HEAD.  The command "git diff --cached", which shows
 875the difference between the HEAD and the index, should therefore
 876produce no output at that point.
 877
 878Modifying the index is easy:
 879
 880To update the index with the new contents of a modified file, use
 881
 882-------------------------------------------------
 883$ git add path/to/file
 884-------------------------------------------------
 885
 886To add the contents of a new file to the index, use
 887
 888-------------------------------------------------
 889$ git add path/to/file
 890-------------------------------------------------
 891
 892To remove a file from the index and from the working tree,
 893
 894-------------------------------------------------
 895$ git rm path/to/file
 896-------------------------------------------------
 897
 898After each step you can verify that
 899
 900-------------------------------------------------
 901$ git diff --cached
 902-------------------------------------------------
 903
 904always shows the difference between the HEAD and the index file--this
 905is what you'd commit if you created the commit now--and that
 906
 907-------------------------------------------------
 908$ git diff
 909-------------------------------------------------
 910
 911shows the difference between the working tree and the index file.
 912
 913Note that "git add" always adds just the current contents of a file
 914to the index; further changes to the same file will be ignored unless
 915you run git-add on the file again.
 916
 917When you're ready, just run
 918
 919-------------------------------------------------
 920$ git commit
 921-------------------------------------------------
 922
 923and git will prompt you for a commit message and then create the new
 924commit.  Check to make sure it looks like what you expected with
 925
 926-------------------------------------------------
 927$ git show
 928-------------------------------------------------
 929
 930As a special shortcut,
 931                
 932-------------------------------------------------
 933$ git commit -a
 934-------------------------------------------------
 935
 936will update the index with any files that you've modified or removed
 937and create a commit, all in one step.
 938
 939A number of commands are useful for keeping track of what you're
 940about to commit:
 941
 942-------------------------------------------------
 943$ git diff --cached # difference between HEAD and the index; what
 944                    # would be commited if you ran "commit" now.
 945$ git diff          # difference between the index file and your
 946                    # working directory; changes that would not
 947                    # be included if you ran "commit" now.
 948$ git diff HEAD     # difference between HEAD and working tree; what
 949                    # would be committed if you ran "commit -a" now.
 950$ git status        # a brief per-file summary of the above.
 951-------------------------------------------------
 952
 953[[creating-good-commit-messages]]
 954Creating good commit messages
 955-----------------------------
 956
 957Though not required, it's a good idea to begin the commit message
 958with a single short (less than 50 character) line summarizing the
 959change, followed by a blank line and then a more thorough
 960description.  Tools that turn commits into email, for example, use
 961the first line on the Subject line and the rest of the commit in the
 962body.
 963
 964[[how-to-merge]]
 965How to merge
 966------------
 967
 968You can rejoin two diverging branches of development using
 969gitlink:git-merge[1]:
 970
 971-------------------------------------------------
 972$ git merge branchname
 973-------------------------------------------------
 974
 975merges the development in the branch "branchname" into the current
 976branch.  If there are conflicts--for example, if the same file is
 977modified in two different ways in the remote branch and the local
 978branch--then you are warned; the output may look something like this:
 979
 980-------------------------------------------------
 981$ git merge next
 982 100% (4/4) done
 983Auto-merged file.txt
 984CONFLICT (content): Merge conflict in file.txt
 985Automatic merge failed; fix conflicts and then commit the result.
 986-------------------------------------------------
 987
 988Conflict markers are left in the problematic files, and after
 989you resolve the conflicts manually, you can update the index
 990with the contents and run git commit, as you normally would when
 991creating a new file.
 992
 993If you examine the resulting commit using gitk, you will see that it
 994has two parents, one pointing to the top of the current branch, and
 995one to the top of the other branch.
 996
 997[[resolving-a-merge]]
 998Resolving a merge
 999-----------------
1000
1001When a merge isn't resolved automatically, git leaves the index and
1002the working tree in a special state that gives you all the
1003information you need to help resolve the merge.
1004
1005Files with conflicts are marked specially in the index, so until you
1006resolve the problem and update the index, gitlink:git-commit[1] will
1007fail:
1008
1009-------------------------------------------------
1010$ git commit
1011file.txt: needs merge
1012-------------------------------------------------
1013
1014Also, gitlink:git-status[1] will list those files as "unmerged", and the
1015files with conflicts will have conflict markers added, like this:
1016
1017-------------------------------------------------
1018<<<<<<< HEAD:file.txt
1019Hello world
1020=======
1021Goodbye
1022>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1023-------------------------------------------------
1024
1025All you need to do is edit the files to resolve the conflicts, and then
1026
1027-------------------------------------------------
1028$ git add file.txt
1029$ git commit
1030-------------------------------------------------
1031
1032Note that the commit message will already be filled in for you with
1033some information about the merge.  Normally you can just use this
1034default message unchanged, but you may add additional commentary of
1035your own if desired.
1036
1037The above is all you need to know to resolve a simple merge.  But git
1038also provides more information to help resolve conflicts:
1039
1040[[conflict-resolution]]
1041Getting conflict-resolution help during a merge
1042~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1043
1044All of the changes that git was able to merge automatically are
1045already added to the index file, so gitlink:git-diff[1] shows only
1046the conflicts.  It uses an unusual syntax:
1047
1048-------------------------------------------------
1049$ git diff
1050diff --cc file.txt
1051index 802992c,2b60207..0000000
1052--- a/file.txt
1053+++ b/file.txt
1054@@@ -1,1 -1,1 +1,5 @@@
1055++<<<<<<< HEAD:file.txt
1056 +Hello world
1057++=======
1058+ Goodbye
1059++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1060-------------------------------------------------
1061
1062Recall that the commit which will be commited after we resolve this
1063conflict will have two parents instead of the usual one: one parent
1064will be HEAD, the tip of the current branch; the other will be the
1065tip of the other branch, which is stored temporarily in MERGE_HEAD.
1066
1067During the merge, the index holds three versions of each file.  Each of
1068these three "file stages" represents a different version of the file:
1069
1070-------------------------------------------------
1071$ git show :1:file.txt  # the file in a common ancestor of both branches
1072$ git show :2:file.txt  # the version from HEAD, but including any
1073                        # nonconflicting changes from MERGE_HEAD
1074$ git show :3:file.txt  # the version from MERGE_HEAD, but including any
1075                        # nonconflicting changes from HEAD.
1076-------------------------------------------------
1077
1078Since the stage 2 and stage 3 versions have already been updated with
1079nonconflicting changes, the only remaining differences between them are
1080the important ones; thus gitlink:git-diff[1] can use the information in
1081the index to show only those conflicts.
1082
1083The diff above shows the differences between the working-tree version of
1084file.txt and the stage 2 and stage 3 versions.  So instead of preceding
1085each line by a single "+" or "-", it now uses two columns: the first
1086column is used for differences between the first parent and the working
1087directory copy, and the second for differences between the second parent
1088and the working directory copy.  (See the "COMBINED DIFF FORMAT" section
1089of gitlink:git-diff-files[1] for a details of the format.)
1090
1091After resolving the conflict in the obvious way (but before updating the
1092index), the diff will look like:
1093
1094-------------------------------------------------
1095$ git diff
1096diff --cc file.txt
1097index 802992c,2b60207..0000000
1098--- a/file.txt
1099+++ b/file.txt
1100@@@ -1,1 -1,1 +1,1 @@@
1101- Hello world
1102 -Goodbye
1103++Goodbye world
1104-------------------------------------------------
1105
1106This shows that our resolved version deleted "Hello world" from the
1107first parent, deleted "Goodbye" from the second parent, and added
1108"Goodbye world", which was previously absent from both.
1109
1110Some special diff options allow diffing the working directory against
1111any of these stages:
1112
1113-------------------------------------------------
1114$ git diff -1 file.txt          # diff against stage 1
1115$ git diff --base file.txt      # same as the above
1116$ git diff -2 file.txt          # diff against stage 2
1117$ git diff --ours file.txt      # same as the above
1118$ git diff -3 file.txt          # diff against stage 3
1119$ git diff --theirs file.txt    # same as the above.
1120-------------------------------------------------
1121
1122The gitlink:git-log[1] and gitk[1] commands also provide special help
1123for merges:
1124
1125-------------------------------------------------
1126$ git log --merge
1127$ gitk --merge
1128-------------------------------------------------
1129
1130These will display all commits which exist only on HEAD or on
1131MERGE_HEAD, and which touch an unmerged file.
1132
1133You may also use gitlink:git-mergetool[1], which lets you merge the
1134unmerged files using external tools such as emacs or kdiff3.
1135
1136Each time you resolve the conflicts in a file and update the index:
1137
1138-------------------------------------------------
1139$ git add file.txt
1140-------------------------------------------------
1141
1142the different stages of that file will be "collapsed", after which
1143git-diff will (by default) no longer show diffs for that file.
1144
1145[[undoing-a-merge]]
1146Undoing a merge
1147---------------
1148
1149If you get stuck and decide to just give up and throw the whole mess
1150away, you can always return to the pre-merge state with
1151
1152-------------------------------------------------
1153$ git reset --hard HEAD
1154-------------------------------------------------
1155
1156Or, if you've already commited the merge that you want to throw away,
1157
1158-------------------------------------------------
1159$ git reset --hard ORIG_HEAD
1160-------------------------------------------------
1161
1162However, this last command can be dangerous in some cases--never
1163throw away a commit you have already committed if that commit may
1164itself have been merged into another branch, as doing so may confuse
1165further merges.
1166
1167[[fast-forwards]]
1168Fast-forward merges
1169-------------------
1170
1171There is one special case not mentioned above, which is treated
1172differently.  Normally, a merge results in a merge commit, with two
1173parents, one pointing at each of the two lines of development that
1174were merged.
1175
1176However, if the current branch is a descendant of the other--so every
1177commit present in the one is already contained in the other--then git
1178just performs a "fast forward"; the head of the current branch is moved
1179forward to point at the head of the merged-in branch, without any new
1180commits being created.
1181
1182[[fixing-mistakes]]
1183Fixing mistakes
1184---------------
1185
1186If you've messed up the working tree, but haven't yet committed your
1187mistake, you can return the entire working tree to the last committed
1188state with
1189
1190-------------------------------------------------
1191$ git reset --hard HEAD
1192-------------------------------------------------
1193
1194If you make a commit that you later wish you hadn't, there are two
1195fundamentally different ways to fix the problem:
1196
1197        1. You can create a new commit that undoes whatever was done
1198        by the previous commit.  This is the correct thing if your
1199        mistake has already been made public.
1200
1201        2. You can go back and modify the old commit.  You should
1202        never do this if you have already made the history public;
1203        git does not normally expect the "history" of a project to
1204        change, and cannot correctly perform repeated merges from
1205        a branch that has had its history changed.
1206
1207[[reverting-a-commit]]
1208Fixing a mistake with a new commit
1209~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1210
1211Creating a new commit that reverts an earlier change is very easy;
1212just pass the gitlink:git-revert[1] command a reference to the bad
1213commit; for example, to revert the most recent commit:
1214
1215-------------------------------------------------
1216$ git revert HEAD
1217-------------------------------------------------
1218
1219This will create a new commit which undoes the change in HEAD.  You
1220will be given a chance to edit the commit message for the new commit.
1221
1222You can also revert an earlier change, for example, the next-to-last:
1223
1224-------------------------------------------------
1225$ git revert HEAD^
1226-------------------------------------------------
1227
1228In this case git will attempt to undo the old change while leaving
1229intact any changes made since then.  If more recent changes overlap
1230with the changes to be reverted, then you will be asked to fix
1231conflicts manually, just as in the case of <<resolving-a-merge,
1232resolving a merge>>.
1233
1234[[fixing-a-mistake-by-editing-history]]
1235Fixing a mistake by editing history
1236~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1237
1238If the problematic commit is the most recent commit, and you have not
1239yet made that commit public, then you may just
1240<<undoing-a-merge,destroy it using git-reset>>.
1241
1242Alternatively, you
1243can edit the working directory and update the index to fix your
1244mistake, just as if you were going to <<how-to-make-a-commit,create a
1245new commit>>, then run
1246
1247-------------------------------------------------
1248$ git commit --amend
1249-------------------------------------------------
1250
1251which will replace the old commit by a new commit incorporating your
1252changes, giving you a chance to edit the old commit message first.
1253
1254Again, you should never do this to a commit that may already have
1255been merged into another branch; use gitlink:git-revert[1] instead in
1256that case.
1257
1258It is also possible to edit commits further back in the history, but
1259this is an advanced topic to be left for
1260<<cleaning-up-history,another chapter>>.
1261
1262[[checkout-of-path]]
1263Checking out an old version of a file
1264~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1265
1266In the process of undoing a previous bad change, you may find it
1267useful to check out an older version of a particular file using
1268gitlink:git-checkout[1].  We've used git checkout before to switch
1269branches, but it has quite different behavior if it is given a path
1270name: the command
1271
1272-------------------------------------------------
1273$ git checkout HEAD^ path/to/file
1274-------------------------------------------------
1275
1276replaces path/to/file by the contents it had in the commit HEAD^, and
1277also updates the index to match.  It does not change branches.
1278
1279If you just want to look at an old version of the file, without
1280modifying the working directory, you can do that with
1281gitlink:git-show[1]:
1282
1283-------------------------------------------------
1284$ git show HEAD^:path/to/file
1285-------------------------------------------------
1286
1287which will display the given version of the file.
1288
1289[[ensuring-good-performance]]
1290Ensuring good performance
1291-------------------------
1292
1293On large repositories, git depends on compression to keep the history
1294information from taking up to much space on disk or in memory.
1295
1296This compression is not performed automatically.  Therefore you
1297should occasionally run gitlink:git-gc[1]:
1298
1299-------------------------------------------------
1300$ git gc
1301-------------------------------------------------
1302
1303to recompress the archive.  This can be very time-consuming, so
1304you may prefer to run git-gc when you are not doing other work.
1305
1306
1307[[ensuring-reliability]]
1308Ensuring reliability
1309--------------------
1310
1311[[checking-for-corruption]]
1312Checking the repository for corruption
1313~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1314
1315The gitlink:git-fsck[1] command runs a number of self-consistency checks
1316on the repository, and reports on any problems.  This may take some
1317time.  The most common warning by far is about "dangling" objects:
1318
1319-------------------------------------------------
1320$ git fsck
1321dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1322dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1323dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1324dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb
1325dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f
1326dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e
1327dangling tree d50bb86186bf27b681d25af89d3b5b68382e4085
1328dangling tree b24c2473f1fd3d91352a624795be026d64c8841f
1329...
1330-------------------------------------------------
1331
1332Dangling objects are not a problem.  At worst they may take up a little
1333extra disk space.  They can sometimes provide a last-resort method of
1334recovery lost work--see <<dangling-objects>> for details.  However, if
1335you want, you may remove them with gitlink:git-prune[1] or the --prune
1336option to gitlink:git-gc[1]:
1337
1338-------------------------------------------------
1339$ git gc --prune
1340-------------------------------------------------
1341
1342This may be time-consuming.  Unlike most other git operations (including
1343git-gc when run without any options), it is not safe to prune while
1344other git operations are in progress in the same repository.
1345
1346[[recovering-lost-changes]]
1347Recovering lost changes
1348~~~~~~~~~~~~~~~~~~~~~~~
1349
1350[[reflogs]]
1351Reflogs
1352^^^^^^^
1353
1354Say you modify a branch with gitlink:git-reset[1] --hard, and then
1355realize that the branch was the only reference you had to that point in
1356history.
1357
1358Fortunately, git also keeps a log, called a "reflog", of all the
1359previous values of each branch.  So in this case you can still find the
1360old history using, for example, 
1361
1362-------------------------------------------------
1363$ git log master@{1}
1364-------------------------------------------------
1365
1366This lists the commits reachable from the previous version of the head.
1367This syntax can be used to with any git command that accepts a commit,
1368not just with git log.  Some other examples:
1369
1370-------------------------------------------------
1371$ git show master@{2}           # See where the branch pointed 2,
1372$ git show master@{3}           # 3, ... changes ago.
1373$ gitk master@{yesterday}       # See where it pointed yesterday,
1374$ gitk master@{"1 week ago"}    # ... or last week
1375$ git log --walk-reflogs master # show reflog entries for master
1376-------------------------------------------------
1377
1378A separate reflog is kept for the HEAD, so
1379
1380-------------------------------------------------
1381$ git show HEAD@{"1 week ago"}
1382-------------------------------------------------
1383
1384will show what HEAD pointed to one week ago, not what the current branch
1385pointed to one week ago.  This allows you to see the history of what
1386you've checked out.
1387
1388The reflogs are kept by default for 30 days, after which they may be
1389pruned.  See gitlink:git-reflog[1] and gitlink:git-gc[1] to learn
1390how to control this pruning, and see the "SPECIFYING REVISIONS"
1391section of gitlink:git-rev-parse[1] for details.
1392
1393Note that the reflog history is very different from normal git history.
1394While normal history is shared by every repository that works on the
1395same project, the reflog history is not shared: it tells you only about
1396how the branches in your local repository have changed over time.
1397
1398[[dangling-object-recovery]]
1399Examining dangling objects
1400^^^^^^^^^^^^^^^^^^^^^^^^^^
1401
1402In some situations the reflog may not be able to save you.  For example,
1403suppose you delete a branch, then realize you need the history it
1404contained.  The reflog is also deleted; however, if you have not yet
1405pruned the repository, then you may still be able to find the lost
1406commits in the dangling objects that git-fsck reports.  See
1407<<dangling-objects>> for the details.
1408
1409-------------------------------------------------
1410$ git fsck
1411dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1412dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1413dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1414...
1415-------------------------------------------------
1416
1417You can examine
1418one of those dangling commits with, for example,
1419
1420------------------------------------------------
1421$ gitk 7281251ddd --not --all
1422------------------------------------------------
1423
1424which does what it sounds like: it says that you want to see the commit
1425history that is described by the dangling commit(s), but not the
1426history that is described by all your existing branches and tags.  Thus
1427you get exactly the history reachable from that commit that is lost.
1428(And notice that it might not be just one commit: we only report the
1429"tip of the line" as being dangling, but there might be a whole deep
1430and complex commit history that was dropped.)
1431
1432If you decide you want the history back, you can always create a new
1433reference pointing to it, for example, a new branch:
1434
1435------------------------------------------------
1436$ git branch recovered-branch 7281251ddd 
1437------------------------------------------------
1438
1439Other types of dangling objects (blobs and trees) are also possible, and
1440dangling objects can arise in other situations.
1441
1442
1443[[sharing-development]]
1444Sharing development with others
1445===============================
1446
1447[[getting-updates-with-git-pull]]
1448Getting updates with git pull
1449-----------------------------
1450
1451After you clone a repository and make a few changes of your own, you
1452may wish to check the original repository for updates and merge them
1453into your own work.
1454
1455We have already seen <<Updating-a-repository-with-git-fetch,how to
1456keep remote tracking branches up to date>> with gitlink:git-fetch[1],
1457and how to merge two branches.  So you can merge in changes from the
1458original repository's master branch with:
1459
1460-------------------------------------------------
1461$ git fetch
1462$ git merge origin/master
1463-------------------------------------------------
1464
1465However, the gitlink:git-pull[1] command provides a way to do this in
1466one step:
1467
1468-------------------------------------------------
1469$ git pull origin master
1470-------------------------------------------------
1471
1472In fact, "origin" is normally the default repository to pull from,
1473and the default branch is normally the HEAD of the remote repository,
1474so often you can accomplish the above with just
1475
1476-------------------------------------------------
1477$ git pull
1478-------------------------------------------------
1479
1480See the descriptions of the branch.<name>.remote and branch.<name>.merge
1481options in gitlink:git-config[1] to learn how to control these defaults
1482depending on the current branch.  Also note that the --track option to
1483gitlink:git-branch[1] and gitlink:git-checkout[1] can be used to
1484automatically set the default remote branch to pull from at the time
1485that a branch is created:
1486
1487-------------------------------------------------
1488$ git checkout --track -b origin/maint maint
1489-------------------------------------------------
1490
1491In addition to saving you keystrokes, "git pull" also helps you by
1492producing a default commit message documenting the branch and
1493repository that you pulled from.
1494
1495(But note that no such commit will be created in the case of a
1496<<fast-forwards,fast forward>>; instead, your branch will just be
1497updated to point to the latest commit from the upstream branch.)
1498
1499The git-pull command can also be given "." as the "remote" repository,
1500in which case it just merges in a branch from the current repository; so
1501the commands
1502
1503-------------------------------------------------
1504$ git pull . branch
1505$ git merge branch
1506-------------------------------------------------
1507
1508are roughly equivalent.  The former is actually very commonly used.
1509
1510[[submitting-patches]]
1511Submitting patches to a project
1512-------------------------------
1513
1514If you just have a few changes, the simplest way to submit them may
1515just be to send them as patches in email:
1516
1517First, use gitlink:git-format-patch[1]; for example:
1518
1519-------------------------------------------------
1520$ git format-patch origin
1521-------------------------------------------------
1522
1523will produce a numbered series of files in the current directory, one
1524for each patch in the current branch but not in origin/HEAD.
1525
1526You can then import these into your mail client and send them by
1527hand.  However, if you have a lot to send at once, you may prefer to
1528use the gitlink:git-send-email[1] script to automate the process.
1529Consult the mailing list for your project first to determine how they
1530prefer such patches be handled.
1531
1532[[importing-patches]]
1533Importing patches to a project
1534------------------------------
1535
1536Git also provides a tool called gitlink:git-am[1] (am stands for
1537"apply mailbox"), for importing such an emailed series of patches.
1538Just save all of the patch-containing messages, in order, into a
1539single mailbox file, say "patches.mbox", then run
1540
1541-------------------------------------------------
1542$ git am -3 patches.mbox
1543-------------------------------------------------
1544
1545Git will apply each patch in order; if any conflicts are found, it
1546will stop, and you can fix the conflicts as described in
1547"<<resolving-a-merge,Resolving a merge>>".  (The "-3" option tells
1548git to perform a merge; if you would prefer it just to abort and
1549leave your tree and index untouched, you may omit that option.)
1550
1551Once the index is updated with the results of the conflict
1552resolution, instead of creating a new commit, just run
1553
1554-------------------------------------------------
1555$ git am --resolved
1556-------------------------------------------------
1557
1558and git will create the commit for you and continue applying the
1559remaining patches from the mailbox.
1560
1561The final result will be a series of commits, one for each patch in
1562the original mailbox, with authorship and commit log message each
1563taken from the message containing each patch.
1564
1565[[setting-up-a-public-repository]]
1566Setting up a public repository
1567------------------------------
1568
1569Another way to submit changes to a project is to simply tell the
1570maintainer of that project to pull from your repository, exactly as
1571you did in the section "<<getting-updates-with-git-pull, Getting
1572updates with git pull>>".
1573
1574If you and maintainer both have accounts on the same machine, then
1575then you can just pull changes from each other's repositories
1576directly; note that all of the commands (gitlink:git-clone[1],
1577git-fetch[1], git-pull[1], etc.) that accept a URL as an argument
1578will also accept a local directory name; so, for example, you can
1579use
1580
1581-------------------------------------------------
1582$ git clone /path/to/repository
1583$ git pull /path/to/other/repository
1584-------------------------------------------------
1585
1586If this sort of setup is inconvenient or impossible, another (more
1587common) option is to set up a public repository on a public server.
1588This also allows you to cleanly separate private work in progress
1589from publicly visible work.
1590
1591You will continue to do your day-to-day work in your personal
1592repository, but periodically "push" changes from your personal
1593repository into your public repository, allowing other developers to
1594pull from that repository.  So the flow of changes, in a situation
1595where there is one other developer with a public repository, looks
1596like this:
1597
1598                        you push
1599  your personal repo ------------------> your public repo
1600        ^                                     |
1601        |                                     |
1602        | you pull                            | they pull
1603        |                                     |
1604        |                                     |
1605        |               they push             V
1606  their public repo <------------------- their repo
1607
1608Now, assume your personal repository is in the directory ~/proj.  We
1609first create a new clone of the repository:
1610
1611-------------------------------------------------
1612$ git clone --bare ~/proj proj.git
1613-------------------------------------------------
1614
1615The resulting directory proj.git contains a "bare" git repository--it is
1616just the contents of the ".git" directory, without a checked-out copy of
1617a working directory.
1618
1619Next, copy proj.git to the server where you plan to host the
1620public repository.  You can use scp, rsync, or whatever is most
1621convenient.
1622
1623If somebody else maintains the public server, they may already have
1624set up a git service for you, and you may skip to the section
1625"<<pushing-changes-to-a-public-repository,Pushing changes to a public
1626repository>>", below.
1627
1628Otherwise, the following sections explain how to export your newly
1629created public repository:
1630
1631[[exporting-via-http]]
1632Exporting a git repository via http
1633-----------------------------------
1634
1635The git protocol gives better performance and reliability, but on a
1636host with a web server set up, http exports may be simpler to set up.
1637
1638All you need to do is place the newly created bare git repository in
1639a directory that is exported by the web server, and make some
1640adjustments to give web clients some extra information they need:
1641
1642-------------------------------------------------
1643$ mv proj.git /home/you/public_html/proj.git
1644$ cd proj.git
1645$ git --bare update-server-info
1646$ chmod a+x hooks/post-update
1647-------------------------------------------------
1648
1649(For an explanation of the last two lines, see
1650gitlink:git-update-server-info[1], and the documentation
1651link:hooks.txt[Hooks used by git].)
1652
1653Advertise the url of proj.git.  Anybody else should then be able to
1654clone or pull from that url, for example with a commandline like:
1655
1656-------------------------------------------------
1657$ git clone http://yourserver.com/~you/proj.git
1658-------------------------------------------------
1659
1660(See also
1661link:howto/setup-git-server-over-http.txt[setup-git-server-over-http]
1662for a slightly more sophisticated setup using WebDAV which also
1663allows pushing over http.)
1664
1665[[exporting-via-git]]
1666Exporting a git repository via the git protocol
1667-----------------------------------------------
1668
1669This is the preferred method.
1670
1671For now, we refer you to the gitlink:git-daemon[1] man page for
1672instructions.  (See especially the examples section.)
1673
1674[[pushing-changes-to-a-public-repository]]
1675Pushing changes to a public repository
1676--------------------------------------
1677
1678Note that the two techniques outline above (exporting via
1679<<exporting-via-http,http>> or <<exporting-via-git,git>>) allow other
1680maintainers to fetch your latest changes, but they do not allow write
1681access, which you will need to update the public repository with the
1682latest changes created in your private repository.
1683
1684The simplest way to do this is using gitlink:git-push[1] and ssh; to
1685update the remote branch named "master" with the latest state of your
1686branch named "master", run
1687
1688-------------------------------------------------
1689$ git push ssh://yourserver.com/~you/proj.git master:master
1690-------------------------------------------------
1691
1692or just
1693
1694-------------------------------------------------
1695$ git push ssh://yourserver.com/~you/proj.git master
1696-------------------------------------------------
1697
1698As with git-fetch, git-push will complain if this does not result in
1699a <<fast-forwards,fast forward>>.  Normally this is a sign of
1700something wrong.  However, if you are sure you know what you're
1701doing, you may force git-push to perform the update anyway by
1702proceeding the branch name by a plus sign:
1703
1704-------------------------------------------------
1705$ git push ssh://yourserver.com/~you/proj.git +master
1706-------------------------------------------------
1707
1708As with git-fetch, you may also set up configuration options to
1709save typing; so, for example, after
1710
1711-------------------------------------------------
1712$ cat >>.git/config <<EOF
1713[remote "public-repo"]
1714        url = ssh://yourserver.com/~you/proj.git
1715EOF
1716-------------------------------------------------
1717
1718you should be able to perform the above push with just
1719
1720-------------------------------------------------
1721$ git push public-repo master
1722-------------------------------------------------
1723
1724See the explanations of the remote.<name>.url, branch.<name>.remote,
1725and remote.<name>.push options in gitlink:git-config[1] for
1726details.
1727
1728[[setting-up-a-shared-repository]]
1729Setting up a shared repository
1730------------------------------
1731
1732Another way to collaborate is by using a model similar to that
1733commonly used in CVS, where several developers with special rights
1734all push to and pull from a single shared repository.  See
1735link:cvs-migration.txt[git for CVS users] for instructions on how to
1736set this up.
1737
1738[[setting-up-gitweb]]
1739Allow web browsing of a repository
1740----------------------------------
1741
1742The gitweb cgi script provides users an easy way to browse your
1743project's files and history without having to install git; see the file
1744gitweb/INSTALL in the git source tree for instructions on setting it up.
1745
1746[[sharing-development-examples]]
1747Examples
1748--------
1749
1750TODO: topic branches, typical roles as in everyday.txt, ?
1751
1752
1753[[cleaning-up-history]]
1754Rewriting history and maintaining patch series
1755==============================================
1756
1757Normally commits are only added to a project, never taken away or
1758replaced.  Git is designed with this assumption, and violating it will
1759cause git's merge machinery (for example) to do the wrong thing.
1760
1761However, there is a situation in which it can be useful to violate this
1762assumption.
1763
1764[[patch-series]]
1765Creating the perfect patch series
1766---------------------------------
1767
1768Suppose you are a contributor to a large project, and you want to add a
1769complicated feature, and to present it to the other developers in a way
1770that makes it easy for them to read your changes, verify that they are
1771correct, and understand why you made each change.
1772
1773If you present all of your changes as a single patch (or commit), they
1774may find that it is too much to digest all at once.
1775
1776If you present them with the entire history of your work, complete with
1777mistakes, corrections, and dead ends, they may be overwhelmed.
1778
1779So the ideal is usually to produce a series of patches such that:
1780
1781        1. Each patch can be applied in order.
1782
1783        2. Each patch includes a single logical change, together with a
1784           message explaining the change.
1785
1786        3. No patch introduces a regression: after applying any initial
1787           part of the series, the resulting project still compiles and
1788           works, and has no bugs that it didn't have before.
1789
1790        4. The complete series produces the same end result as your own
1791           (probably much messier!) development process did.
1792
1793We will introduce some tools that can help you do this, explain how to
1794use them, and then explain some of the problems that can arise because
1795you are rewriting history.
1796
1797[[using-git-rebase]]
1798Keeping a patch series up to date using git-rebase
1799--------------------------------------------------
1800
1801Suppose that you create a branch "mywork" on a remote-tracking branch
1802"origin", and create some commits on top of it:
1803
1804-------------------------------------------------
1805$ git checkout -b mywork origin
1806$ vi file.txt
1807$ git commit
1808$ vi otherfile.txt
1809$ git commit
1810...
1811-------------------------------------------------
1812
1813You have performed no merges into mywork, so it is just a simple linear
1814sequence of patches on top of "origin":
1815
1816................................................
1817 o--o--o <-- origin
1818        \
1819         o--o--o <-- mywork
1820................................................
1821
1822Some more interesting work has been done in the upstream project, and
1823"origin" has advanced:
1824
1825................................................
1826 o--o--O--o--o--o <-- origin
1827        \
1828         a--b--c <-- mywork
1829................................................
1830
1831At this point, you could use "pull" to merge your changes back in;
1832the result would create a new merge commit, like this:
1833
1834................................................
1835 o--o--O--o--o--o <-- origin
1836        \        \
1837         a--b--c--m <-- mywork
1838................................................
1839 
1840However, if you prefer to keep the history in mywork a simple series of
1841commits without any merges, you may instead choose to use
1842gitlink:git-rebase[1]:
1843
1844-------------------------------------------------
1845$ git checkout mywork
1846$ git rebase origin
1847-------------------------------------------------
1848
1849This will remove each of your commits from mywork, temporarily saving
1850them as patches (in a directory named ".dotest"), update mywork to
1851point at the latest version of origin, then apply each of the saved
1852patches to the new mywork.  The result will look like:
1853
1854
1855................................................
1856 o--o--O--o--o--o <-- origin
1857                 \
1858                  a'--b'--c' <-- mywork
1859................................................
1860
1861In the process, it may discover conflicts.  In that case it will stop
1862and allow you to fix the conflicts; after fixing conflicts, use "git
1863add" to update the index with those contents, and then, instead of
1864running git-commit, just run
1865
1866-------------------------------------------------
1867$ git rebase --continue
1868-------------------------------------------------
1869
1870and git will continue applying the rest of the patches.
1871
1872At any point you may use the --abort option to abort this process and
1873return mywork to the state it had before you started the rebase:
1874
1875-------------------------------------------------
1876$ git rebase --abort
1877-------------------------------------------------
1878
1879[[modifying-one-commit]]
1880Modifying a single commit
1881-------------------------
1882
1883We saw in <<fixing-a-mistake-by-editing-history>> that you can replace the
1884most recent commit using
1885
1886-------------------------------------------------
1887$ git commit --amend
1888-------------------------------------------------
1889
1890which will replace the old commit by a new commit incorporating your
1891changes, giving you a chance to edit the old commit message first.
1892
1893You can also use a combination of this and gitlink:git-rebase[1] to edit
1894commits further back in your history.  First, tag the problematic commit with
1895
1896-------------------------------------------------
1897$ git tag bad mywork~5
1898-------------------------------------------------
1899
1900(Either gitk or git-log may be useful for finding the commit.)
1901
1902Then check out that commit, edit it, and rebase the rest of the series
1903on top of it (note that we could check out the commit on a temporary
1904branch, but instead we're using a <<detached-head,detached head>>):
1905
1906-------------------------------------------------
1907$ git checkout bad
1908$ # make changes here and update the index
1909$ git commit --amend
1910$ git rebase --onto HEAD bad mywork
1911-------------------------------------------------
1912
1913When you're done, you'll be left with mywork checked out, with the top
1914patches on mywork reapplied on top of your modified commit.  You can
1915then clean up with
1916
1917-------------------------------------------------
1918$ git tag -d bad
1919-------------------------------------------------
1920
1921Note that the immutable nature of git history means that you haven't really
1922"modified" existing commits; instead, you have replaced the old commits with
1923new commits having new object names.
1924
1925[[reordering-patch-series]]
1926Reordering or selecting from a patch series
1927-------------------------------------------
1928
1929Given one existing commit, the gitlink:git-cherry-pick[1] command
1930allows you to apply the change introduced by that commit and create a
1931new commit that records it.  So, for example, if "mywork" points to a
1932series of patches on top of "origin", you might do something like:
1933
1934-------------------------------------------------
1935$ git checkout -b mywork-new origin
1936$ gitk origin..mywork &
1937-------------------------------------------------
1938
1939And browse through the list of patches in the mywork branch using gitk,
1940applying them (possibly in a different order) to mywork-new using
1941cherry-pick, and possibly modifying them as you go using commit
1942--amend.
1943
1944Another technique is to use git-format-patch to create a series of
1945patches, then reset the state to before the patches:
1946
1947-------------------------------------------------
1948$ git format-patch origin
1949$ git reset --hard origin
1950-------------------------------------------------
1951
1952Then modify, reorder, or eliminate patches as preferred before applying
1953them again with gitlink:git-am[1].
1954
1955[[patch-series-tools]]
1956Other tools
1957-----------
1958
1959There are numerous other tools, such as stgit, which exist for the
1960purpose of maintaining a patch series.  These are outside of the scope of
1961this manual.
1962
1963[[problems-with-rewriting-history]]
1964Problems with rewriting history
1965-------------------------------
1966
1967The primary problem with rewriting the history of a branch has to do
1968with merging.  Suppose somebody fetches your branch and merges it into
1969their branch, with a result something like this:
1970
1971................................................
1972 o--o--O--o--o--o <-- origin
1973        \        \
1974         t--t--t--m <-- their branch:
1975................................................
1976
1977Then suppose you modify the last three commits:
1978
1979................................................
1980         o--o--o <-- new head of origin
1981        /
1982 o--o--O--o--o--o <-- old head of origin
1983................................................
1984
1985If we examined all this history together in one repository, it will
1986look like:
1987
1988................................................
1989         o--o--o <-- new head of origin
1990        /
1991 o--o--O--o--o--o <-- old head of origin
1992        \        \
1993         t--t--t--m <-- their branch:
1994................................................
1995
1996Git has no way of knowing that the new head is an updated version of
1997the old head; it treats this situation exactly the same as it would if
1998two developers had independently done the work on the old and new heads
1999in parallel.  At this point, if someone attempts to merge the new head
2000in to their branch, git will attempt to merge together the two (old and
2001new) lines of development, instead of trying to replace the old by the
2002new.  The results are likely to be unexpected.
2003
2004You may still choose to publish branches whose history is rewritten,
2005and it may be useful for others to be able to fetch those branches in
2006order to examine or test them, but they should not attempt to pull such
2007branches into their own work.
2008
2009For true distributed development that supports proper merging,
2010published branches should never be rewritten.
2011
2012[[advanced-branch-management]]
2013Advanced branch management
2014==========================
2015
2016[[fetching-individual-branches]]
2017Fetching individual branches
2018----------------------------
2019
2020Instead of using gitlink:git-remote[1], you can also choose just
2021to update one branch at a time, and to store it locally under an
2022arbitrary name:
2023
2024-------------------------------------------------
2025$ git fetch origin todo:my-todo-work
2026-------------------------------------------------
2027
2028The first argument, "origin", just tells git to fetch from the
2029repository you originally cloned from.  The second argument tells git
2030to fetch the branch named "todo" from the remote repository, and to
2031store it locally under the name refs/heads/my-todo-work.
2032
2033You can also fetch branches from other repositories; so
2034
2035-------------------------------------------------
2036$ git fetch git://example.com/proj.git master:example-master
2037-------------------------------------------------
2038
2039will create a new branch named "example-master" and store in it the
2040branch named "master" from the repository at the given URL.  If you
2041already have a branch named example-master, it will attempt to
2042<<fast-forwards,fast-forward>> to the commit given by example.com's
2043master branch.  In more detail:
2044
2045[[fetch-fast-forwards]]
2046git fetch and fast-forwards
2047---------------------------
2048
2049In the previous example, when updating an existing branch, "git
2050fetch" checks to make sure that the most recent commit on the remote
2051branch is a descendant of the most recent commit on your copy of the
2052branch before updating your copy of the branch to point at the new
2053commit.  Git calls this process a <<fast-forwards,fast forward>>.
2054
2055A fast forward looks something like this:
2056
2057................................................
2058 o--o--o--o <-- old head of the branch
2059           \
2060            o--o--o <-- new head of the branch
2061................................................
2062
2063
2064In some cases it is possible that the new head will *not* actually be
2065a descendant of the old head.  For example, the developer may have
2066realized she made a serious mistake, and decided to backtrack,
2067resulting in a situation like:
2068
2069................................................
2070 o--o--o--o--a--b <-- old head of the branch
2071           \
2072            o--o--o <-- new head of the branch
2073................................................
2074
2075In this case, "git fetch" will fail, and print out a warning.
2076
2077In that case, you can still force git to update to the new head, as
2078described in the following section.  However, note that in the
2079situation above this may mean losing the commits labeled "a" and "b",
2080unless you've already created a reference of your own pointing to
2081them.
2082
2083[[forcing-fetch]]
2084Forcing git fetch to do non-fast-forward updates
2085------------------------------------------------
2086
2087If git fetch fails because the new head of a branch is not a
2088descendant of the old head, you may force the update with:
2089
2090-------------------------------------------------
2091$ git fetch git://example.com/proj.git +master:refs/remotes/example/master
2092-------------------------------------------------
2093
2094Note the addition of the "+" sign.  Alternatively, you can use the "-f"
2095flag to force updates of all the fetched branches, as in:
2096
2097-------------------------------------------------
2098$ git fetch -f origin
2099-------------------------------------------------
2100
2101Be aware that commits that the old version of example/master pointed at
2102may be lost, as we saw in the previous section.
2103
2104[[remote-branch-configuration]]
2105Configuring remote branches
2106---------------------------
2107
2108We saw above that "origin" is just a shortcut to refer to the
2109repository that you originally cloned from.  This information is
2110stored in git configuration variables, which you can see using
2111gitlink:git-config[1]:
2112
2113-------------------------------------------------
2114$ git config -l
2115core.repositoryformatversion=0
2116core.filemode=true
2117core.logallrefupdates=true
2118remote.origin.url=git://git.kernel.org/pub/scm/git/git.git
2119remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
2120branch.master.remote=origin
2121branch.master.merge=refs/heads/master
2122-------------------------------------------------
2123
2124If there are other repositories that you also use frequently, you can
2125create similar configuration options to save typing; for example,
2126after
2127
2128-------------------------------------------------
2129$ git config remote.example.url git://example.com/proj.git
2130-------------------------------------------------
2131
2132then the following two commands will do the same thing:
2133
2134-------------------------------------------------
2135$ git fetch git://example.com/proj.git master:refs/remotes/example/master
2136$ git fetch example master:refs/remotes/example/master
2137-------------------------------------------------
2138
2139Even better, if you add one more option:
2140
2141-------------------------------------------------
2142$ git config remote.example.fetch master:refs/remotes/example/master
2143-------------------------------------------------
2144
2145then the following commands will all do the same thing:
2146
2147-------------------------------------------------
2148$ git fetch git://example.com/proj.git master:refs/remotes/example/master
2149$ git fetch example master:refs/remotes/example/master
2150$ git fetch example
2151-------------------------------------------------
2152
2153You can also add a "+" to force the update each time:
2154
2155-------------------------------------------------
2156$ git config remote.example.fetch +master:ref/remotes/example/master
2157-------------------------------------------------
2158
2159Don't do this unless you're sure you won't mind "git fetch" possibly
2160throwing away commits on mybranch.
2161
2162Also note that all of the above configuration can be performed by
2163directly editing the file .git/config instead of using
2164gitlink:git-config[1].
2165
2166See gitlink:git-config[1] for more details on the configuration
2167options mentioned above.
2168
2169
2170[[git-internals]]
2171Git internals
2172=============
2173
2174Git depends on two fundamental abstractions: the "object database", and
2175the "current directory cache" aka "index".
2176
2177[[the-object-database]]
2178The Object Database
2179-------------------
2180
2181The object database is literally just a content-addressable collection
2182of objects.  All objects are named by their content, which is
2183approximated by the SHA1 hash of the object itself.  Objects may refer
2184to other objects (by referencing their SHA1 hash), and so you can
2185build up a hierarchy of objects.
2186
2187All objects have a statically determined "type" which is
2188determined at object creation time, and which identifies the format of
2189the object (i.e. how it is used, and how it can refer to other
2190objects).  There are currently four different object types: "blob",
2191"tree", "commit", and "tag".
2192
2193A <<def_blob_object,"blob" object>> cannot refer to any other object,
2194and is, as the name implies, a pure storage object containing some
2195user data.  It is used to actually store the file data, i.e. a blob
2196object is associated with some particular version of some file.
2197
2198A <<def_tree_object,"tree" object>> is an object that ties one or more
2199"blob" objects into a directory structure. In addition, a tree object
2200can refer to other tree objects, thus creating a directory hierarchy.
2201
2202A <<def_commit_object,"commit" object>> ties such directory hierarchies
2203together into a <<def_DAG,directed acyclic graph>> of revisions - each
2204"commit" is associated with exactly one tree (the directory hierarchy at
2205the time of the commit). In addition, a "commit" refers to one or more
2206"parent" commit objects that describe the history of how we arrived at
2207that directory hierarchy.
2208
2209As a special case, a commit object with no parents is called the "root"
2210commit, and is the point of an initial project commit.  Each project
2211must have at least one root, and while you can tie several different
2212root objects together into one project by creating a commit object which
2213has two or more separate roots as its ultimate parents, that's probably
2214just going to confuse people.  So aim for the notion of "one root object
2215per project", even if git itself does not enforce that. 
2216
2217A <<def_tag_object,"tag" object>> symbolically identifies and can be
2218used to sign other objects. It contains the identifier and type of
2219another object, a symbolic name (of course!) and, optionally, a
2220signature.
2221
2222Regardless of object type, all objects share the following
2223characteristics: they are all deflated with zlib, and have a header
2224that not only specifies their type, but also provides size information
2225about the data in the object.  It's worth noting that the SHA1 hash
2226that is used to name the object is the hash of the original data
2227plus this header, so `sha1sum` 'file' does not match the object name
2228for 'file'.
2229(Historical note: in the dawn of the age of git the hash
2230was the sha1 of the 'compressed' object.)
2231
2232As a result, the general consistency of an object can always be tested
2233independently of the contents or the type of the object: all objects can
2234be validated by verifying that (a) their hashes match the content of the
2235file and (b) the object successfully inflates to a stream of bytes that
2236forms a sequence of <ascii type without space> + <space> + <ascii decimal
2237size> + <byte\0> + <binary object data>. 
2238
2239The structured objects can further have their structure and
2240connectivity to other objects verified. This is generally done with
2241the `git-fsck` program, which generates a full dependency graph
2242of all objects, and verifies their internal consistency (in addition
2243to just verifying their superficial consistency through the hash).
2244
2245The object types in some more detail:
2246
2247[[blob-object]]
2248Blob Object
2249-----------
2250
2251A "blob" object is nothing but a binary blob of data, and doesn't
2252refer to anything else.  There is no signature or any other
2253verification of the data, so while the object is consistent (it 'is'
2254indexed by its sha1 hash, so the data itself is certainly correct), it
2255has absolutely no other attributes.  No name associations, no
2256permissions.  It is purely a blob of data (i.e. normally "file
2257contents").
2258
2259In particular, since the blob is entirely defined by its data, if two
2260files in a directory tree (or in multiple different versions of the
2261repository) have the same contents, they will share the same blob
2262object. The object is totally independent of its location in the
2263directory tree, and renaming a file does not change the object that
2264file is associated with in any way.
2265
2266A blob is typically created when gitlink:git-update-index[1]
2267is run, and its data can be accessed by gitlink:git-cat-file[1].
2268
2269[[tree-object]]
2270Tree Object
2271-----------
2272
2273The next hierarchical object type is the "tree" object.  A tree object
2274is a list of mode/name/blob data, sorted by name.  Alternatively, the
2275mode data may specify a directory mode, in which case instead of
2276naming a blob, that name is associated with another TREE object.
2277
2278Like the "blob" object, a tree object is uniquely determined by the
2279set contents, and so two separate but identical trees will always
2280share the exact same object. This is true at all levels, i.e. it's
2281true for a "leaf" tree (which does not refer to any other trees, only
2282blobs) as well as for a whole subdirectory.
2283
2284For that reason a "tree" object is just a pure data abstraction: it
2285has no history, no signatures, no verification of validity, except
2286that since the contents are again protected by the hash itself, we can
2287trust that the tree is immutable and its contents never change.
2288
2289So you can trust the contents of a tree to be valid, the same way you
2290can trust the contents of a blob, but you don't know where those
2291contents 'came' from.
2292
2293Side note on trees: since a "tree" object is a sorted list of
2294"filename+content", you can create a diff between two trees without
2295actually having to unpack two trees.  Just ignore all common parts,
2296and your diff will look right.  In other words, you can effectively
2297(and efficiently) tell the difference between any two random trees by
2298O(n) where "n" is the size of the difference, rather than the size of
2299the tree.
2300
2301Side note 2 on trees: since the name of a "blob" depends entirely and
2302exclusively on its contents (i.e. there are no names or permissions
2303involved), you can see trivial renames or permission changes by
2304noticing that the blob stayed the same.  However, renames with data
2305changes need a smarter "diff" implementation.
2306
2307A tree is created with gitlink:git-write-tree[1] and
2308its data can be accessed by gitlink:git-ls-tree[1].
2309Two trees can be compared with gitlink:git-diff-tree[1].
2310
2311[[commit-object]]
2312Commit Object
2313-------------
2314
2315The "commit" object is an object that introduces the notion of
2316history into the picture.  In contrast to the other objects, it
2317doesn't just describe the physical state of a tree, it describes how
2318we got there, and why.
2319
2320A "commit" is defined by the tree-object that it results in, the
2321parent commits (zero, one or more) that led up to that point, and a
2322comment on what happened.  Again, a commit is not trusted per se:
2323the contents are well-defined and "safe" due to the cryptographically
2324strong signatures at all levels, but there is no reason to believe
2325that the tree is "good" or that the merge information makes sense.
2326The parents do not have to actually have any relationship with the
2327result, for example.
2328
2329Note on commits: unlike some SCM's, commits do not contain
2330rename information or file mode change information.  All of that is
2331implicit in the trees involved (the result tree, and the result trees
2332of the parents), and describing that makes no sense in this idiotic
2333file manager.
2334
2335A commit is created with gitlink:git-commit-tree[1] and
2336its data can be accessed by gitlink:git-cat-file[1].
2337
2338[[trust]]
2339Trust
2340-----
2341
2342An aside on the notion of "trust". Trust is really outside the scope
2343of "git", but it's worth noting a few things.  First off, since
2344everything is hashed with SHA1, you 'can' trust that an object is
2345intact and has not been messed with by external sources.  So the name
2346of an object uniquely identifies a known state - just not a state that
2347you may want to trust.
2348
2349Furthermore, since the SHA1 signature of a commit refers to the
2350SHA1 signatures of the tree it is associated with and the signatures
2351of the parent, a single named commit specifies uniquely a whole set
2352of history, with full contents.  You can't later fake any step of the
2353way once you have the name of a commit.
2354
2355So to introduce some real trust in the system, the only thing you need
2356to do is to digitally sign just 'one' special note, which includes the
2357name of a top-level commit.  Your digital signature shows others
2358that you trust that commit, and the immutability of the history of
2359commits tells others that they can trust the whole history.
2360
2361In other words, you can easily validate a whole archive by just
2362sending out a single email that tells the people the name (SHA1 hash)
2363of the top commit, and digitally sign that email using something
2364like GPG/PGP.
2365
2366To assist in this, git also provides the tag object...
2367
2368[[tag-object]]
2369Tag Object
2370----------
2371
2372Git provides the "tag" object to simplify creating, managing and
2373exchanging symbolic and signed tokens.  The "tag" object at its
2374simplest simply symbolically identifies another object by containing
2375the sha1, type and symbolic name.
2376
2377However it can optionally contain additional signature information
2378(which git doesn't care about as long as there's less than 8k of
2379it). This can then be verified externally to git.
2380
2381Note that despite the tag features, "git" itself only handles content
2382integrity; the trust framework (and signature provision and
2383verification) has to come from outside.
2384
2385A tag is created with gitlink:git-mktag[1],
2386its data can be accessed by gitlink:git-cat-file[1],
2387and the signature can be verified by
2388gitlink:git-verify-tag[1].
2389
2390
2391[[the-index]]
2392The "index" aka "Current Directory Cache"
2393-----------------------------------------
2394
2395The index is a simple binary file, which contains an efficient
2396representation of the contents of a virtual directory.  It
2397does so by a simple array that associates a set of names, dates,
2398permissions and content (aka "blob") objects together.  The cache is
2399always kept ordered by name, and names are unique (with a few very
2400specific rules) at any point in time, but the cache has no long-term
2401meaning, and can be partially updated at any time.
2402
2403In particular, the index certainly does not need to be consistent with
2404the current directory contents (in fact, most operations will depend on
2405different ways to make the index 'not' be consistent with the directory
2406hierarchy), but it has three very important attributes:
2407
2408'(a) it can re-generate the full state it caches (not just the
2409directory structure: it contains pointers to the "blob" objects so
2410that it can regenerate the data too)'
2411
2412As a special case, there is a clear and unambiguous one-way mapping
2413from a current directory cache to a "tree object", which can be
2414efficiently created from just the current directory cache without
2415actually looking at any other data.  So a directory cache at any one
2416time uniquely specifies one and only one "tree" object (but has
2417additional data to make it easy to match up that tree object with what
2418has happened in the directory)
2419
2420'(b) it has efficient methods for finding inconsistencies between that
2421cached state ("tree object waiting to be instantiated") and the
2422current state.'
2423
2424'(c) it can additionally efficiently represent information about merge
2425conflicts between different tree objects, allowing each pathname to be
2426associated with sufficient information about the trees involved that
2427you can create a three-way merge between them.'
2428
2429Those are the ONLY three things that the directory cache does.  It's a
2430cache, and the normal operation is to re-generate it completely from a
2431known tree object, or update/compare it with a live tree that is being
2432developed.  If you blow the directory cache away entirely, you generally
2433haven't lost any information as long as you have the name of the tree
2434that it described. 
2435
2436At the same time, the index is at the same time also the
2437staging area for creating new trees, and creating a new tree always
2438involves a controlled modification of the index file.  In particular,
2439the index file can have the representation of an intermediate tree that
2440has not yet been instantiated.  So the index can be thought of as a
2441write-back cache, which can contain dirty information that has not yet
2442been written back to the backing store.
2443
2444
2445
2446[[the-workflow]]
2447The Workflow
2448------------
2449
2450Generally, all "git" operations work on the index file. Some operations
2451work *purely* on the index file (showing the current state of the
2452index), but most operations move data to and from the index file. Either
2453from the database or from the working directory. Thus there are four
2454main combinations: 
2455
2456[[working-directory-to-index]]
2457working directory -> index
2458~~~~~~~~~~~~~~~~~~~~~~~~~~
2459
2460You update the index with information from the working directory with
2461the gitlink:git-update-index[1] command.  You
2462generally update the index information by just specifying the filename
2463you want to update, like so:
2464
2465-------------------------------------------------
2466$ git-update-index filename
2467-------------------------------------------------
2468
2469but to avoid common mistakes with filename globbing etc, the command
2470will not normally add totally new entries or remove old entries,
2471i.e. it will normally just update existing cache entries.
2472
2473To tell git that yes, you really do realize that certain files no
2474longer exist, or that new files should be added, you
2475should use the `--remove` and `--add` flags respectively.
2476
2477NOTE! A `--remove` flag does 'not' mean that subsequent filenames will
2478necessarily be removed: if the files still exist in your directory
2479structure, the index will be updated with their new status, not
2480removed. The only thing `--remove` means is that update-cache will be
2481considering a removed file to be a valid thing, and if the file really
2482does not exist any more, it will update the index accordingly.
2483
2484As a special case, you can also do `git-update-index --refresh`, which
2485will refresh the "stat" information of each index to match the current
2486stat information. It will 'not' update the object status itself, and
2487it will only update the fields that are used to quickly test whether
2488an object still matches its old backing store object.
2489
2490[[index-to-object-database]]
2491index -> object database
2492~~~~~~~~~~~~~~~~~~~~~~~~
2493
2494You write your current index file to a "tree" object with the program
2495
2496-------------------------------------------------
2497$ git-write-tree
2498-------------------------------------------------
2499
2500that doesn't come with any options - it will just write out the
2501current index into the set of tree objects that describe that state,
2502and it will return the name of the resulting top-level tree. You can
2503use that tree to re-generate the index at any time by going in the
2504other direction:
2505
2506[[object-database-to-index]]
2507object database -> index
2508~~~~~~~~~~~~~~~~~~~~~~~~
2509
2510You read a "tree" file from the object database, and use that to
2511populate (and overwrite - don't do this if your index contains any
2512unsaved state that you might want to restore later!) your current
2513index.  Normal operation is just
2514
2515-------------------------------------------------
2516$ git-read-tree <sha1 of tree>
2517-------------------------------------------------
2518
2519and your index file will now be equivalent to the tree that you saved
2520earlier. However, that is only your 'index' file: your working
2521directory contents have not been modified.
2522
2523[[index-to-working-directory]]
2524index -> working directory
2525~~~~~~~~~~~~~~~~~~~~~~~~~~
2526
2527You update your working directory from the index by "checking out"
2528files. This is not a very common operation, since normally you'd just
2529keep your files updated, and rather than write to your working
2530directory, you'd tell the index files about the changes in your
2531working directory (i.e. `git-update-index`).
2532
2533However, if you decide to jump to a new version, or check out somebody
2534else's version, or just restore a previous tree, you'd populate your
2535index file with read-tree, and then you need to check out the result
2536with
2537
2538-------------------------------------------------
2539$ git-checkout-index filename
2540-------------------------------------------------
2541
2542or, if you want to check out all of the index, use `-a`.
2543
2544NOTE! git-checkout-index normally refuses to overwrite old files, so
2545if you have an old version of the tree already checked out, you will
2546need to use the "-f" flag ('before' the "-a" flag or the filename) to
2547'force' the checkout.
2548
2549
2550Finally, there are a few odds and ends which are not purely moving
2551from one representation to the other:
2552
2553[[tying-it-all-together]]
2554Tying it all together
2555~~~~~~~~~~~~~~~~~~~~~
2556
2557To commit a tree you have instantiated with "git-write-tree", you'd
2558create a "commit" object that refers to that tree and the history
2559behind it - most notably the "parent" commits that preceded it in
2560history.
2561
2562Normally a "commit" has one parent: the previous state of the tree
2563before a certain change was made. However, sometimes it can have two
2564or more parent commits, in which case we call it a "merge", due to the
2565fact that such a commit brings together ("merges") two or more
2566previous states represented by other commits.
2567
2568In other words, while a "tree" represents a particular directory state
2569of a working directory, a "commit" represents that state in "time",
2570and explains how we got there.
2571
2572You create a commit object by giving it the tree that describes the
2573state at the time of the commit, and a list of parents:
2574
2575-------------------------------------------------
2576$ git-commit-tree <tree> -p <parent> [-p <parent2> ..]
2577-------------------------------------------------
2578
2579and then giving the reason for the commit on stdin (either through
2580redirection from a pipe or file, or by just typing it at the tty).
2581
2582git-commit-tree will return the name of the object that represents
2583that commit, and you should save it away for later use. Normally,
2584you'd commit a new `HEAD` state, and while git doesn't care where you
2585save the note about that state, in practice we tend to just write the
2586result to the file pointed at by `.git/HEAD`, so that we can always see
2587what the last committed state was.
2588
2589Here is an ASCII art by Jon Loeliger that illustrates how
2590various pieces fit together.
2591
2592------------
2593
2594                     commit-tree
2595                      commit obj
2596                       +----+
2597                       |    |
2598                       |    |
2599                       V    V
2600                    +-----------+
2601                    | Object DB |
2602                    |  Backing  |
2603                    |   Store   |
2604                    +-----------+
2605                       ^
2606           write-tree  |     |
2607             tree obj  |     |
2608                       |     |  read-tree
2609                       |     |  tree obj
2610                             V
2611                    +-----------+
2612                    |   Index   |
2613                    |  "cache"  |
2614                    +-----------+
2615         update-index  ^
2616             blob obj  |     |
2617                       |     |
2618    checkout-index -u  |     |  checkout-index
2619             stat      |     |  blob obj
2620                             V
2621                    +-----------+
2622                    |  Working  |
2623                    | Directory |
2624                    +-----------+
2625
2626------------
2627
2628
2629[[examining-the-data]]
2630Examining the data
2631------------------
2632
2633You can examine the data represented in the object database and the
2634index with various helper tools. For every object, you can use
2635gitlink:git-cat-file[1] to examine details about the
2636object:
2637
2638-------------------------------------------------
2639$ git-cat-file -t <objectname>
2640-------------------------------------------------
2641
2642shows the type of the object, and once you have the type (which is
2643usually implicit in where you find the object), you can use
2644
2645-------------------------------------------------
2646$ git-cat-file blob|tree|commit|tag <objectname>
2647-------------------------------------------------
2648
2649to show its contents. NOTE! Trees have binary content, and as a result
2650there is a special helper for showing that content, called
2651`git-ls-tree`, which turns the binary content into a more easily
2652readable form.
2653
2654It's especially instructive to look at "commit" objects, since those
2655tend to be small and fairly self-explanatory. In particular, if you
2656follow the convention of having the top commit name in `.git/HEAD`,
2657you can do
2658
2659-------------------------------------------------
2660$ git-cat-file commit HEAD
2661-------------------------------------------------
2662
2663to see what the top commit was.
2664
2665[[merging-multiple-trees]]
2666Merging multiple trees
2667----------------------
2668
2669Git helps you do a three-way merge, which you can expand to n-way by
2670repeating the merge procedure arbitrary times until you finally
2671"commit" the state.  The normal situation is that you'd only do one
2672three-way merge (two parents), and commit it, but if you like to, you
2673can do multiple parents in one go.
2674
2675To do a three-way merge, you need the two sets of "commit" objects
2676that you want to merge, use those to find the closest common parent (a
2677third "commit" object), and then use those commit objects to find the
2678state of the directory ("tree" object) at these points.
2679
2680To get the "base" for the merge, you first look up the common parent
2681of two commits with
2682
2683-------------------------------------------------
2684$ git-merge-base <commit1> <commit2>
2685-------------------------------------------------
2686
2687which will return you the commit they are both based on.  You should
2688now look up the "tree" objects of those commits, which you can easily
2689do with (for example)
2690
2691-------------------------------------------------
2692$ git-cat-file commit <commitname> | head -1
2693-------------------------------------------------
2694
2695since the tree object information is always the first line in a commit
2696object.
2697
2698Once you know the three trees you are going to merge (the one "original"
2699tree, aka the common tree, and the two "result" trees, aka the branches
2700you want to merge), you do a "merge" read into the index. This will
2701complain if it has to throw away your old index contents, so you should
2702make sure that you've committed those - in fact you would normally
2703always do a merge against your last commit (which should thus match what
2704you have in your current index anyway).
2705
2706To do the merge, do
2707
2708-------------------------------------------------
2709$ git-read-tree -m -u <origtree> <yourtree> <targettree>
2710-------------------------------------------------
2711
2712which will do all trivial merge operations for you directly in the
2713index file, and you can just write the result out with
2714`git-write-tree`.
2715
2716
2717[[merging-multiple-trees-2]]
2718Merging multiple trees, continued
2719---------------------------------
2720
2721Sadly, many merges aren't trivial. If there are files that have
2722been added.moved or removed, or if both branches have modified the
2723same file, you will be left with an index tree that contains "merge
2724entries" in it. Such an index tree can 'NOT' be written out to a tree
2725object, and you will have to resolve any such merge clashes using
2726other tools before you can write out the result.
2727
2728You can examine such index state with `git-ls-files --unmerged`
2729command.  An example:
2730
2731------------------------------------------------
2732$ git-read-tree -m $orig HEAD $target
2733$ git-ls-files --unmerged
2734100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1       hello.c
2735100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2       hello.c
2736100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello.c
2737------------------------------------------------
2738
2739Each line of the `git-ls-files --unmerged` output begins with
2740the blob mode bits, blob SHA1, 'stage number', and the
2741filename.  The 'stage number' is git's way to say which tree it
2742came from: stage 1 corresponds to `$orig` tree, stage 2 `HEAD`
2743tree, and stage3 `$target` tree.
2744
2745Earlier we said that trivial merges are done inside
2746`git-read-tree -m`.  For example, if the file did not change
2747from `$orig` to `HEAD` nor `$target`, or if the file changed
2748from `$orig` to `HEAD` and `$orig` to `$target` the same way,
2749obviously the final outcome is what is in `HEAD`.  What the
2750above example shows is that file `hello.c` was changed from
2751`$orig` to `HEAD` and `$orig` to `$target` in a different way.
2752You could resolve this by running your favorite 3-way merge
2753program, e.g.  `diff3`, `merge`, or git's own merge-file, on
2754the blob objects from these three stages yourself, like this:
2755
2756------------------------------------------------
2757$ git-cat-file blob 263414f... >hello.c~1
2758$ git-cat-file blob 06fa6a2... >hello.c~2
2759$ git-cat-file blob cc44c73... >hello.c~3
2760$ git merge-file hello.c~2 hello.c~1 hello.c~3
2761------------------------------------------------
2762
2763This would leave the merge result in `hello.c~2` file, along
2764with conflict markers if there are conflicts.  After verifying
2765the merge result makes sense, you can tell git what the final
2766merge result for this file is by:
2767
2768-------------------------------------------------
2769$ mv -f hello.c~2 hello.c
2770$ git-update-index hello.c
2771-------------------------------------------------
2772
2773When a path is in unmerged state, running `git-update-index` for
2774that path tells git to mark the path resolved.
2775
2776The above is the description of a git merge at the lowest level,
2777to help you understand what conceptually happens under the hood.
2778In practice, nobody, not even git itself, uses three `git-cat-file`
2779for this.  There is `git-merge-index` program that extracts the
2780stages to temporary files and calls a "merge" script on it:
2781
2782-------------------------------------------------
2783$ git-merge-index git-merge-one-file hello.c
2784-------------------------------------------------
2785
2786and that is what higher level `git merge -s resolve` is implemented with.
2787
2788[[pack-files]]
2789How git stores objects efficiently: pack files
2790----------------------------------------------
2791
2792We've seen how git stores each object in a file named after the
2793object's SHA1 hash.
2794
2795Unfortunately this system becomes inefficient once a project has a
2796lot of objects.  Try this on an old project:
2797
2798------------------------------------------------
2799$ git count-objects
28006930 objects, 47620 kilobytes
2801------------------------------------------------
2802
2803The first number is the number of objects which are kept in
2804individual files.  The second is the amount of space taken up by
2805those "loose" objects.
2806
2807You can save space and make git faster by moving these loose objects in
2808to a "pack file", which stores a group of objects in an efficient
2809compressed format; the details of how pack files are formatted can be
2810found in link:technical/pack-format.txt[technical/pack-format.txt].
2811
2812To put the loose objects into a pack, just run git repack:
2813
2814------------------------------------------------
2815$ git repack
2816Generating pack...
2817Done counting 6020 objects.
2818Deltifying 6020 objects.
2819 100% (6020/6020) done
2820Writing 6020 objects.
2821 100% (6020/6020) done
2822Total 6020, written 6020 (delta 4070), reused 0 (delta 0)
2823Pack pack-3e54ad29d5b2e05838c75df582c65257b8d08e1c created.
2824------------------------------------------------
2825
2826You can then run
2827
2828------------------------------------------------
2829$ git prune
2830------------------------------------------------
2831
2832to remove any of the "loose" objects that are now contained in the
2833pack.  This will also remove any unreferenced objects (which may be
2834created when, for example, you use "git reset" to remove a commit).
2835You can verify that the loose objects are gone by looking at the
2836.git/objects directory or by running
2837
2838------------------------------------------------
2839$ git count-objects
28400 objects, 0 kilobytes
2841------------------------------------------------
2842
2843Although the object files are gone, any commands that refer to those
2844objects will work exactly as they did before.
2845
2846The gitlink:git-gc[1] command performs packing, pruning, and more for
2847you, so is normally the only high-level command you need.
2848
2849[[dangling-objects]]
2850Dangling objects
2851----------------
2852
2853The gitlink:git-fsck[1] command will sometimes complain about dangling
2854objects.  They are not a problem.
2855
2856The most common cause of dangling objects is that you've rebased a
2857branch, or you have pulled from somebody else who rebased a branch--see
2858<<cleaning-up-history>>.  In that case, the old head of the original
2859branch still exists, as does everything it pointed to. The branch
2860pointer itself just doesn't, since you replaced it with another one.
2861
2862There are also other situations that cause dangling objects. For
2863example, a "dangling blob" may arise because you did a "git add" of a
2864file, but then, before you actually committed it and made it part of the
2865bigger picture, you changed something else in that file and committed
2866that *updated* thing - the old state that you added originally ends up
2867not being pointed to by any commit or tree, so it's now a dangling blob
2868object.
2869
2870Similarly, when the "recursive" merge strategy runs, and finds that
2871there are criss-cross merges and thus more than one merge base (which is
2872fairly unusual, but it does happen), it will generate one temporary
2873midway tree (or possibly even more, if you had lots of criss-crossing
2874merges and more than two merge bases) as a temporary internal merge
2875base, and again, those are real objects, but the end result will not end
2876up pointing to them, so they end up "dangling" in your repository.
2877
2878Generally, dangling objects aren't anything to worry about. They can
2879even be very useful: if you screw something up, the dangling objects can
2880be how you recover your old tree (say, you did a rebase, and realized
2881that you really didn't want to - you can look at what dangling objects
2882you have, and decide to reset your head to some old dangling state).
2883
2884For commits, you can just use:
2885
2886------------------------------------------------
2887$ gitk <dangling-commit-sha-goes-here> --not --all
2888------------------------------------------------
2889
2890This asks for all the history reachable from the given commit but not
2891from any branch, tag, or other reference.  If you decide it's something
2892you want, you can always create a new reference to it, e.g.,
2893
2894------------------------------------------------
2895$ git branch recovered-branch <dangling-commit-sha-goes-here>
2896------------------------------------------------
2897
2898For blobs and trees, you can't do the same, but you can still examine
2899them.  You can just do
2900
2901------------------------------------------------
2902$ git show <dangling-blob/tree-sha-goes-here>
2903------------------------------------------------
2904
2905to show what the contents of the blob were (or, for a tree, basically
2906what the "ls" for that directory was), and that may give you some idea
2907of what the operation was that left that dangling object.
2908
2909Usually, dangling blobs and trees aren't very interesting. They're
2910almost always the result of either being a half-way mergebase (the blob
2911will often even have the conflict markers from a merge in it, if you
2912have had conflicting merges that you fixed up by hand), or simply
2913because you interrupted a "git fetch" with ^C or something like that,
2914leaving _some_ of the new objects in the object database, but just
2915dangling and useless.
2916
2917Anyway, once you are sure that you're not interested in any dangling 
2918state, you can just prune all unreachable objects:
2919
2920------------------------------------------------
2921$ git prune
2922------------------------------------------------
2923
2924and they'll be gone. But you should only run "git prune" on a quiescent
2925repository - it's kind of like doing a filesystem fsck recovery: you
2926don't want to do that while the filesystem is mounted.
2927
2928(The same is true of "git-fsck" itself, btw - but since 
2929git-fsck never actually *changes* the repository, it just reports 
2930on what it found, git-fsck itself is never "dangerous" to run. 
2931Running it while somebody is actually changing the repository can cause 
2932confusing and scary messages, but it won't actually do anything bad. In 
2933contrast, running "git prune" while somebody is actively changing the 
2934repository is a *BAD* idea).
2935
2936[[birdview-on-the-source-code]]
2937A birds-eye view of Git's source code
2938-------------------------------------
2939
2940It is not always easy for new developers to find their way through Git's
2941source code.  This section gives you a little guidance to show where to
2942start.
2943
2944A good place to start is with the contents of the initial commit, with:
2945
2946----------------------------------------------------
2947$ git checkout e83c5163
2948----------------------------------------------------
2949
2950The initial revision lays the foundation for almost everything git has
2951today, but is small enough to read in one sitting.
2952
2953Note that terminology has changed since that revision.  For example, the
2954README in that revision uses the word "changeset" to describe what we
2955now call a <<def_commit_object,commit>>.
2956
2957Also, we do not call it "cache" any more, but "index", however, the
2958file is still called `cache.h`.  Remark: Not much reason to change it now,
2959especially since there is no good single name for it anyway, because it is
2960basically _the_ header file which is included by _all_ of Git's C sources.
2961
2962If you grasp the ideas in that initial commit, you should check out a
2963more recent version and skim `cache.h`, `object.h` and `commit.h`.
2964
2965In the early days, Git (in the tradition of UNIX) was a bunch of programs
2966which were extremely simple, and which you used in scripts, piping the
2967output of one into another. This turned out to be good for initial
2968development, since it was easier to test new things.  However, recently
2969many of these parts have become builtins, and some of the core has been
2970"libified", i.e. put into libgit.a for performance, portability reasons,
2971and to avoid code duplication.
2972
2973By now, you know what the index is (and find the corresponding data
2974structures in `cache.h`), and that there are just a couple of object types
2975(blobs, trees, commits and tags) which inherit their common structure from
2976`struct object`, which is their first member (and thus, you can cast e.g.
2977`(struct object *)commit` to achieve the _same_ as `&commit->object`, i.e.
2978get at the object name and flags).
2979
2980Now is a good point to take a break to let this information sink in.
2981
2982Next step: get familiar with the object naming.  Read <<naming-commits>>.
2983There are quite a few ways to name an object (and not only revisions!).
2984All of these are handled in `sha1_name.c`. Just have a quick look at
2985the function `get_sha1()`. A lot of the special handling is done by
2986functions like `get_sha1_basic()` or the likes.
2987
2988This is just to get you into the groove for the most libified part of Git:
2989the revision walker.
2990
2991Basically, the initial version of `git log` was a shell script:
2992
2993----------------------------------------------------------------
2994$ git-rev-list --pretty $(git-rev-parse --default HEAD "$@") | \
2995        LESS=-S ${PAGER:-less}
2996----------------------------------------------------------------
2997
2998What does this mean?
2999
3000`git-rev-list` is the original version of the revision walker, which
3001_always_ printed a list of revisions to stdout.  It is still functional,
3002and needs to, since most new Git programs start out as scripts using
3003`git-rev-list`.
3004
3005`git-rev-parse` is not as important any more; it was only used to filter out
3006options that were relevant for the different plumbing commands that were
3007called by the script.
3008
3009Most of what `git-rev-list` did is contained in `revision.c` and
3010`revision.h`.  It wraps the options in a struct named `rev_info`, which
3011controls how and what revisions are walked, and more.
3012
3013The original job of `git-rev-parse` is now taken by the function
3014`setup_revisions()`, which parses the revisions and the common command line
3015options for the revision walker. This information is stored in the struct
3016`rev_info` for later consumption. You can do your own command line option
3017parsing after calling `setup_revisions()`. After that, you have to call
3018`prepare_revision_walk()` for initialization, and then you can get the
3019commits one by one with the function `get_revision()`.
3020
3021If you are interested in more details of the revision walking process,
3022just have a look at the first implementation of `cmd_log()`; call
3023`git-show v1.3.0~155^2~4` and scroll down to that function (note that you
3024no longer need to call `setup_pager()` directly).
3025
3026Nowadays, `git log` is a builtin, which means that it is _contained_ in the
3027command `git`.  The source side of a builtin is
3028
3029- a function called `cmd_<bla>`, typically defined in `builtin-<bla>.c`,
3030  and declared in `builtin.h`,
3031
3032- an entry in the `commands[]` array in `git.c`, and
3033
3034- an entry in `BUILTIN_OBJECTS` in the `Makefile`.
3035
3036Sometimes, more than one builtin is contained in one source file.  For
3037example, `cmd_whatchanged()` and `cmd_log()` both reside in `builtin-log.c`,
3038since they share quite a bit of code.  In that case, the commands which are
3039_not_ named like the `.c` file in which they live have to be listed in
3040`BUILT_INS` in the `Makefile`.
3041
3042`git log` looks more complicated in C than it does in the original script,
3043but that allows for a much greater flexibility and performance.
3044
3045Here again it is a good point to take a pause.
3046
3047Lesson three is: study the code.  Really, it is the best way to learn about
3048the organization of Git (after you know the basic concepts).
3049
3050So, think about something which you are interested in, say, "how can I
3051access a blob just knowing the object name of it?".  The first step is to
3052find a Git command with which you can do it.  In this example, it is either
3053`git show` or `git cat-file`.
3054
3055For the sake of clarity, let's stay with `git cat-file`, because it
3056
3057- is plumbing, and
3058
3059- was around even in the initial commit (it literally went only through
3060  some 20 revisions as `cat-file.c`, was renamed to `builtin-cat-file.c`
3061  when made a builtin, and then saw less than 10 versions).
3062
3063So, look into `builtin-cat-file.c`, search for `cmd_cat_file()` and look what
3064it does.
3065
3066------------------------------------------------------------------
3067        git_config(git_default_config);
3068        if (argc != 3)
3069                usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
3070        if (get_sha1(argv[2], sha1))
3071                die("Not a valid object name %s", argv[2]);
3072------------------------------------------------------------------
3073
3074Let's skip over the obvious details; the only really interesting part
3075here is the call to `get_sha1()`.  It tries to interpret `argv[2]` as an
3076object name, and if it refers to an object which is present in the current
3077repository, it writes the resulting SHA-1 into the variable `sha1`.
3078
3079Two things are interesting here:
3080
3081- `get_sha1()` returns 0 on _success_.  This might surprise some new
3082  Git hackers, but there is a long tradition in UNIX to return different
3083  negative numbers in case of different errors -- and 0 on success.
3084
3085- the variable `sha1` in the function signature of `get_sha1()` is `unsigned
3086  char \*`, but is actually expected to be a pointer to `unsigned
3087  char[20]`.  This variable will contain the 160-bit SHA-1 of the given
3088  commit.  Note that whenever a SHA-1 is passed as `unsigned char \*`, it
3089  is the binary representation, as opposed to the ASCII representation in
3090  hex characters, which is passed as `char *`.
3091
3092You will see both of these things throughout the code.
3093
3094Now, for the meat:
3095
3096-----------------------------------------------------------------------------
3097        case 0:
3098                buf = read_object_with_reference(sha1, argv[1], &size, NULL);
3099-----------------------------------------------------------------------------
3100
3101This is how you read a blob (actually, not only a blob, but any type of
3102object).  To know how the function `read_object_with_reference()` actually
3103works, find the source code for it (something like `git grep
3104read_object_with | grep ":[a-z]"` in the git repository), and read
3105the source.
3106
3107To find out how the result can be used, just read on in `cmd_cat_file()`:
3108
3109-----------------------------------
3110        write_or_die(1, buf, size);
3111-----------------------------------
3112
3113Sometimes, you do not know where to look for a feature.  In many such cases,
3114it helps to search through the output of `git log`, and then `git show` the
3115corresponding commit.
3116
3117Example: If you know that there was some test case for `git bundle`, but
3118do not remember where it was (yes, you _could_ `git grep bundle t/`, but that
3119does not illustrate the point!):
3120
3121------------------------
3122$ git log --no-merges t/
3123------------------------
3124
3125In the pager (`less`), just search for "bundle", go a few lines back,
3126and see that it is in commit 18449ab0...  Now just copy this object name,
3127and paste it into the command line
3128
3129-------------------
3130$ git show 18449ab0
3131-------------------
3132
3133Voila.
3134
3135Another example: Find out what to do in order to make some script a
3136builtin:
3137
3138-------------------------------------------------
3139$ git log --no-merges --diff-filter=A builtin-*.c
3140-------------------------------------------------
3141
3142You see, Git is actually the best tool to find out about the source of Git
3143itself!
3144
3145[[glossary]]
3146include::glossary.txt[]
3147
3148[[git-quick-start]]
3149Appendix A: Git Quick Start
3150===========================
3151
3152This is a quick summary of the major commands; the following chapters
3153will explain how these work in more detail.
3154
3155[[quick-creating-a-new-repository]]
3156Creating a new repository
3157-------------------------
3158
3159From a tarball:
3160
3161-----------------------------------------------
3162$ tar xzf project.tar.gz
3163$ cd project
3164$ git init
3165Initialized empty Git repository in .git/
3166$ git add .
3167$ git commit
3168-----------------------------------------------
3169
3170From a remote repository:
3171
3172-----------------------------------------------
3173$ git clone git://example.com/pub/project.git
3174$ cd project
3175-----------------------------------------------
3176
3177[[managing-branches]]
3178Managing branches
3179-----------------
3180
3181-----------------------------------------------
3182$ git branch         # list all local branches in this repo
3183$ git checkout test  # switch working directory to branch "test"
3184$ git branch new     # create branch "new" starting at current HEAD
3185$ git branch -d new  # delete branch "new"
3186-----------------------------------------------
3187
3188Instead of basing new branch on current HEAD (the default), use:
3189
3190-----------------------------------------------
3191$ git branch new test    # branch named "test"
3192$ git branch new v2.6.15 # tag named v2.6.15
3193$ git branch new HEAD^   # commit before the most recent
3194$ git branch new HEAD^^  # commit before that
3195$ git branch new test~10 # ten commits before tip of branch "test"
3196-----------------------------------------------
3197
3198Create and switch to a new branch at the same time:
3199
3200-----------------------------------------------
3201$ git checkout -b new v2.6.15
3202-----------------------------------------------
3203
3204Update and examine branches from the repository you cloned from:
3205
3206-----------------------------------------------
3207$ git fetch             # update
3208$ git branch -r         # list
3209  origin/master
3210  origin/next
3211  ...
3212$ git checkout -b masterwork origin/master
3213-----------------------------------------------
3214
3215Fetch a branch from a different repository, and give it a new
3216name in your repository:
3217
3218-----------------------------------------------
3219$ git fetch git://example.com/project.git theirbranch:mybranch
3220$ git fetch git://example.com/project.git v2.6.15:mybranch
3221-----------------------------------------------
3222
3223Keep a list of repositories you work with regularly:
3224
3225-----------------------------------------------
3226$ git remote add example git://example.com/project.git
3227$ git remote                    # list remote repositories
3228example
3229origin
3230$ git remote show example       # get details
3231* remote example
3232  URL: git://example.com/project.git
3233  Tracked remote branches
3234    master next ...
3235$ git fetch example             # update branches from example
3236$ git branch -r                 # list all remote branches
3237-----------------------------------------------
3238
3239
3240[[exploring-history]]
3241Exploring history
3242-----------------
3243
3244-----------------------------------------------
3245$ gitk                      # visualize and browse history
3246$ git log                   # list all commits
3247$ git log src/              # ...modifying src/
3248$ git log v2.6.15..v2.6.16  # ...in v2.6.16, not in v2.6.15
3249$ git log master..test      # ...in branch test, not in branch master
3250$ git log test..master      # ...in branch master, but not in test
3251$ git log test...master     # ...in one branch, not in both
3252$ git log -S'foo()'         # ...where difference contain "foo()"
3253$ git log --since="2 weeks ago"
3254$ git log -p                # show patches as well
3255$ git show                  # most recent commit
3256$ git diff v2.6.15..v2.6.16 # diff between two tagged versions
3257$ git diff v2.6.15..HEAD    # diff with current head
3258$ git grep "foo()"          # search working directory for "foo()"
3259$ git grep v2.6.15 "foo()"  # search old tree for "foo()"
3260$ git show v2.6.15:a.txt    # look at old version of a.txt
3261-----------------------------------------------
3262
3263Search for regressions:
3264
3265-----------------------------------------------
3266$ git bisect start
3267$ git bisect bad                # current version is bad
3268$ git bisect good v2.6.13-rc2   # last known good revision
3269Bisecting: 675 revisions left to test after this
3270                                # test here, then:
3271$ git bisect good               # if this revision is good, or
3272$ git bisect bad                # if this revision is bad.
3273                                # repeat until done.
3274-----------------------------------------------
3275
3276[[making-changes]]
3277Making changes
3278--------------
3279
3280Make sure git knows who to blame:
3281
3282------------------------------------------------
3283$ cat >>~/.gitconfig <<\EOF
3284[user]
3285        name = Your Name Comes Here
3286        email = you@yourdomain.example.com
3287EOF
3288------------------------------------------------
3289
3290Select file contents to include in the next commit, then make the
3291commit:
3292
3293-----------------------------------------------
3294$ git add a.txt    # updated file
3295$ git add b.txt    # new file
3296$ git rm c.txt     # old file
3297$ git commit
3298-----------------------------------------------
3299
3300Or, prepare and create the commit in one step:
3301
3302-----------------------------------------------
3303$ git commit d.txt # use latest content only of d.txt
3304$ git commit -a    # use latest content of all tracked files
3305-----------------------------------------------
3306
3307[[merging]]
3308Merging
3309-------
3310
3311-----------------------------------------------
3312$ git merge test   # merge branch "test" into the current branch
3313$ git pull git://example.com/project.git master
3314                   # fetch and merge in remote branch
3315$ git pull . test  # equivalent to git merge test
3316-----------------------------------------------
3317
3318[[sharing-your-changes]]
3319Sharing your changes
3320--------------------
3321
3322Importing or exporting patches:
3323
3324-----------------------------------------------
3325$ git format-patch origin..HEAD # format a patch for each commit
3326                                # in HEAD but not in origin
3327$ git am mbox # import patches from the mailbox "mbox"
3328-----------------------------------------------
3329
3330Fetch a branch in a different git repository, then merge into the
3331current branch:
3332
3333-----------------------------------------------
3334$ git pull git://example.com/project.git theirbranch
3335-----------------------------------------------
3336
3337Store the fetched branch into a local branch before merging into the
3338current branch:
3339
3340-----------------------------------------------
3341$ git pull git://example.com/project.git theirbranch:mybranch
3342-----------------------------------------------
3343
3344After creating commits on a local branch, update the remote
3345branch with your commits:
3346
3347-----------------------------------------------
3348$ git push ssh://example.com/project.git mybranch:theirbranch
3349-----------------------------------------------
3350
3351When remote and local branch are both named "test":
3352
3353-----------------------------------------------
3354$ git push ssh://example.com/project.git test
3355-----------------------------------------------
3356
3357Shortcut version for a frequently used remote repository:
3358
3359-----------------------------------------------
3360$ git remote add example ssh://example.com/project.git
3361$ git push example test
3362-----------------------------------------------
3363
3364[[repository-maintenance]]
3365Repository maintenance
3366----------------------
3367
3368Check for corruption:
3369
3370-----------------------------------------------
3371$ git fsck
3372-----------------------------------------------
3373
3374Recompress, remove unused cruft:
3375
3376-----------------------------------------------
3377$ git gc
3378-----------------------------------------------
3379
3380
3381[[todo]]
3382Appendix B: Notes and todo list for this manual
3383===============================================
3384
3385This is a work in progress.
3386
3387The basic requirements:
3388        - It must be readable in order, from beginning to end, by
3389          someone intelligent with a basic grasp of the unix
3390          commandline, but without any special knowledge of git.  If
3391          necessary, any other prerequisites should be specifically
3392          mentioned as they arise.
3393        - Whenever possible, section headings should clearly describe
3394          the task they explain how to do, in language that requires
3395          no more knowledge than necessary: for example, "importing
3396          patches into a project" rather than "the git-am command"
3397
3398Think about how to create a clear chapter dependency graph that will
3399allow people to get to important topics without necessarily reading
3400everything in between.
3401
3402Say something about .gitignore.
3403
3404Scan Documentation/ for other stuff left out; in particular:
3405        howto's
3406        some of technical/?
3407        hooks
3408        list of commands in gitlink:git[1]
3409
3410Scan email archives for other stuff left out
3411
3412Scan man pages to see if any assume more background than this manual
3413provides.
3414
3415Simplify beginning by suggesting disconnected head instead of
3416temporary branch creation?
3417
3418Add more good examples.  Entire sections of just cookbook examples
3419might be a good idea; maybe make an "advanced examples" section a
3420standard end-of-chapter section?
3421
3422Include cross-references to the glossary, where appropriate.
3423
3424Document shallow clones?  See draft 1.5.0 release notes for some
3425documentation.
3426
3427Add a section on working with other version control systems, including
3428CVS, Subversion, and just imports of series of release tarballs.
3429
3430More details on gitweb?
3431
3432Write a chapter on using plumbing and writing scripts.