1GITCORE-TUTORIAL(7)               Git Manual               GITCORE-TUTORIAL(7)
2
3
4

NAME

6       gitcore-tutorial - A git core tutorial for developers
7

SYNOPSIS

9       git *
10

DESCRIPTION

12       This tutorial explains how to use the "core" git commands to set up and
13       work with a git repository.
14
15       If you just need to use git as a revision control system you may prefer
16       to start with "A Tutorial Introduction to GIT" (gittutorial(7)) or the
17       GIT User Manual[1].
18
19       However, an understanding of these low-level tools can be helpful if
20       you want to understand git’s internals.
21
22       The core git is often called "plumbing", with the prettier user
23       interfaces on top of it called "porcelain". You may not want to use the
24       plumbing directly very often, but it can be good to know what the
25       plumbing does for when the porcelain isn’t flushing.
26
27       Back when this document was originally written, many porcelain commands
28       were shell scripts. For simplicity, it still uses them as examples to
29       illustrate how plumbing is fit together to form the porcelain commands.
30       The source tree includes some of these scripts in contrib/examples/ for
31       reference. Although these are not implemented as shell scripts anymore,
32       the description of what the plumbing layer commands do is still valid.
33
34           Note
35           Deeper technical details are often marked as Notes, which you can
36           skip on your first reading.
37

CREATING A GIT REPOSITORY

39       Creating a new git repository couldn’t be easier: all git repositories
40       start out empty, and the only thing you need to do is find yourself a
41       subdirectory that you want to use as a working tree - either an empty
42       one for a totally new project, or an existing working tree that you
43       want to import into git.
44
45       For our first example, we’re going to start a totally new repository
46       from scratch, with no pre-existing files, and we’ll call it
47       git-tutorial. To start up, create a subdirectory for it, change into
48       that subdirectory, and initialize the git infrastructure with git init:
49
50           $ mkdir git-tutorial
51           $ cd git-tutorial
52           $ git init
53
54
55       to which git will reply
56
57           Initialized empty Git repository in .git/
58
59
60       which is just git’s way of saying that you haven’t been doing anything
61       strange, and that it will have created a local .git directory setup for
62       your new project. You will now have a .git directory, and you can
63       inspect that with ls. For your new empty project, it should show you
64       three entries, among other things:
65
66       ·   a file called HEAD, that has ref: refs/heads/master in it. This is
67           similar to a symbolic link and points at refs/heads/master relative
68           to the HEAD file.
69
70           Don’t worry about the fact that the file that the HEAD link points
71           to doesn’t even exist yet — you haven’t created the commit that
72           will start your HEAD development branch yet.
73
74       ·   a subdirectory called objects, which will contain all the objects
75           of your project. You should never have any real reason to look at
76           the objects directly, but you might want to know that these objects
77           are what contains all the real data in your repository.
78
79       ·   a subdirectory called refs, which contains references to objects.
80
81       In particular, the refs subdirectory will contain two other
82       subdirectories, named heads and tags respectively. They do exactly what
83       their names imply: they contain references to any number of different
84       heads of development (aka branches), and to any tags that you have
85       created to name specific versions in your repository.
86
87       One note: the special master head is the default branch, which is why
88       the .git/HEAD file was created points to it even if it doesn’t yet
89       exist. Basically, the HEAD link is supposed to always point to the
90       branch you are working on right now, and you always start out expecting
91       to work on the master branch.
92
93       However, this is only a convention, and you can name your branches
94       anything you want, and don’t have to ever even have a master branch. A
95       number of the git tools will assume that .git/HEAD is valid, though.
96
97           Note
98           An object is identified by its 160-bit SHA1 hash, aka object name,
99           and a reference to an object is always the 40-byte hex
100           representation of that SHA1 name. The files in the refs
101           subdirectory are expected to contain these hex references (usually
102           with a final \n at the end), and you should thus expect to see a
103           number of 41-byte files containing these references in these refs
104           subdirectories when you actually start populating your tree.
105
106           Note
107           An advanced user may want to take a look at gitrepository-layout(5)
108           after finishing this tutorial.
109
110       You have now created your first git repository. Of course, since it’s
111       empty, that’s not very useful, so let’s start populating it with data.
112

POPULATING A GIT REPOSITORY

114       We’ll keep this simple and stupid, so we’ll start off with populating a
115       few trivial files just to get a feel for it.
116
117       Start off with just creating any random files that you want to maintain
118       in your git repository. We’ll start off with a few bad examples, just
119       to get a feel for how this works:
120
121           $ echo "Hello World" >hello
122           $ echo "Silly example" >example
123
124
125       you have now created two files in your working tree (aka working
126       directory), but to actually check in your hard work, you will have to
127       go through two steps:
128
129       ·   fill in the index file (aka cache) with the information about your
130           working tree state.
131
132       ·   commit that index file as an object.
133
134       The first step is trivial: when you want to tell git about any changes
135       to your working tree, you use the git update-index program. That
136       program normally just takes a list of filenames you want to update, but
137       to avoid trivial mistakes, it refuses to add new entries to the index
138       (or remove existing ones) unless you explicitly tell it that you’re
139       adding a new entry with the --add flag (or removing an entry with the
140       --remove) flag.
141
142       So to populate the index with the two files you just created, you can
143       do
144
145           $ git update-index --add hello example
146
147
148       and you have now told git to track those two files.
149
150       In fact, as you did that, if you now look into your object directory,
151       you’ll notice that git will have added two new objects to the object
152       database. If you did exactly the steps above, you should now be able to
153       do
154
155           $ ls .git/objects/??/*
156
157
158       and see two files:
159
160           .git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238
161           .git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962
162
163
164       which correspond with the objects with names of 557db... and f24c7...
165       respectively.
166
167       If you want to, you can use git cat-file to look at those objects, but
168       you’ll have to use the object name, not the filename of the object:
169
170           $ git cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238
171
172
173       where the -t tells git cat-file to tell you what the "type" of the
174       object is. git will tell you that you have a "blob" object (i.e., just
175       a regular file), and you can see the contents with
176
177           $ git cat-file blob 557db03
178
179
180       which will print out "Hello World". The object 557db03 is nothing more
181       than the contents of your file hello.
182
183           Note
184           Don’t confuse that object with the file hello itself. The object is
185           literally just those specific contents of the file, and however
186           much you later change the contents in file hello, the object we
187           just looked at will never change. Objects are immutable.
188
189           Note
190           The second example demonstrates that you can abbreviate the object
191           name to only the first several hexadecimal digits in most places.
192
193       Anyway, as we mentioned previously, you normally never actually take a
194       look at the objects themselves, and typing long 40-character hex names
195       is not something you’d normally want to do. The above digression was
196       just to show that git update-index did something magical, and actually
197       saved away the contents of your files into the git object database.
198
199       Updating the index did something else too: it created a .git/index
200       file. This is the index that describes your current working tree, and
201       something you should be very aware of. Again, you normally never worry
202       about the index file itself, but you should be aware of the fact that
203       you have not actually really "checked in" your files into git so far,
204       you’ve only told git about them.
205
206       However, since git knows about them, you can now start using some of
207       the most basic git commands to manipulate the files or look at their
208       status.
209
210       In particular, let’s not even check in the two files into git yet,
211       we’ll start off by adding another line to hello first:
212
213           $ echo "It's a new day for git" >>hello
214
215
216       and you can now, since you told git about the previous state of hello,
217       ask git what has changed in the tree compared to your old index, using
218       the git diff-files command:
219
220           $ git diff-files
221
222
223       Oops. That wasn’t very readable. It just spit out its own internal
224       version of a diff, but that internal version really just tells you that
225       it has noticed that "hello" has been modified, and that the old object
226       contents it had have been replaced with something else.
227
228       To make it readable, we can tell git diff-files to output the
229       differences as a patch, using the -p flag:
230
231           $ git diff-files -p
232           diff --git a/hello b/hello
233           index 557db03..263414f 100644
234           --- a/hello
235           +++ b/hello
236           @@ -1 +1,2 @@
237            Hello World
238           +It's a new day for git
239
240
241       i.e. the diff of the change we caused by adding another line to hello.
242
243       In other words, git diff-files always shows us the difference between
244       what is recorded in the index, and what is currently in the working
245       tree. That’s very useful.
246
247       A common shorthand for git diff-files -p is to just write git diff,
248       which will do the same thing.
249
250           $ git diff
251           diff --git a/hello b/hello
252           index 557db03..263414f 100644
253           --- a/hello
254           +++ b/hello
255           @@ -1 +1,2 @@
256            Hello World
257           +It's a new day for git
258
259

COMMITTING GIT STATE

261       Now, we want to go to the next stage in git, which is to take the files
262       that git knows about in the index, and commit them as a real tree. We
263       do that in two phases: creating a tree object, and committing that tree
264       object as a commit object together with an explanation of what the tree
265       was all about, along with information of how we came to that state.
266
267       Creating a tree object is trivial, and is done with git write-tree.
268       There are no options or other input: git write-tree will take the
269       current index state, and write an object that describes that whole
270       index. In other words, we’re now tying together all the different
271       filenames with their contents (and their permissions), and we’re
272       creating the equivalent of a git "directory" object:
273
274           $ git write-tree
275
276
277       and this will just output the name of the resulting tree, in this case
278       (if you have done exactly as I’ve described) it should be
279
280           8988da15d077d4829fc51d8544c097def6644dbb
281
282
283       which is another incomprehensible object name. Again, if you want to,
284       you can use git cat-file -t 8988d... to see that this time the object
285       is not a "blob" object, but a "tree" object (you can also use git
286       cat-file to actually output the raw object contents, but you’ll see
287       mainly a binary mess, so that’s less interesting).
288
289       However — normally you’d never use git write-tree on its own, because
290       normally you always commit a tree into a commit object using the git
291       commit-tree command. In fact, it’s easier to not actually use git
292       write-tree on its own at all, but to just pass its result in as an
293       argument to git commit-tree.
294
295       git commit-tree normally takes several arguments — it wants to know
296       what the parent of a commit was, but since this is the first commit
297       ever in this new repository, and it has no parents, we only need to
298       pass in the object name of the tree. However, git commit-tree also
299       wants to get a commit message on its standard input, and it will write
300       out the resulting object name for the commit to its standard output.
301
302       And this is where we create the .git/refs/heads/master file which is
303       pointed at by HEAD. This file is supposed to contain the reference to
304       the top-of-tree of the master branch, and since that’s exactly what git
305       commit-tree spits out, we can do this all with a sequence of simple
306       shell commands:
307
308           $ tree=$(git write-tree)
309           $ commit=$(echo 'Initial commit' | git commit-tree $tree)
310           $ git update-ref HEAD $commit
311
312
313       In this case this creates a totally new commit that is not related to
314       anything else. Normally you do this only once for a project ever, and
315       all later commits will be parented on top of an earlier commit.
316
317       Again, normally you’d never actually do this by hand. There is a
318       helpful script called git commit that will do all of this for you. So
319       you could have just written git commit instead, and it would have done
320       the above magic scripting for you.
321

MAKING A CHANGE

323       Remember how we did the git update-index on file hello and then we
324       changed hello afterward, and could compare the new state of hello with
325       the state we saved in the index file?
326
327       Further, remember how I said that git write-tree writes the contents of
328       the index file to the tree, and thus what we just committed was in fact
329       the original contents of the file hello, not the new ones. We did that
330       on purpose, to show the difference between the index state, and the
331       state in the working tree, and how they don’t have to match, even when
332       we commit things.
333
334       As before, if we do git diff-files -p in our git-tutorial project,
335       we’ll still see the same difference we saw last time: the index file
336       hasn’t changed by the act of committing anything. However, now that we
337       have committed something, we can also learn to use a new command: git
338       diff-index.
339
340       Unlike git diff-files, which showed the difference between the index
341       file and the working tree, git diff-index shows the differences between
342       a committed tree and either the index file or the working tree. In
343       other words, git diff-index wants a tree to be diffed against, and
344       before we did the commit, we couldn’t do that, because we didn’t have
345       anything to diff against.
346
347       But now we can do
348
349           $ git diff-index -p HEAD
350
351
352       (where -p has the same meaning as it did in git diff-files), and it
353       will show us the same difference, but for a totally different reason.
354       Now we’re comparing the working tree not against the index file, but
355       against the tree we just wrote. It just so happens that those two are
356       obviously the same, so we get the same result.
357
358       Again, because this is a common operation, you can also just shorthand
359       it with
360
361           $ git diff HEAD
362
363
364       which ends up doing the above for you.
365
366       In other words, git diff-index normally compares a tree against the
367       working tree, but when given the --cached flag, it is told to instead
368       compare against just the index cache contents, and ignore the current
369       working tree state entirely. Since we just wrote the index file to
370       HEAD, doing git diff-index --cached -p HEAD should thus return an empty
371       set of differences, and that’s exactly what it does.
372
373           Note
374           git diff-index really always uses the index for its comparisons,
375           and saying that it compares a tree against the working tree is thus
376           not strictly accurate. In particular, the list of files to compare
377           (the "meta-data") always comes from the index file, regardless of
378           whether the --cached flag is used or not. The --cached flag really
379           only determines whether the file contents to be compared come from
380           the working tree or not.
381
382           This is not hard to understand, as soon as you realize that git
383           simply never knows (or cares) about files that it is not told about
384           explicitly. git will never go looking for files to compare, it
385           expects you to tell it what the files are, and that’s what the
386           index is there for.
387
388       However, our next step is to commit the change we did, and again, to
389       understand what’s going on, keep in mind the difference between
390       "working tree contents", "index file" and "committed tree". We have
391       changes in the working tree that we want to commit, and we always have
392       to work through the index file, so the first thing we need to do is to
393       update the index cache:
394
395           $ git update-index hello
396
397
398       (note how we didn’t need the --add flag this time, since git knew about
399       the file already).
400
401       Note what happens to the different git diff-* versions here. After
402       we’ve updated hello in the index, git diff-files -p now shows no
403       differences, but git diff-index -p HEAD still does show that the
404       current state is different from the state we committed. In fact, now
405       git diff-index shows the same difference whether we use the --cached
406       flag or not, since now the index is coherent with the working tree.
407
408       Now, since we’ve updated hello in the index, we can commit the new
409       version. We could do it by writing the tree by hand again, and
410       committing the tree (this time we’d have to use the -p HEAD flag to
411       tell commit that the HEAD was the parent of the new commit, and that
412       this wasn’t an initial commit any more), but you’ve done that once
413       already, so let’s just use the helpful script this time:
414
415           $ git commit
416
417
418       which starts an editor for you to write the commit message and tells
419       you a bit about what you have done.
420
421       Write whatever message you want, and all the lines that start with #
422       will be pruned out, and the rest will be used as the commit message for
423       the change. If you decide you don’t want to commit anything after all
424       at this point (you can continue to edit things and update the index),
425       you can just leave an empty message. Otherwise git commit will commit
426       the change for you.
427
428       You’ve now made your first real git commit. And if you’re interested in
429       looking at what git commit really does, feel free to investigate: it’s
430       a few very simple shell scripts to generate the helpful (?) commit
431       message headers, and a few one-liners that actually do the commit
432       itself (git commit).
433

INSPECTING CHANGES

435       While creating changes is useful, it’s even more useful if you can tell
436       later what changed. The most useful command for this is another of the
437       diff family, namely git diff-tree.
438
439       git diff-tree can be given two arbitrary trees, and it will tell you
440       the differences between them. Perhaps even more commonly, though, you
441       can give it just a single commit object, and it will figure out the
442       parent of that commit itself, and show the difference directly. Thus,
443       to get the same diff that we’ve already seen several times, we can now
444       do
445
446           $ git diff-tree -p HEAD
447
448
449       (again, -p means to show the difference as a human-readable patch), and
450       it will show what the last commit (in HEAD) actually changed.
451
452           Note
453           Here is an ASCII art by Jon Loeliger that illustrates how various
454           diff-* commands compare things.
455
456                           diff-tree
457                            +----+
458                            |    |
459                            |    |
460                            V    V
461                         +-----------+
462                         | Object DB |
463                         |  Backing  |
464                         |   Store   |
465                         +-----------+
466                           ^    ^
467                           |    |
468                           |    |  diff-index --cached
469                           |    |
470               diff-index  |    V
471                           |  +-----------+
472                           |  |   Index   |
473                           |  |  "cache"  |
474                           |  +-----------+
475                           |    ^
476                           |    |
477                           |    |  diff-files
478                           |    |
479                           V    V
480                         +-----------+
481                         |  Working  |
482                         | Directory |
483                         +-----------+
484
485       More interestingly, you can also give git diff-tree the --pretty flag,
486       which tells it to also show the commit message and author and date of
487       the commit, and you can tell it to show a whole series of diffs.
488       Alternatively, you can tell it to be "silent", and not show the diffs
489       at all, but just show the actual commit message.
490
491       In fact, together with the git rev-list program (which generates a list
492       of revisions), git diff-tree ends up being a veritable fount of
493       changes. A trivial (but very useful) script called git whatchanged is
494       included with git which does exactly this, and shows a log of recent
495       activities.
496
497       To see the whole history of our pitiful little git-tutorial project,
498       you can do
499
500           $ git log
501
502
503       which shows just the log messages, or if we want to see the log
504       together with the associated patches use the more complex (and much
505       more powerful)
506
507           $ git whatchanged -p
508
509
510       and you will see exactly what has changed in the repository over its
511       short history.
512
513           Note
514           When using the above two commands, the initial commit will be
515           shown. If this is a problem because it is huge, you can hide it by
516           setting the log.showroot configuration variable to false. Having
517           this, you can still show it for each command just adding the --root
518           option, which is a flag for git diff-tree accepted by both
519           commands.
520
521       With that, you should now be having some inkling of what git does, and
522       can explore on your own.
523
524           Note
525           Most likely, you are not directly using the core git Plumbing
526           commands, but using Porcelain such as git add, ‘git-rm’ and
527           ‘git-commit’.
528

TAGGING A VERSION

530       In git, there are two kinds of tags, a "light" one, and an "annotated
531       tag".
532
533       A "light" tag is technically nothing more than a branch, except we put
534       it in the .git/refs/tags/ subdirectory instead of calling it a head. So
535       the simplest form of tag involves nothing more than
536
537           $ git tag my-first-tag
538
539
540       which just writes the current HEAD into the .git/refs/tags/my-first-tag
541       file, after which point you can then use this symbolic name for that
542       particular state. You can, for example, do
543
544           $ git diff my-first-tag
545
546
547       to diff your current state against that tag which at this point will
548       obviously be an empty diff, but if you continue to develop and commit
549       stuff, you can use your tag as an "anchor-point" to see what has
550       changed since you tagged it.
551
552       An "annotated tag" is actually a real git object, and contains not only
553       a pointer to the state you want to tag, but also a small tag name and
554       message, along with optionally a PGP signature that says that yes, you
555       really did that tag. You create these annotated tags with either the -a
556       or -s flag to git tag:
557
558           $ git tag -s <tagname>
559
560
561       which will sign the current HEAD (but you can also give it another
562       argument that specifies the thing to tag, e.g., you could have tagged
563       the current mybranch point by using git tag <tagname> mybranch).
564
565       You normally only do signed tags for major releases or things like
566       that, while the light-weight tags are useful for any marking you want
567       to do — any time you decide that you want to remember a certain point,
568       just create a private tag for it, and you have a nice symbolic name for
569       the state at that point.
570

COPYING REPOSITORIES

572       git repositories are normally totally self-sufficient and relocatable.
573       Unlike CVS, for example, there is no separate notion of "repository"
574       and "working tree". A git repository normally is the working tree, with
575       the local git information hidden in the .git subdirectory. There is
576       nothing else. What you see is what you got.
577
578           Note
579           You can tell git to split the git internal information from the
580           directory that it tracks, but we’ll ignore that for now: it’s not
581           how normal projects work, and it’s really only meant for special
582           uses. So the mental model of "the git information is always tied
583           directly to the working tree that it describes" may not be
584           technically 100% accurate, but it’s a good model for all normal
585           use.
586
587       This has two implications:
588
589       ·   if you grow bored with the tutorial repository you created (or
590           you’ve made a mistake and want to start all over), you can just do
591           simple
592
593               $ rm -rf git-tutorial
594
595           and it will be gone. There’s no external repository, and there’s no
596           history outside the project you created.
597
598       ·   if you want to move or duplicate a git repository, you can do so.
599           There is git clone command, but if all you want to do is just to
600           create a copy of your repository (with all the full history that
601           went along with it), you can do so with a regular cp -a
602           git-tutorial new-git-tutorial.
603
604           Note that when you’ve moved or copied a git repository, your git
605           index file (which caches various information, notably some of the
606           "stat" information for the files involved) will likely need to be
607           refreshed. So after you do a cp -a to create a new copy, you’ll
608           want to do
609
610               $ git update-index --refresh
611
612           in the new repository to make sure that the index file is
613           up-to-date.
614
615       Note that the second point is true even across machines. You can
616       duplicate a remote git repository with any regular copy mechanism, be
617       it scp, rsync or wget.
618
619       When copying a remote repository, you’ll want to at a minimum update
620       the index cache when you do this, and especially with other peoples'
621       repositories you often want to make sure that the index cache is in
622       some known state (you don’t know what they’ve done and not yet checked
623       in), so usually you’ll precede the git update-index with a
624
625           $ git read-tree --reset HEAD
626           $ git update-index --refresh
627
628
629       which will force a total index re-build from the tree pointed to by
630       HEAD. It resets the index contents to HEAD, and then the git
631       update-index makes sure to match up all index entries with the
632       checked-out files. If the original repository had uncommitted changes
633       in its working tree, git update-index --refresh notices them and tells
634       you they need to be updated.
635
636       The above can also be written as simply
637
638           $ git reset
639
640
641       and in fact a lot of the common git command combinations can be
642       scripted with the git xyz interfaces. You can learn things by just
643       looking at what the various git scripts do. For example, git reset used
644       to be the above two lines implemented in git reset, but some things
645       like git status and git commit are slightly more complex scripts around
646       the basic git commands.
647
648       Many (most?) public remote repositories will not contain any of the
649       checked out files or even an index file, and will only contain the
650       actual core git files. Such a repository usually doesn’t even have the
651       .git subdirectory, but has all the git files directly in the
652       repository.
653
654       To create your own local live copy of such a "raw" git repository,
655       you’d first create your own subdirectory for the project, and then copy
656       the raw repository contents into the .git directory. For example, to
657       create your own copy of the git repository, you’d do the following
658
659           $ mkdir my-git
660           $ cd my-git
661           $ rsync -rL rsync://rsync.kernel.org/pub/scm/git/git.git/ .git
662
663
664       followed by
665
666           $ git read-tree HEAD
667
668
669       to populate the index. However, now you have populated the index, and
670       you have all the git internal files, but you will notice that you don’t
671       actually have any of the working tree files to work on. To get those,
672       you’d check them out with
673
674           $ git checkout-index -u -a
675
676
677       where the -u flag means that you want the checkout to keep the index
678       up-to-date (so that you don’t have to refresh it afterward), and the -a
679       flag means "check out all files" (if you have a stale copy or an older
680       version of a checked out tree you may also need to add the -f flag
681       first, to tell git checkout-index to force overwriting of any old
682       files).
683
684       Again, this can all be simplified with
685
686           $ git clone rsync://rsync.kernel.org/pub/scm/git/git.git/ my-git
687           $ cd my-git
688           $ git checkout
689
690
691       which will end up doing all of the above for you.
692
693       You have now successfully copied somebody else’s (mine) remote
694       repository, and checked it out.
695

CREATING A NEW BRANCH

697       Branches in git are really nothing more than pointers into the git
698       object database from within the .git/refs/ subdirectory, and as we
699       already discussed, the HEAD branch is nothing but a symlink to one of
700       these object pointers.
701
702       You can at any time create a new branch by just picking an arbitrary
703       point in the project history, and just writing the SHA1 name of that
704       object into a file under .git/refs/heads/. You can use any filename you
705       want (and indeed, subdirectories), but the convention is that the
706       "normal" branch is called master. That’s just a convention, though, and
707       nothing enforces it.
708
709       To show that as an example, let’s go back to the git-tutorial
710       repository we used earlier, and create a branch in it. You do that by
711       simply just saying that you want to check out a new branch:
712
713           $ git checkout -b mybranch
714
715
716       will create a new branch based at the current HEAD position, and switch
717       to it.
718
719           Note
720           If you make the decision to start your new branch at some other
721           point in the history than the current HEAD, you can do so by just
722           telling git checkout what the base of the checkout would be. In
723           other words, if you have an earlier tag or branch, you’d just do
724
725               $ git checkout -b mybranch earlier-commit
726
727
728           and it would create the new branch mybranch at the earlier commit,
729           and check out the state at that time.
730
731       You can always just jump back to your original master branch by doing
732
733           $ git checkout master
734
735
736       (or any other branch-name, for that matter) and if you forget which
737       branch you happen to be on, a simple
738
739           $ cat .git/HEAD
740
741
742       will tell you where it’s pointing. To get the list of branches you
743       have, you can say
744
745           $ git branch
746
747
748       which used to be nothing more than a simple script around ls
749       .git/refs/heads. There will be an asterisk in front of the branch you
750       are currently on.
751
752       Sometimes you may wish to create a new branch without actually checking
753       it out and switching to it. If so, just use the command
754
755           $ git branch <branchname> [startingpoint]
756
757
758       which will simply create the branch, but will not do anything further.
759       You can then later — once you decide that you want to actually develop
760       on that branch — switch to that branch with a regular git checkout with
761       the branchname as the argument.
762

MERGING TWO BRANCHES

764       One of the ideas of having a branch is that you do some (possibly
765       experimental) work in it, and eventually merge it back to the main
766       branch. So assuming you created the above mybranch that started out
767       being the same as the original master branch, let’s make sure we’re in
768       that branch, and do some work there.
769
770           $ git checkout mybranch
771           $ echo "Work, work, work" >>hello
772           $ git commit -m "Some work." -i hello
773
774
775       Here, we just added another line to hello, and we used a shorthand for
776       doing both git update-index hello and git commit by just giving the
777       filename directly to git commit, with an -i flag (it tells git to
778       include that file in addition to what you have done to the index file
779       so far when making the commit). The -m flag is to give the commit log
780       message from the command line.
781
782       Now, to make it a bit more interesting, let’s assume that somebody else
783       does some work in the original branch, and simulate that by going back
784       to the master branch, and editing the same file differently there:
785
786           $ git checkout master
787
788
789       Here, take a moment to look at the contents of hello, and notice how
790       they don’t contain the work we just did in mybranch — because that work
791       hasn’t happened in the master branch at all. Then do
792
793           $ echo "Play, play, play" >>hello
794           $ echo "Lots of fun" >>example
795           $ git commit -m "Some fun." -i hello example
796
797
798       since the master branch is obviously in a much better mood.
799
800       Now, you’ve got two branches, and you decide that you want to merge the
801       work done. Before we do that, let’s introduce a cool graphical tool
802       that helps you view what’s going on:
803
804           $ gitk --all
805
806
807       will show you graphically both of your branches (that’s what the --all
808       means: normally it will just show you your current HEAD) and their
809       histories. You can also see exactly how they came to be from a common
810       source.
811
812       Anyway, let’s exit gitk (^Q or the File menu), and decide that we want
813       to merge the work we did on the mybranch branch into the master branch
814       (which is currently our HEAD too). To do that, there’s a nice script
815       called git merge, which wants to know which branches you want to
816       resolve and what the merge is all about:
817
818           $ git merge -m "Merge work in mybranch" mybranch
819
820
821       where the first argument is going to be used as the commit message if
822       the merge can be resolved automatically.
823
824       Now, in this case we’ve intentionally created a situation where the
825       merge will need to be fixed up by hand, though, so git will do as much
826       of it as it can automatically (which in this case is just merge the
827       example file, which had no differences in the mybranch branch), and
828       say:
829
830                   Auto-merging hello
831                   CONFLICT (content): Merge conflict in hello
832                   Automatic merge failed; fix conflicts and then commit the result.
833
834
835       It tells you that it did an "Automatic merge", which failed due to
836       conflicts in hello.
837
838       Not to worry. It left the (trivial) conflict in hello in the same form
839       you should already be well used to if you’ve ever used CVS, so let’s
840       just open hello in our editor (whatever that may be), and fix it up
841       somehow. I’d suggest just making it so that hello contains all four
842       lines:
843
844           Hello World
845           It's a new day for git
846           Play, play, play
847           Work, work, work
848
849
850       and once you’re happy with your manual merge, just do a
851
852           $ git commit -i hello
853
854
855       which will very loudly warn you that you’re now committing a merge
856       (which is correct, so never mind), and you can write a small merge
857       message about your adventures in git merge-land.
858
859       After you’re done, start up gitk --all to see graphically what the
860       history looks like. Notice that mybranch still exists, and you can
861       switch to it, and continue to work with it if you want to. The mybranch
862       branch will not contain the merge, but next time you merge it from the
863       master branch, git will know how you merged it, so you’ll not have to
864       do that merge again.
865
866       Another useful tool, especially if you do not always work in X-Window
867       environment, is git show-branch.
868
869           $ git show-branch --topo-order --more=1 master mybranch
870           * [master] Merge work in mybranch
871            ! [mybranch] Some work.
872           --
873           -  [master] Merge work in mybranch
874           *+ [mybranch] Some work.
875           *  [master^] Some fun.
876
877
878       The first two lines indicate that it is showing the two branches and
879       the first line of the commit log message from their top-of-the-tree
880       commits, you are currently on master branch (notice the asterisk *
881       character), and the first column for the later output lines is used to
882       show commits contained in the master branch, and the second column for
883       the mybranch branch. Three commits are shown along with their log
884       messages. All of them have non blank characters in the first column (*
885       shows an ordinary commit on the current branch, - is a merge commit),
886       which means they are now part of the master branch. Only the "Some
887       work" commit has the plus + character in the second column, because
888       mybranch has not been merged to incorporate these commits from the
889       master branch. The string inside brackets before the commit log message
890       is a short name you can use to name the commit. In the above example,
891       master and mybranch are branch heads. master^ is the first parent of
892       master branch head. Please see gitrevisions(7) if you want to see more
893       complex cases.
894
895           Note
896           Without the --more=1 option, git show-branch would not output the
897           [master^] commit, as [mybranch] commit is a common ancestor of both
898           master and mybranch tips. Please see git-show-branch(1) for
899           details.
900
901           Note
902           If there were more commits on the master branch after the merge,
903           the merge commit itself would not be shown by git show-branch by
904           default. You would need to provide --sparse option to make the
905           merge commit visible in this case.
906
907       Now, let’s pretend you are the one who did all the work in mybranch,
908       and the fruit of your hard work has finally been merged to the master
909       branch. Let’s go back to mybranch, and run git merge to get the
910       "upstream changes" back to your branch.
911
912           $ git checkout mybranch
913           $ git merge -m "Merge upstream changes." master
914
915
916       This outputs something like this (the actual commit object names would
917       be different)
918
919           Updating from ae3a2da... to a80b4aa....
920           Fast-forward (no commit created; -m option ignored)
921            example |    1 +
922            hello   |    1 +
923            2 files changed, 2 insertions(+), 0 deletions(-)
924
925
926       Because your branch did not contain anything more than what had already
927       been merged into the master branch, the merge operation did not
928       actually do a merge. Instead, it just updated the top of the tree of
929       your branch to that of the master branch. This is often called
930       fast-forward merge.
931
932       You can run gitk --all again to see how the commit ancestry looks like,
933       or run show-branch, which tells you this.
934
935           $ git show-branch master mybranch
936           ! [master] Merge work in mybranch
937            * [mybranch] Merge work in mybranch
938           --
939           -- [master] Merge work in mybranch
940
941

MERGING EXTERNAL WORK

943       It’s usually much more common that you merge with somebody else than
944       merging with your own branches, so it’s worth pointing out that git
945       makes that very easy too, and in fact, it’s not that different from
946       doing a git merge. In fact, a remote merge ends up being nothing more
947       than "fetch the work from a remote repository into a temporary tag"
948       followed by a git merge.
949
950       Fetching from a remote repository is done by, unsurprisingly, git
951       fetch:
952
953           $ git fetch <remote-repository>
954
955
956       One of the following transports can be used to name the repository to
957       download from:
958
959       Rsync
960
961           rsync://remote.machine/path/to/repo.git/
962
963           Rsync transport is usable for both uploading and downloading, but
964           is completely unaware of what git does, and can produce unexpected
965           results when you download from the public repository while the
966           repository owner is uploading into it via rsync transport. Most
967           notably, it could update the files under refs/ which holds the
968           object name of the topmost commits before uploading the files in
969           objects/ — the downloader would obtain head commit object name
970           while that object itself is still not available in the repository.
971           For this reason, it is considered deprecated.
972
973       SSH
974
975           remote.machine:/path/to/repo.git/ or
976
977           ssh://remote.machine/path/to/repo.git/
978
979           This transport can be used for both uploading and downloading, and
980           requires you to have a log-in privilege over ssh to the remote
981           machine. It finds out the set of objects the other side lacks by
982           exchanging the head commits both ends have and transfers (close to)
983           minimum set of objects. It is by far the most efficient way to
984           exchange git objects between repositories.
985
986       Local directory
987
988           /path/to/repo.git/
989
990           This transport is the same as SSH transport but uses sh to run both
991           ends on the local machine instead of running other end on the
992           remote machine via ssh.
993
994       git Native
995
996           git://remote.machine/path/to/repo.git/
997
998           This transport was designed for anonymous downloading. Like SSH
999           transport, it finds out the set of objects the downstream side
1000           lacks and transfers (close to) minimum set of objects.
1001
1002       HTTP(S)
1003
1004           http://remote.machine/path/to/repo.git/
1005
1006           Downloader from http and https URL first obtains the topmost commit
1007           object name from the remote site by looking at the specified
1008           refname under repo.git/refs/ directory, and then tries to obtain
1009           the commit object by downloading from repo.git/objects/xx/xxx...
1010           using the object name of that commit object. Then it reads the
1011           commit object to find out its parent commits and the associate tree
1012           object; it repeats this process until it gets all the necessary
1013           objects. Because of this behavior, they are sometimes also called
1014           commit walkers.
1015
1016           The commit walkers are sometimes also called dumb transports,
1017           because they do not require any git aware smart server like git
1018           Native transport does. Any stock HTTP server that does not even
1019           support directory index would suffice. But you must prepare your
1020           repository with git update-server-info to help dumb transport
1021           downloaders.
1022
1023       Once you fetch from the remote repository, you merge that with your
1024       current branch.
1025
1026       However — it’s such a common thing to fetch and then immediately merge,
1027       that it’s called git pull, and you can simply do
1028
1029           $ git pull <remote-repository>
1030
1031
1032       and optionally give a branch-name for the remote end as a second
1033       argument.
1034
1035           Note
1036           You could do without using any branches at all, by keeping as many
1037           local repositories as you would like to have branches, and merging
1038           between them with git pull, just like you merge between branches.
1039           The advantage of this approach is that it lets you keep a set of
1040           files for each branch checked out and you may find it easier to
1041           switch back and forth if you juggle multiple lines of development
1042           simultaneously. Of course, you will pay the price of more disk
1043           usage to hold multiple working trees, but disk space is cheap these
1044           days.
1045
1046       It is likely that you will be pulling from the same remote repository
1047       from time to time. As a short hand, you can store the remote repository
1048       URL in the local repository’s config file like this:
1049
1050           $ git config remote.linus.url http://www.kernel.org/pub/scm/git/git.git/
1051
1052
1053       and use the "linus" keyword with git pull instead of the full URL.
1054
1055       Examples.
1056
1057        1.  git pull linus
1058
1059        2.  git pull linus tag v0.99.1
1060
1061       the above are equivalent to:
1062
1063        1.  git pull http://www.kernel.org/pub/scm/git/git.git/ HEAD
1064
1065        2.  git pull http://www.kernel.org/pub/scm/git/git.git/ tag v0.99.1
1066

HOW DOES THE MERGE WORK?

1068       We said this tutorial shows what plumbing does to help you cope with
1069       the porcelain that isn’t flushing, but we so far did not talk about how
1070       the merge really works. If you are following this tutorial the first
1071       time, I’d suggest to skip to "Publishing your work" section and come
1072       back here later.
1073
1074       OK, still with me? To give us an example to look at, let’s go back to
1075       the earlier repository with "hello" and "example" file, and bring
1076       ourselves back to the pre-merge state:
1077
1078           $ git show-branch --more=2 master mybranch
1079           ! [master] Merge work in mybranch
1080            * [mybranch] Merge work in mybranch
1081           --
1082           -- [master] Merge work in mybranch
1083           +* [master^2] Some work.
1084           +* [master^] Some fun.
1085
1086
1087       Remember, before running git merge, our master head was at "Some fun."
1088       commit, while our mybranch head was at "Some work." commit.
1089
1090           $ git checkout mybranch
1091           $ git reset --hard master^2
1092           $ git checkout master
1093           $ git reset --hard master^
1094
1095
1096       After rewinding, the commit structure should look like this:
1097
1098           $ git show-branch
1099           * [master] Some fun.
1100            ! [mybranch] Some work.
1101           --
1102           *  [master] Some fun.
1103            + [mybranch] Some work.
1104           *+ [master^] Initial commit
1105
1106
1107       Now we are ready to experiment with the merge by hand.
1108
1109       git merge command, when merging two branches, uses 3-way merge
1110       algorithm. First, it finds the common ancestor between them. The
1111       command it uses is git merge-base:
1112
1113           $ mb=$(git merge-base HEAD mybranch)
1114
1115
1116       The command writes the commit object name of the common ancestor to the
1117       standard output, so we captured its output to a variable, because we
1118       will be using it in the next step. By the way, the common ancestor
1119       commit is the "Initial commit" commit in this case. You can tell it by:
1120
1121           $ git name-rev --name-only --tags $mb
1122           my-first-tag
1123
1124
1125       After finding out a common ancestor commit, the second step is this:
1126
1127           $ git read-tree -m -u $mb HEAD mybranch
1128
1129
1130       This is the same git read-tree command we have already seen, but it
1131       takes three trees, unlike previous examples. This reads the contents of
1132       each tree into different stage in the index file (the first tree goes
1133       to stage 1, the second to stage 2, etc.). After reading three trees
1134       into three stages, the paths that are the same in all three stages are
1135       collapsed into stage 0. Also paths that are the same in two of three
1136       stages are collapsed into stage 0, taking the SHA1 from either stage 2
1137       or stage 3, whichever is different from stage 1 (i.e. only one side
1138       changed from the common ancestor).
1139
1140       After collapsing operation, paths that are different in three trees are
1141       left in non-zero stages. At this point, you can inspect the index file
1142       with this command:
1143
1144           $ git ls-files --stage
1145           100644 7f8b141b65fdcee47321e399a2598a235a032422 0       example
1146           100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1       hello
1147           100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2       hello
1148           100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello
1149
1150
1151       In our example of only two files, we did not have unchanged files so
1152       only example resulted in collapsing. But in real-life large projects,
1153       when only a small number of files change in one commit, this collapsing
1154       tends to trivially merge most of the paths fairly quickly, leaving only
1155       a handful of real changes in non-zero stages.
1156
1157       To look at only non-zero stages, use --unmerged flag:
1158
1159           $ git ls-files --unmerged
1160           100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1       hello
1161           100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2       hello
1162           100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello
1163
1164
1165       The next step of merging is to merge these three versions of the file,
1166       using 3-way merge. This is done by giving git merge-one-file command as
1167       one of the arguments to git merge-index command:
1168
1169           $ git merge-index git-merge-one-file hello
1170           Auto-merging hello
1171           ERROR: Merge conflict in hello
1172           fatal: merge program failed
1173
1174
1175       git merge-one-file script is called with parameters to describe those
1176       three versions, and is responsible to leave the merge results in the
1177       working tree. It is a fairly straightforward shell script, and
1178       eventually calls merge program from RCS suite to perform a file-level
1179       3-way merge. In this case, merge detects conflicts, and the merge
1180       result with conflict marks is left in the working tree.. This can be
1181       seen if you run ls-files --stage again at this point:
1182
1183           $ git ls-files --stage
1184           100644 7f8b141b65fdcee47321e399a2598a235a032422 0       example
1185           100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1       hello
1186           100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2       hello
1187           100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello
1188
1189
1190       This is the state of the index file and the working file after git
1191       merge returns control back to you, leaving the conflicting merge for
1192       you to resolve. Notice that the path hello is still unmerged, and what
1193       you see with git diff at this point is differences since stage 2 (i.e.
1194       your version).
1195

PUBLISHING YOUR WORK

1197       So, we can use somebody else’s work from a remote repository, but how
1198       can you prepare a repository to let other people pull from it?
1199
1200       You do your real work in your working tree that has your primary
1201       repository hanging under it as its .git subdirectory. You could make
1202       that repository accessible remotely and ask people to pull from it, but
1203       in practice that is not the way things are usually done. A recommended
1204       way is to have a public repository, make it reachable by other people,
1205       and when the changes you made in your primary working tree are in good
1206       shape, update the public repository from it. This is often called
1207       pushing.
1208
1209           Note
1210           This public repository could further be mirrored, and that is how
1211           git repositories at kernel.org are managed.
1212
1213       Publishing the changes from your local (private) repository to your
1214       remote (public) repository requires a write privilege on the remote
1215       machine. You need to have an SSH account there to run a single command,
1216       git-receive-pack.
1217
1218       First, you need to create an empty repository on the remote machine
1219       that will house your public repository. This empty repository will be
1220       populated and be kept up-to-date by pushing into it later. Obviously,
1221       this repository creation needs to be done only once.
1222
1223           Note
1224           git push uses a pair of commands, git send-pack on your local
1225           machine, and git-receive-pack on the remote machine. The
1226           communication between the two over the network internally uses an
1227           SSH connection.
1228
1229       Your private repository’s git directory is usually .git, but your
1230       public repository is often named after the project name, i.e.
1231       <project>.git. Let’s create such a public repository for project
1232       my-git. After logging into the remote machine, create an empty
1233       directory:
1234
1235           $ mkdir my-git.git
1236
1237
1238       Then, make that directory into a git repository by running git init,
1239       but this time, since its name is not the usual .git, we do things
1240       slightly differently:
1241
1242           $ GIT_DIR=my-git.git git init
1243
1244
1245       Make sure this directory is available for others you want your changes
1246       to be pulled via the transport of your choice. Also you need to make
1247       sure that you have the git-receive-pack program on the $PATH.
1248
1249           Note
1250           Many installations of sshd do not invoke your shell as the login
1251           shell when you directly run programs; what this means is that if
1252           your login shell is bash, only .bashrc is read and not
1253           .bash_profile. As a workaround, make sure .bashrc sets up $PATH so
1254           that you can run git-receive-pack program.
1255
1256           Note
1257           If you plan to publish this repository to be accessed over http,
1258           you should do mv my-git.git/hooks/post-update.sample
1259           my-git.git/hooks/post-update at this point. This makes sure that
1260           every time you push into this repository, git update-server-info is
1261           run.
1262
1263       Your "public repository" is now ready to accept your changes. Come back
1264       to the machine you have your private repository. From there, run this
1265       command:
1266
1267           $ git push <public-host>:/path/to/my-git.git master
1268
1269
1270       This synchronizes your public repository to match the named branch head
1271       (i.e. master in this case) and objects reachable from them in your
1272       current repository.
1273
1274       As a real example, this is how I update my public git repository.
1275       Kernel.org mirror network takes care of the propagation to other
1276       publicly visible machines:
1277
1278           $ git push master.kernel.org:/pub/scm/git/git.git/
1279
1280

PACKING YOUR REPOSITORY

1282       Earlier, we saw that one file under .git/objects/??/ directory is
1283       stored for each git object you create. This representation is efficient
1284       to create atomically and safely, but not so convenient to transport
1285       over the network. Since git objects are immutable once they are
1286       created, there is a way to optimize the storage by "packing them
1287       together". The command
1288
1289           $ git repack
1290
1291
1292       will do it for you. If you followed the tutorial examples, you would
1293       have accumulated about 17 objects in .git/objects/??/ directories by
1294       now. git repack tells you how many objects it packed, and stores the
1295       packed file in .git/objects/pack directory.
1296
1297           Note
1298           You will see two files, pack-*.pack and pack-*.idx, in
1299           .git/objects/pack directory. They are closely related to each
1300           other, and if you ever copy them by hand to a different repository
1301           for whatever reason, you should make sure you copy them together.
1302           The former holds all the data from the objects in the pack, and the
1303           latter holds the index for random access.
1304
1305       If you are paranoid, running git verify-pack command would detect if
1306       you have a corrupt pack, but do not worry too much. Our programs are
1307       always perfect ;-).
1308
1309       Once you have packed objects, you do not need to leave the unpacked
1310       objects that are contained in the pack file anymore.
1311
1312           $ git prune-packed
1313
1314
1315       would remove them for you.
1316
1317       You can try running find .git/objects -type f before and after you run
1318       git prune-packed if you are curious. Also git count-objects would tell
1319       you how many unpacked objects are in your repository and how much space
1320       they are consuming.
1321
1322           Note
1323           git pull is slightly cumbersome for HTTP transport, as a packed
1324           repository may contain relatively few objects in a relatively large
1325           pack. If you expect many HTTP pulls from your public repository you
1326           might want to repack & prune often, or never.
1327
1328       If you run git repack again at this point, it will say "Nothing new to
1329       pack.". Once you continue your development and accumulate the changes,
1330       running git repack again will create a new pack, that contains objects
1331       created since you packed your repository the last time. We recommend
1332       that you pack your project soon after the initial import (unless you
1333       are starting your project from scratch), and then run git repack every
1334       once in a while, depending on how active your project is.
1335
1336       When a repository is synchronized via git push and git pull objects
1337       packed in the source repository are usually stored unpacked in the
1338       destination, unless rsync transport is used. While this allows you to
1339       use different packing strategies on both ends, it also means you may
1340       need to repack both repositories every once in a while.
1341

WORKING WITH OTHERS

1343       Although git is a truly distributed system, it is often convenient to
1344       organize your project with an informal hierarchy of developers. Linux
1345       kernel development is run this way. There is a nice illustration (page
1346       17, "Merges to Mainline") in Randy Dunlap’s presentation[2].
1347
1348       It should be stressed that this hierarchy is purely informal. There is
1349       nothing fundamental in git that enforces the "chain of patch flow" this
1350       hierarchy implies. You do not have to pull from only one remote
1351       repository.
1352
1353       A recommended workflow for a "project lead" goes like this:
1354
1355        1. Prepare your primary repository on your local machine. Your work is
1356           done there.
1357
1358        2. Prepare a public repository accessible to others.
1359
1360           If other people are pulling from your repository over dumb
1361           transport protocols (HTTP), you need to keep this repository dumb
1362           transport friendly. After git init,
1363           $GIT_DIR/hooks/post-update.sample copied from the standard
1364           templates would contain a call to git update-server-info but you
1365           need to manually enable the hook with mv post-update.sample
1366           post-update. This makes sure git update-server-info keeps the
1367           necessary files up-to-date.
1368
1369        3. Push into the public repository from your primary repository.
1370
1371        4.  git repack the public repository. This establishes a big pack that
1372           contains the initial set of objects as the baseline, and possibly
1373           git prune if the transport used for pulling from your repository
1374           supports packed repositories.
1375
1376        5. Keep working in your primary repository. Your changes include
1377           modifications of your own, patches you receive via e-mails, and
1378           merges resulting from pulling the "public" repositories of your
1379           "subsystem maintainers".
1380
1381           You can repack this private repository whenever you feel like.
1382
1383        6. Push your changes to the public repository, and announce it to the
1384           public.
1385
1386        7. Every once in a while, git repack the public repository. Go back to
1387           step 5. and continue working.
1388
1389       A recommended work cycle for a "subsystem maintainer" who works on that
1390       project and has an own "public repository" goes like this:
1391
1392        1. Prepare your work repository, by git clone the public repository of
1393           the "project lead". The URL used for the initial cloning is stored
1394           in the remote.origin.url configuration variable.
1395
1396        2. Prepare a public repository accessible to others, just like the
1397           "project lead" person does.
1398
1399        3. Copy over the packed files from "project lead" public repository to
1400           your public repository, unless the "project lead" repository lives
1401           on the same machine as yours. In the latter case, you can use
1402           objects/info/alternates file to point at the repository you are
1403           borrowing from.
1404
1405        4. Push into the public repository from your primary repository. Run
1406           git repack, and possibly git prune if the transport used for
1407           pulling from your repository supports packed repositories.
1408
1409        5. Keep working in your primary repository. Your changes include
1410           modifications of your own, patches you receive via e-mails, and
1411           merges resulting from pulling the "public" repositories of your
1412           "project lead" and possibly your "sub-subsystem maintainers".
1413
1414           You can repack this private repository whenever you feel like.
1415
1416        6. Push your changes to your public repository, and ask your "project
1417           lead" and possibly your "sub-subsystem maintainers" to pull from
1418           it.
1419
1420        7. Every once in a while, git repack the public repository. Go back to
1421           step 5. and continue working.
1422
1423       A recommended work cycle for an "individual developer" who does not
1424       have a "public" repository is somewhat different. It goes like this:
1425
1426        1. Prepare your work repository, by git clone the public repository of
1427           the "project lead" (or a "subsystem maintainer", if you work on a
1428           subsystem). The URL used for the initial cloning is stored in the
1429           remote.origin.url configuration variable.
1430
1431        2. Do your work in your repository on master branch.
1432
1433        3. Run git fetch origin from the public repository of your upstream
1434           every once in a while. This does only the first half of git pull
1435           but does not merge. The head of the public repository is stored in
1436           .git/refs/remotes/origin/master.
1437
1438        4. Use git cherry origin to see which ones of your patches were
1439           accepted, and/or use git rebase origin to port your unmerged
1440           changes forward to the updated upstream.
1441
1442        5. Use git format-patch origin to prepare patches for e-mail
1443           submission to your upstream and send it out. Go back to step 2. and
1444           continue.
1445

WORKING WITH OTHERS, SHARED REPOSITORY STYLE

1447       If you are coming from CVS background, the style of cooperation
1448       suggested in the previous section may be new to you. You do not have to
1449       worry. git supports "shared public repository" style of cooperation you
1450       are probably more familiar with as well.
1451
1452       See gitcvs-migration(7) for the details.
1453

BUNDLING YOUR WORK TOGETHER

1455       It is likely that you will be working on more than one thing at a time.
1456       It is easy to manage those more-or-less independent tasks using
1457       branches with git.
1458
1459       We have already seen how branches work previously, with "fun and work"
1460       example using two branches. The idea is the same if there are more than
1461       two branches. Let’s say you started out from "master" head, and have
1462       some new code in the "master" branch, and two independent fixes in the
1463       "commit-fix" and "diff-fix" branches:
1464
1465           $ git show-branch
1466           ! [commit-fix] Fix commit message normalization.
1467            ! [diff-fix] Fix rename detection.
1468             * [master] Release candidate #1
1469           ---
1470            +  [diff-fix] Fix rename detection.
1471            +  [diff-fix~1] Better common substring algorithm.
1472           +   [commit-fix] Fix commit message normalization.
1473             * [master] Release candidate #1
1474           ++* [diff-fix~2] Pretty-print messages.
1475
1476
1477       Both fixes are tested well, and at this point, you want to merge in
1478       both of them. You could merge in diff-fix first and then commit-fix
1479       next, like this:
1480
1481           $ git merge -m "Merge fix in diff-fix" diff-fix
1482           $ git merge -m "Merge fix in commit-fix" commit-fix
1483
1484
1485       Which would result in:
1486
1487           $ git show-branch
1488           ! [commit-fix] Fix commit message normalization.
1489            ! [diff-fix] Fix rename detection.
1490             * [master] Merge fix in commit-fix
1491           ---
1492             - [master] Merge fix in commit-fix
1493           + * [commit-fix] Fix commit message normalization.
1494             - [master~1] Merge fix in diff-fix
1495            +* [diff-fix] Fix rename detection.
1496            +* [diff-fix~1] Better common substring algorithm.
1497             * [master~2] Release candidate #1
1498           ++* [master~3] Pretty-print messages.
1499
1500
1501       However, there is no particular reason to merge in one branch first and
1502       the other next, when what you have are a set of truly independent
1503       changes (if the order mattered, then they are not independent by
1504       definition). You could instead merge those two branches into the
1505       current branch at once. First let’s undo what we just did and start
1506       over. We would want to get the master branch before these two merges by
1507       resetting it to master~2:
1508
1509           $ git reset --hard master~2
1510
1511
1512       You can make sure git show-branch matches the state before those two
1513       git merge you just did. Then, instead of running two git merge commands
1514       in a row, you would merge these two branch heads (this is known as
1515       making an Octopus):
1516
1517           $ git merge commit-fix diff-fix
1518           $ git show-branch
1519           ! [commit-fix] Fix commit message normalization.
1520            ! [diff-fix] Fix rename detection.
1521             * [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
1522           ---
1523             - [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
1524           + * [commit-fix] Fix commit message normalization.
1525            +* [diff-fix] Fix rename detection.
1526            +* [diff-fix~1] Better common substring algorithm.
1527             * [master~1] Release candidate #1
1528           ++* [master~2] Pretty-print messages.
1529
1530
1531       Note that you should not do Octopus because you can. An octopus is a
1532       valid thing to do and often makes it easier to view the commit history
1533       if you are merging more than two independent changes at the same time.
1534       However, if you have merge conflicts with any of the branches you are
1535       merging in and need to hand resolve, that is an indication that the
1536       development happened in those branches were not independent after all,
1537       and you should merge two at a time, documenting how you resolved the
1538       conflicts, and the reason why you preferred changes made in one side
1539       over the other. Otherwise it would make the project history harder to
1540       follow, not easier.
1541

SEE ALSO

1543       gittutorial(7), gittutorial-2(7), gitcvs-migration(7), git-help(1),
1544       Everyday git[3], The Git User’s Manual[1]
1545

GIT

1547       Part of the git(1) suite.
1548

NOTES

1550        1. the GIT User Manual
1551           file:///usr/share/doc/git-1.7.4.4/user-manual.html
1552
1553        2. Randy Dunlap’s presentation
1554           http://www.xenotime.net/linux/mentor/linux-mentoring-2006.pdf
1555
1556        3. Everyday git
1557           file:///usr/share/doc/git-1.7.4.4/everyday.html
1558
1559
1560
1561Git 1.7.4.4                       04/11/2011               GITCORE-TUTORIAL(7)
Impressum