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 SHA-1 hash, aka object name,
99           and a reference to an object is always the 40-byte hex
100           representation of that SHA-1 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 SHA-1 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 with
879       the titles of their top-of-the-tree commits, you are currently on
880       master branch (notice the asterisk * character), and the first column
881       for the later output lines is used to show commits contained in the
882       master branch, and the second column for the mybranch branch. Three
883       commits are shown along with their titles. All of them have non blank
884       characters in the first column (* shows an ordinary commit on the
885       current branch, - is a merge commit), which means they are now part of
886       the master branch. Only the "Some work" commit has the plus + character
887       in the second column, because mybranch has not been merged to
888       incorporate these commits from the master branch. The string inside
889       brackets before the commit log message is a short name you can use to
890       name the commit. In the above example, master and mybranch are branch
891       heads. master^ is the first parent of master branch head. Please see
892       gitrevisions(7) if you want to see more complex cases.
893
894           Note
895           Without the --more=1 option, git show-branch would not output the
896           [master^] commit, as [mybranch] commit is a common ancestor of both
897           master and mybranch tips. Please see git-show-branch(1) for
898           details.
899
900           Note
901           If there were more commits on the master branch after the merge,
902           the merge commit itself would not be shown by git show-branch by
903           default. You would need to provide --sparse option to make the
904           merge commit visible in this case.
905
906       Now, let’s pretend you are the one who did all the work in mybranch,
907       and the fruit of your hard work has finally been merged to the master
908       branch. Let’s go back to mybranch, and run git merge to get the
909       "upstream changes" back to your branch.
910
911           $ git checkout mybranch
912           $ git merge -m "Merge upstream changes." master
913
914
915       This outputs something like this (the actual commit object names would
916       be different)
917
918           Updating from ae3a2da... to a80b4aa....
919           Fast-forward (no commit created; -m option ignored)
920            example | 1 +
921            hello   | 1 +
922            2 files changed, 2 insertions(+)
923
924
925       Because your branch did not contain anything more than what had already
926       been merged into the master branch, the merge operation did not
927       actually do a merge. Instead, it just updated the top of the tree of
928       your branch to that of the master branch. This is often called
929       fast-forward merge.
930
931       You can run gitk --all again to see how the commit ancestry looks like,
932       or run show-branch, which tells you this.
933
934           $ git show-branch master mybranch
935           ! [master] Merge work in mybranch
936            * [mybranch] Merge work in mybranch
937           --
938           -- [master] Merge work in mybranch
939
940

MERGING EXTERNAL WORK

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

HOW DOES THE MERGE WORK?

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

PUBLISHING YOUR WORK

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

PACKING YOUR REPOSITORY

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

WORKING WITH OTHERS

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

WORKING WITH OTHERS, SHARED REPOSITORY STYLE

1441       If you are coming from CVS background, the style of cooperation
1442       suggested in the previous section may be new to you. You do not have to
1443       worry. Git supports "shared public repository" style of cooperation you
1444       are probably more familiar with as well.
1445
1446       See gitcvs-migration(7) for the details.
1447

BUNDLING YOUR WORK TOGETHER

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

SEE ALSO

1537       gittutorial(7), gittutorial-2(7), gitcvs-migration(7), git-help(1),
1538       Everyday git[3], The Git User’s Manual[1]
1539

GIT

1541       Part of the git(1) suite.
1542

NOTES

1544        1. the Git User Manual
1545           file:///usr/share/doc/git-1.8.3.1/user-manual.html
1546
1547        2. Randy Dunlap’s presentation
1548           http://www.xenotime.net/linux/mentor/linux-mentoring-2006.pdf
1549
1550        3. Everyday git
1551           file:///usr/share/doc/git-1.8.3.1/everyday.html
1552
1553
1554
1555Git 1.8.3.1                       11/19/2018               GITCORE-TUTORIAL(7)
Impressum