interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / gitcore-tutorial.adoc

1655 lines · 62189 bytes Raw Blame History
1gitcore-tutorial(7)
2===================
3
4NAME
5----
6gitcore-tutorial - A Git core tutorial for developers
7
8DESCRIPTION
9-----------
10
11This tutorial explains how to use the "core" Git commands to set up and
12work with a Git repository.
13
14If you just need to use Git as a revision control system you may prefer
15to start with "A Tutorial Introduction to Git" (linkgit:gittutorial[7]) or
16link:user-manual.html[the Git User Manual].
17
18However, an understanding of these low-level tools can be helpful if
19you want to understand Git's internals.
20
21The core Git is often called "plumbing", with the prettier user
22interfaces on top of it called "porcelain". You may not want to use the
23plumbing directly very often, but it can be good to know what the
24plumbing does when the porcelain isn't flushing.
25
26Back when this document was originally written, many porcelain
27commands were shell scripts. For simplicity, it still uses them as
28examples to illustrate how plumbing is fit together to form the
29porcelain commands. The source tree includes some of these scripts in
30contrib/examples/ for reference. Although these are not implemented as
31shell scripts anymore, the description of what the plumbing layer
32commands do is still valid.
33
34[NOTE]
35Deeper technical details are often marked as Notes, which you can
36skip on your first reading.
37
38
39Creating a Git repository
40-------------------------
41
42Creating a new Git repository couldn't be easier: all Git repositories start
43out empty, and the only thing you need to do is find yourself a
44subdirectory that you want to use as a working tree - either an empty
45one for a totally new project, or an existing working tree that you want
46to import into Git.
47
48For our first example, we're going to start a totally new repository from
49scratch, with no pre-existing files, and we'll call it 'git-tutorial'.
50To start up, create a subdirectory for it, change into that
51subdirectory, and initialize the Git infrastructure with 'git init':
52
53------------------------------------------------
54$ mkdir git-tutorial
55$ cd git-tutorial
56$ git init
57------------------------------------------------
58
59to which Git will reply
60
61----------------
62Initialized empty Git repository in .git/
63----------------
64
65which is just Git's way of saying that you haven't been doing anything
66strange, and that it will have created a local `.git` directory setup for
67your new project. You will now have a `.git` directory, and you can
68inspect that with 'ls'. For your new empty project, it should show you
69three entries, among other things:
70
71 - a file called `HEAD`, that has `ref: refs/heads/master` in it.
72 This is similar to a symbolic link and points at
73 `refs/heads/master` relative to the `HEAD` file.
74+
75Don't worry about the fact that the file that the `HEAD` link points to
76doesn't even exist yet -- you haven't created the commit that will
77start your `HEAD` development branch yet.
78
79 - a subdirectory called `objects`, which will contain all the
80 objects of your project. You should never have any real reason to
81 look at the objects directly, but you might want to know that these
82 objects are what contains all the real 'data' in your repository.
83
84 - a subdirectory called `refs`, which contains references to objects.
85
86In particular, the `refs` subdirectory will contain two other
87subdirectories, named `heads` and `tags` respectively. They do
88exactly what their names imply: they contain references to any number
89of different 'heads' of development (aka 'branches'), and to any
90'tags' that you have created to name specific versions in your
91repository.
92
93One note: the special `master` head is the default branch, which is
94why the `.git/HEAD` file was created points to it even if it
95doesn't yet exist. Basically, the `HEAD` link is supposed to always
96point to the branch you are working on right now, and you always
97start out expecting to work on the `master` branch.
98
99However, this is only a convention, and you can name your branches
100anything you want, and don't have to ever even 'have' a `master`
101branch. A number of the Git tools will assume that `.git/HEAD` is
102valid, though.
103
104[NOTE]
105An 'object' is identified by its 160-bit SHA-1 hash, aka 'object name',
106and a reference to an object is always the 40-byte hex
107representation of that SHA-1 name. The files in the `refs`
108subdirectory are expected to contain these hex references
109(usually with a final `\n` at the end), and you should thus
110expect to see a number of 41-byte files containing these
111references in these `refs` subdirectories when you actually start
112populating your tree.
113
114[NOTE]
115An advanced user may want to take a look at linkgit:gitrepository-layout[5]
116after finishing this tutorial.
117
118You have now created your first Git repository. Of course, since it's
119empty, that's not very useful, so let's start populating it with data.
120
121
122Populating a Git repository
123---------------------------
124
125We'll keep this simple and stupid, so we'll start off with populating a
126few trivial files just to get a feel for it.
127
128Start off with just creating any random files that you want to maintain
129in your Git repository. We'll start off with a few bad examples, just to
130get a feel for how this works:
131
132------------------------------------------------
133$ echo "Hello World" >hello
134$ echo "Silly example" >example
135------------------------------------------------
136
137you have now created two files in your working tree (aka 'working directory'),
138but to actually check in your hard work, you will have to go through two steps:
139
140 - fill in the 'index' file (aka 'cache') with the information about your
141 working tree state.
142
143 - commit that index file as an object.
144
145The first step is trivial: when you want to tell Git about any changes
146to your working tree, you use the 'git update-index' program. That
147program normally just takes a list of filenames you want to update, but
148to avoid trivial mistakes, it refuses to add new entries to the index
149(or remove existing ones) unless you explicitly tell it that you're
150adding a new entry with the `--add` flag (or removing an entry with the
151`--remove`) flag.
152
153So to populate the index with the two files you just created, you can do
154
155------------------------------------------------
156$ git update-index --add hello example
157------------------------------------------------
158
159and you have now told Git to track those two files.
160
161In fact, as you did that, if you now look into your object directory,
162you'll notice that Git will have added two new objects to the object
163database. If you did exactly the steps above, you should now be able to do
164
165
166----------------
167$ ls .git/objects/??/*
168----------------
169
170and see two files:
171
172----------------
173.git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238
174.git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962
175----------------
176
177which correspond with the objects with names of `557db...` and
178`f24c7...` respectively.
179
180If you want to, you can use 'git cat-file' to look at those objects, but
181you'll have to use the object name, not the filename of the object:
182
183----------------
184$ git cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238
185----------------
186
187where the `-t` tells 'git cat-file' to tell you what the "type" of the
188object is. Git will tell you that you have a "blob" object (i.e., just a
189regular file), and you can see the contents with
190
191----------------
192$ git cat-file blob 557db03
193----------------
194
195which will print out "Hello World". The object `557db03` is nothing
196more than the contents of your file `hello`.
197
198[NOTE]
199Don't confuse that object with the file `hello` itself. The
200object is literally just those specific *contents* of the file, and
201however much you later change the contents in file `hello`, the object
202we just looked at will never change. Objects are immutable.
203
204[NOTE]
205The second example demonstrates that you can
206abbreviate the object name to only the first several
207hexadecimal digits in most places.
208
209Anyway, as we mentioned previously, you normally never actually take a
210look at the objects themselves, and typing long 40-character hex
211names is not something you'd normally want to do. The above digression
212was just to show that 'git update-index' did something magical, and
213actually saved away the contents of your files into the Git object
214database.
215
216Updating the index did something else too: it created a `.git/index`
217file. This is the index that describes your current working tree, and
218something you should be very aware of. Again, you normally never worry
219about the index file itself, but you should be aware of the fact that
220you have not actually really "checked in" your files into Git so far,
221you've only *told* Git about them.
222
223However, since Git knows about them, you can now start using some of the
224most basic Git commands to manipulate the files or look at their status.
225
226In particular, let's not even check in the two files into Git yet, we'll
227start off by adding another line to `hello` first:
228
229------------------------------------------------
230$ echo "It's a new day for git" >>hello
231------------------------------------------------
232
233and you can now, since you told Git about the previous state of `hello`, ask
234Git what has changed in the tree compared to your old index, using the
235'git diff-files' command:
236
237------------
238$ git diff-files
239------------
240
241Oops. That wasn't very readable. It just spit out its own internal
242version of a 'diff', but that internal version really just tells you
243that it has noticed that "hello" has been modified, and that the old object
244contents it had have been replaced with something else.
245
246To make it readable, we can tell 'git diff-files' to output the
247differences as a patch, using the `-p` flag:
248
249------------
250$ git diff-files -p
251diff --git a/hello b/hello
252index 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
260i.e. the diff of the change we caused by adding another line to `hello`.
261
262In other words, 'git diff-files' always shows us the difference between
263what is recorded in the index, and what is currently in the working
264tree. That's very useful.
265
266A common shorthand for `git diff-files -p` is to just write `git
267diff`, which will do the same thing.
268
269------------
270$ git diff
271diff --git a/hello b/hello
272index 557db03..263414f 100644
273--- a/hello
274+++ b/hello
275@@ -1 +1,2 @@
276 Hello World
277+It's a new day for git
278------------
279
280
281Committing Git state
282--------------------
283
284Now, we want to go to the next stage in Git, which is to take the files
285that Git knows about in the index, and commit them as a real tree. We do
286that in two phases: creating a 'tree' object, and committing that 'tree'
287object as a 'commit' object together with an explanation of what the
288tree was all about, along with information of how we came to that state.
289
290Creating a tree object is trivial, and is done with 'git write-tree'.
291There are no options or other input: `git write-tree` will take the
292current index state, and write an object that describes that whole
293index. In other words, we're now tying together all the different
294filenames with their contents (and their permissions), and we're
295creating the equivalent of a Git "directory" object:
296
297------------------------------------------------
298$ git write-tree
299------------------------------------------------
300
301and this will just output the name of the resulting tree, in this case
302(if you have done exactly as I've described) it should be
303
304----------------
3058988da15d077d4829fc51d8544c097def6644dbb
306----------------
307
308which is another incomprehensible object name. Again, if you want to,
309you can use `git cat-file -t 8988d...` to see that this time the object
310is not a "blob" object, but a "tree" object (you can also use
311`git cat-file` to actually output the raw object contents, but you'll see
312mainly a binary mess, so that's less interesting).
313
314However -- normally you'd never use 'git write-tree' on its own, because
315normally you always commit a tree into a commit object using the
316'git commit-tree' command. In fact, it's easier to not actually use
317'git write-tree' on its own at all, but to just pass its result in as an
318argument to 'git commit-tree'.
319
320'git commit-tree' normally takes several arguments -- it wants to know
321what the 'parent' of a commit was, but since this is the first commit
322ever in this new repository, and it has no parents, we only need to pass in
323the object name of the tree. However, 'git commit-tree' also wants to get a
324commit message on its standard input, and it will write out the resulting
325object name for the commit to its standard output.
326
327And this is where we create the `.git/refs/heads/master` file
328which is pointed at by `HEAD`. This file is supposed to contain
329the reference to the top-of-tree of the master branch, and since
330that's exactly what 'git commit-tree' spits out, we can do this
331all with a sequence of simple shell commands:
332
333------------------------------------------------
334$ tree=$(git write-tree)
335$ commit=$(echo 'Initial commit' | git commit-tree $tree)
336$ git update-ref HEAD $commit
337------------------------------------------------
338
339In this case this creates a totally new commit that is not related to
340anything else. Normally you do this only *once* for a project ever, and
341all later commits will be parented on top of an earlier commit.
342
343Again, normally you'd never actually do this by hand. There is a
344helpful script called `git commit` that will do all of this for you. So
345you could have just written `git commit`
346instead, and it would have done the above magic scripting for you.
347
348
349Making a change
350---------------
351
352Remember how we did the 'git update-index' on file `hello` and then we
353changed `hello` afterward, and could compare the new state of `hello` with the
354state we saved in the index file?
355
356Further, remember how I said that 'git write-tree' writes the contents
357of the *index* file to the tree, and thus what we just committed was in
358fact the *original* contents of the file `hello`, not the new ones. We did
359that on purpose, to show the difference between the index state, and the
360state in the working tree, and how they don't have to match, even
361when we commit things.
362
363As before, if we do `git diff-files -p` in our git-tutorial project,
364we'll still see the same difference we saw last time: the index file
365hasn't changed by the act of committing anything. However, now that we
366have committed something, we can also learn to use a new command:
367'git diff-index'.
368
369Unlike 'git diff-files', which showed the difference between the index
370file and the working tree, 'git diff-index' shows the differences
371between a committed *tree* and either the index file or the working
372tree. In other words, 'git diff-index' wants a tree to be diffed
373against, and before we did the commit, we couldn't do that, because we
374didn't have anything to diff against.
375
376But now we can do
377
378----------------
379$ git diff-index -p HEAD
380----------------
381
382(where `-p` has the same meaning as it did in 'git diff-files'), and it
383will show us the same difference, but for a totally different reason.
384Now we're comparing the working tree not against the index file,
385but against the tree we just wrote. It just so happens that those two
386are obviously the same, so we get the same result.
387
388Again, because this is a common operation, you can also just shorthand
389it with
390
391----------------
392$ git diff HEAD
393----------------
394
395which ends up doing the above for you.
396
397In other words, 'git diff-index' normally compares a tree against the
398working tree, but when given the `--cached` flag, it is told to
399instead compare against just the index cache contents, and ignore the
400current working tree state entirely. Since we just wrote the index
401file to HEAD, doing `git diff-index --cached -p HEAD` should thus return
402an empty set of differences, and that's exactly what it does.
403
404[NOTE]
405================
406'git diff-index' really always uses the index for its
407comparisons, and saying that it compares a tree against the working
408tree is thus not strictly accurate. In particular, the list of
409files to compare (the "meta-data") *always* comes from the index file,
410regardless of whether the `--cached` flag is used or not. The `--cached`
411flag really only determines whether the file *contents* to be compared
412come from the working tree or not.
413
414This is not hard to understand, as soon as you realize that Git simply
415never knows (or cares) about files that it is not told about
416explicitly. Git will never go *looking* for files to compare, it
417expects you to tell it what the files are, and that's what the index
418is there for.
419================
420
421However, our next step is to commit the *change* we did, and again, to
422understand what's going on, keep in mind the difference between "working
423tree contents", "index file" and "committed tree". We have changes
424in the working tree that we want to commit, and we always have to
425work through the index file, so the first thing we need to do is to
426update the index cache:
427
428------------------------------------------------
429$ git update-index hello
430------------------------------------------------
431
432(note how we didn't need the `--add` flag this time, since Git knew
433about the file already).
434
435Note what happens to the different 'git diff-{asterisk}' versions here.
436After we've updated `hello` in the index, `git diff-files -p` now shows no
437differences, but `git diff-index -p HEAD` still *does* show that the
438current state is different from the state we committed. In fact, now
439'git diff-index' shows the same difference whether we use the `--cached`
440flag or not, since now the index is coherent with the working tree.
441
442Now, since we've updated `hello` in the index, we can commit the new
443version. We could do it by writing the tree by hand again, and
444committing the tree (this time we'd have to use the `-p HEAD` flag to
445tell commit that the HEAD was the *parent* of the new commit, and that
446this wasn't an initial commit any more), but you've done that once
447already, so let's just use the helpful script this time:
448
449------------------------------------------------
450$ git commit
451------------------------------------------------
452
453which starts an editor for you to write the commit message and tells you
454a bit about what you have done.
455
456Write whatever message you want, and all the lines that start with '#'
457will be pruned out, and the rest will be used as the commit message for
458the change. If you decide you don't want to commit anything after all at
459this point (you can continue to edit things and update the index), you
460can just leave an empty message. Otherwise `git commit` will commit
461the change for you.
462
463You've now made your first real Git commit. And if you're interested in
464looking at what `git commit` really does, feel free to investigate:
465it's a few very simple shell scripts to generate the helpful (?) commit
466message headers, and a few one-liners that actually do the
467commit itself ('git commit').
468
469
470Inspecting Changes
471------------------
472
473While creating changes is useful, it's even more useful if you can tell
474later what changed. The most useful command for this is another of the
475'diff' family, namely 'git diff-tree'.
476
477'git diff-tree' can be given two arbitrary trees, and it will tell you the
478differences between them. Perhaps even more commonly, though, you can
479give it just a single commit object, and it will figure out the parent
480of that commit itself, and show the difference directly. Thus, to get
481the same diff that we've already seen several times, we can now do
482
483----------------
484$ git diff-tree -p HEAD
485----------------
486
487(again, `-p` means to show the difference as a human-readable patch),
488and it will show what the last commit (in `HEAD`) actually changed.
489
490[NOTE]
491============
492Here is an ASCII art by Jon Loeliger that illustrates how
493various 'diff-{asterisk}' commands compare things.
494
495 diff-tree
496 +----+
497 | |
498 | |
499 V V
500 +-----------+
501 | Object DB |
502 | Backing |
503 | Store |
504 +-----------+
505 ^ ^
506 | |
507 | | diff-index --cached
508 | |
509 diff-index | V
510 | +-----------+
511 | | Index |
512 | | "cache" |
513 | +-----------+
514 | ^
515 | |
516 | | diff-files
517 | |
518 V V
519 +-----------+
520 | Working |
521 | Directory |
522 +-----------+
523============
524
525More interestingly, you can also give 'git diff-tree' the `--pretty` flag,
526which tells it to also show the commit message and author and date of the
527commit, and you can tell it to show a whole series of diffs.
528Alternatively, you can tell it to be "silent", and not show the diffs at
529all, but just show the actual commit message.
530
531In fact, together with the 'git rev-list' program (which generates a
532list of revisions), 'git diff-tree' ends up being a veritable fount of
533changes. You can emulate `git log`, `git log -p`, etc. with a trivial
534script that pipes the output of `git rev-list` to `git diff-tree --stdin`,
535which was exactly how early versions of `git log` were implemented.
536
537
538Tagging a version
539-----------------
540
541In Git, there are two kinds of tags, a "light" one, and an "annotated tag".
542
543A "light" tag is technically nothing more than a branch, except we put
544it in the `.git/refs/tags/` subdirectory instead of calling it a `head`.
545So the simplest form of tag involves nothing more than
546
547------------------------------------------------
548$ git tag my-first-tag
549------------------------------------------------
550
551which just writes the current `HEAD` into the `.git/refs/tags/my-first-tag`
552file, after which point you can then use this symbolic name for that
553particular state. You can, for example, do
554
555----------------
556$ git diff my-first-tag
557----------------
558
559to diff your current state against that tag which at this point will
560obviously be an empty diff, but if you continue to develop and commit
561stuff, you can use your tag as an "anchor-point" to see what has changed
562since you tagged it.
563
564An "annotated tag" is actually a real Git object, and contains not only a
565pointer to the state you want to tag, but also a small tag name and
566message, along with optionally a PGP signature that says that yes,
567you really did
568that tag. You create these annotated tags with either the `-a` or
569`-s` flag to 'git tag':
570
571----------------
572$ git tag -s <tagname>
573----------------
574
575which will sign the current `HEAD` (but you can also give it another
576argument that specifies the thing to tag, e.g., you could have tagged the
577current `mybranch` point by using `git tag <tagname> mybranch`).
578
579You normally only do signed tags for major releases or things
580like that, while the light-weight tags are useful for any marking you
581want to do -- any time you decide that you want to remember a certain
582point, just create a private tag for it, and you have a nice symbolic
583name for the state at that point.
584
585
586Copying repositories
587--------------------
588
589Git repositories are normally totally self-sufficient and relocatable.
590Unlike CVS, for example, there is no separate notion of
591"repository" and "working tree". A Git repository normally *is* the
592working tree, with the local Git information hidden in the `.git`
593subdirectory. There is nothing else. What you see is what you got.
594
595[NOTE]
596You can tell Git to split the Git internal information from
597the directory that it tracks, but we'll ignore that for now: it's not
598how normal projects work, and it's really only meant for special uses.
599So the mental model of "the Git information is always tied directly to
600the working tree that it describes" may not be technically 100%
601accurate, but it's a good model for all normal use.
602
603This has two implications:
604
605 - if you grow bored with the tutorial repository you created (or you've
606 made a mistake and want to start all over), you can just do simple
607+
608----------------
609$ rm -rf git-tutorial
610----------------
611+
612and it will be gone. There's no external repository, and there's no
613history outside the project you created.
614
615 - if you want to move or duplicate a Git repository, you can do so. There
616 is 'git clone' command, but if all you want to do is just to
617 create a copy of your repository (with all the full history that
618 went along with it), you can do so with a regular
619 `cp -a git-tutorial new-git-tutorial`.
620+
621Note that when you've moved or copied a Git repository, your Git index
622file (which caches various information, notably some of the "stat"
623information for the files involved) will likely need to be refreshed.
624So after you do a `cp -a` to create a new copy, you'll want to do
625+
626----------------
627$ git update-index --refresh
628----------------
629+
630in the new repository to make sure that the index file is up to date.
631
632Note that the second point is true even across machines. You can
633duplicate a remote Git repository with *any* regular copy mechanism, be it
634'scp', 'rsync' or 'wget'.
635
636When copying a remote repository, you'll want to at a minimum update the
637index cache when you do this, and especially with other peoples'
638repositories you often want to make sure that the index cache is in some
639known state (you don't know *what* they've done and not yet checked in),
640so usually you'll precede the 'git update-index' with a
641
642----------------
643$ git read-tree --reset HEAD
644$ git update-index --refresh
645----------------
646
647which will force a total index re-build from the tree pointed to by `HEAD`.
648It resets the index contents to `HEAD`, and then the 'git update-index'
649makes sure to match up all index entries with the checked-out files.
650If the original repository had uncommitted changes in its
651working tree, `git update-index --refresh` notices them and
652tells you they need to be updated.
653
654The above can also be written as simply
655
656----------------
657$ git reset
658----------------
659
660and in fact a lot of the common Git command combinations can be scripted
661with the `git xyz` interfaces. You can learn things by just looking
662at what the various git scripts do. For example, `git reset` used to be
663the above two lines implemented in 'git reset', but some things like
664'git status' and 'git commit' are slightly more complex scripts around
665the basic Git commands.
666
667Many (most?) public remote repositories will not contain any of
668the checked out files or even an index file, and will *only* contain the
669actual core Git files. Such a repository usually doesn't even have the
670`.git` subdirectory, but has all the Git files directly in the
671repository.
672
673To create your own local live copy of such a "raw" Git repository, you'd
674first create your own subdirectory for the project, and then copy the
675raw repository contents into the `.git` directory. For example, to
676create your own copy of the Git repository, you'd do the following
677
678----------------
679$ mkdir my-git
680$ cd my-git
681$ rsync -rL rsync://rsync.kernel.org/pub/scm/git/git.git/ .git
682----------------
683
684followed by
685
686----------------
687$ git read-tree HEAD
688----------------
689
690to populate the index. However, now you have populated the index, and
691you have all the Git internal files, but you will notice that you don't
692actually have any of the working tree files to work on. To get
693those, you'd check them out with
694
695----------------
696$ git checkout-index -u -a
697----------------
698
699where the `-u` flag means that you want the checkout to keep the index
700up to date (so that you don't have to refresh it afterward), and the
701`-a` flag means "check out all files" (if you have a stale copy or an
702older version of a checked out tree you may also need to add the `-f`
703flag first, to tell 'git checkout-index' to *force* overwriting of any old
704files).
705
706Again, this can all be simplified with
707
708----------------
709$ git clone git://git.kernel.org/pub/scm/git/git.git/ my-git
710$ cd my-git
711$ git checkout
712----------------
713
714which will end up doing all of the above for you.
715
716You have now successfully copied somebody else's (mine) remote
717repository, and checked it out.
718
719
720Creating a new branch
721---------------------
722
723Branches in Git are really nothing more than pointers into the Git
724object database from within the `.git/refs/` subdirectory, and as we
725already discussed, the `HEAD` branch is nothing but a symlink to one of
726these object pointers.
727
728You can at any time create a new branch by just picking an arbitrary
729point in the project history, and just writing the SHA-1 name of that
730object into a file under `.git/refs/heads/`. You can use any filename you
731want (and indeed, subdirectories), but the convention is that the
732"normal" branch is called `master`. That's just a convention, though,
733and nothing enforces it.
734
735To show that as an example, let's go back to the git-tutorial repository we
736used earlier, and create a branch in it. You do that by simply just
737saying that you want to check out a new branch:
738
739------------
740$ git switch -c mybranch
741------------
742
743will create a new branch based at the current `HEAD` position, and switch
744to it.
745
746[NOTE]
747================================================
748If you make the decision to start your new branch at some
749other point in the history than the current `HEAD`, you can do so by
750just telling 'git switch' what the base of the checkout would be.
751In other words, if you have an earlier tag or branch, you'd just do
752
753------------
754$ git switch -c mybranch earlier-commit
755------------
756
757and it would create the new branch `mybranch` at the earlier commit,
758and check out the state at that time.
759================================================
760
761You can always just jump back to your original `master` branch by doing
762
763------------
764$ git switch master
765------------
766
767(or any other branch-name, for that matter) and if you forget which
768branch you happen to be on, a simple
769
770------------
771$ cat .git/HEAD
772------------
773
774will tell you where it's pointing. To get the list of branches
775you have, you can say
776
777------------
778$ git branch
779------------
780
781which used to be nothing more than a simple script around `ls .git/refs/heads`.
782There will be an asterisk in front of the branch you are currently on.
783
784Sometimes you may wish to create a new branch _without_ actually
785checking it out and switching to it. If so, just use the command
786
787------------
788$ git branch <branchname> [startingpoint]
789------------
790
791which will simply _create_ the branch, but will not do anything further.
792You can then later -- once you decide that you want to actually develop
793on that branch -- switch to that branch with a regular 'git switch'
794with the branchname as the argument.
795
796
797Merging two branches
798--------------------
799
800One of the ideas of having a branch is that you do some (possibly
801experimental) work in it, and eventually merge it back to the main
802branch. So assuming you created the above `mybranch` that started out
803being the same as the original `master` branch, let's make sure we're in
804that branch, and do some work there.
805
806------------------------------------------------
807$ git switch mybranch
808$ echo "Work, work, work" >>hello
809$ git commit -m "Some work." -i hello
810------------------------------------------------
811
812Here, we just added another line to `hello`, and we used a shorthand for
813doing both `git update-index hello` and `git commit` by just giving the
814filename directly to `git commit`, with an `-i` flag (it tells
815Git to 'include' that file in addition to what you have done to
816the index file so far when making the commit). The `-m` flag is to give the
817commit log message from the command line.
818
819Now, to make it a bit more interesting, let's assume that somebody else
820does some work in the original branch, and simulate that by going back
821to the master branch, and editing the same file differently there:
822
823------------
824$ git switch master
825------------
826
827Here, take a moment to look at the contents of `hello`, and notice how they
828don't contain the work we just did in `mybranch` -- because that work
829hasn't happened in the `master` branch at all. Then do
830
831------------
832$ echo "Play, play, play" >>hello
833$ echo "Lots of fun" >>example
834$ git commit -m "Some fun." -i hello example
835------------
836
837since the master branch is obviously in a much better mood.
838
839Now, you've got two branches, and you decide that you want to merge the
840work done. Before we do that, let's introduce a cool graphical tool that
841helps you view what's going on:
842
843----------------
844$ gitk --all
845----------------
846
847will show you graphically both of your branches (that's what the `--all`
848means: normally it will just show you your current `HEAD`) and their
849histories. You can also see exactly how they came to be from a common
850source.
851
852Anyway, let's exit 'gitk' (`^Q` or the File menu), and decide that we want
853to merge the work we did on the `mybranch` branch into the `master`
854branch (which is currently our `HEAD` too). To do that, there's a nice
855script called 'git merge', which wants to know which branches you want
856to resolve and what the merge is all about:
857
858------------
859$ git merge -m "Merge work in mybranch" mybranch
860------------
861
862where the first argument is going to be used as the commit message if
863the merge can be resolved automatically.
864
865Now, in this case we've intentionally created a situation where the
866merge will need to be fixed up by hand, though, so Git will do as much
867of it as it can automatically (which in this case is just merge the `example`
868file, which had no differences in the `mybranch` branch), and say:
869
870----------------
871 Auto-merging hello
872 CONFLICT (content): Merge conflict in hello
873 Automatic merge failed; fix conflicts and then commit the result.
874----------------
875
876It tells you that it did an "Automatic merge", which
877failed due to conflicts in `hello`.
878
879Not to worry. It left the (trivial) conflict in `hello` in the same form you
880should already be well used to if you've ever used CVS, so let's just
881open `hello` in our editor (whatever that may be), and fix it up somehow.
882I'd suggest just making it so that `hello` contains all four lines:
883
884------------
885Hello World
886It's a new day for git
887Play, play, play
888Work, work, work
889------------
890
891and once you're happy with your manual merge, just do a
892
893------------
894$ git commit -i hello
895------------
896
897which will very loudly warn you that you're now committing a merge
898(which is correct, so never mind), and you can write a small merge
899message about your adventures in 'git merge'-land.
900
901After you're done, start up `gitk --all` to see graphically what the
902history looks like. Notice that `mybranch` still exists, and you can
903switch to it, and continue to work with it if you want to. The
904`mybranch` branch will not contain the merge, but next time you merge it
905from the `master` branch, Git will know how you merged it, so you'll not
906have to do _that_ merge again.
907
908Another useful tool, especially if you do not always work in X-Window
909environment, is `git show-branch`.
910
911------------------------------------------------
912$ git show-branch --topo-order --more=1 master mybranch
913* [master] Merge work in mybranch
914 ! [mybranch] Some work.
915--
916- [master] Merge work in mybranch
917*+ [mybranch] Some work.
918* [master^] Some fun.
919------------------------------------------------
920
921The first two lines indicate that it is showing the two branches
922with the titles of their top-of-the-tree commits, you are currently on
923`master` branch (notice the asterisk `*` character), and the first
924column for the later output lines is used to show commits contained in the
925`master` branch, and the second column for the `mybranch`
926branch. Three commits are shown along with their titles.
927All of them have non blank characters in the first column (`*`
928shows an ordinary commit on the current branch, `-` is a merge commit), which
929means they are now part of the `master` branch. Only the "Some
930work" commit has the plus `+` character in the second column,
931because `mybranch` has not been merged to incorporate these
932commits from the master branch. The string inside brackets
933before the commit log message is a short name you can use to
934name the commit. In the above example, 'master' and 'mybranch'
935are branch heads. 'master^' is the first parent of 'master'
936branch head. Please see linkgit:gitrevisions[7] if you want to
937see more complex cases.
938
939[NOTE]
940Without the '--more=1' option, 'git show-branch' would not output the
941'[master^]' commit, as '[mybranch]' commit is a common ancestor of
942both 'master' and 'mybranch' tips. Please see linkgit:git-show-branch[1]
943for details.
944
945[NOTE]
946If there were more commits on the 'master' branch after the merge, the
947merge commit itself would not be shown by 'git show-branch' by
948default. You would need to provide `--sparse` option to make the
949merge commit visible in this case.
950
951Now, let's pretend you are the one who did all the work in
952`mybranch`, and the fruit of your hard work has finally been merged
953to the `master` branch. Let's go back to `mybranch`, and run
954'git merge' to get the "upstream changes" back to your branch.
955
956------------
957$ git switch mybranch
958$ git merge -m "Merge upstream changes." master
959------------
960
961This outputs something like this (the actual commit object names
962would be different)
963
964----------------
965Updating from ae3a2da... to a80b4aa....
966Fast-forward (no commit created; -m option ignored)
967 example | 1 +
968 hello | 1 +
969 2 files changed, 2 insertions(+)
970----------------
971
972Because your branch did not contain anything more than what had
973already been merged into the `master` branch, the merge operation did
974not actually do a merge. Instead, it just updated the top of
975the tree of your branch to that of the `master` branch. This is
976often called 'fast-forward' merge.
977
978You can run `gitk --all` again to see how the commit ancestry
979looks like, or run 'show-branch', which tells you this.
980
981------------------------------------------------
982$ git show-branch master mybranch
983! [master] Merge work in mybranch
984 * [mybranch] Merge work in mybranch
985--
986-- [master] Merge work in mybranch
987------------------------------------------------
988
989
990Merging external work
991---------------------
992
993It's usually much more common that you merge with somebody else than
994merging with your own branches, so it's worth pointing out that Git
995makes that very easy too, and in fact, it's not that different from
996doing a 'git merge'. In fact, a remote merge ends up being nothing
997more than "fetch the work from a remote repository into a temporary tag"
998followed by a 'git merge'.
999
1000Fetching from a remote repository is done by, unsurprisingly,
1001'git fetch':
1002
1003----------------
1004$ git fetch <remote-repository>
1005----------------
1006
1007One of the following transports can be used to name the
1008repository to download from:
1009
1010SSH::
1011 `remote.machine:/path/to/repo.git/` or
1012+
1013`ssh://remote.machine/path/to/repo.git/`
1014+
1015This transport can be used for both uploading and downloading,
1016and requires you to have a log-in privilege over `ssh` to the
1017remote machine. It finds out the set of objects the other side
1018lacks by exchanging the head commits both ends have and
1019transfers (close to) minimum set of objects. It is by far the
1020most efficient way to exchange Git objects between repositories.
1021
1022Local directory::
1023 `/path/to/repo.git/`
1024+
1025This transport is the same as SSH transport but uses 'sh' to run
1026both ends on the local machine instead of running other end on
1027the remote machine via 'ssh'.
1028
1029Git Native::
1030 `git://remote.machine/path/to/repo.git/`
1031+
1032This transport was designed for anonymous downloading. Like SSH
1033transport, it finds out the set of objects the downstream side
1034lacks and transfers (close to) minimum set of objects.
1035
1036HTTP(S)::
1037 `http://remote.machine/path/to/repo.git/`
1038+
1039Downloader from http and https URL
1040first obtains the topmost commit object name from the remote site
1041by looking at the specified refname under `repo.git/refs/` directory,
1042and then tries to obtain the
1043commit object by downloading from `repo.git/objects/xx/xxx...`
1044using the object name of that commit object. Then it reads the
1045commit object to find out its parent commits and the associate
1046tree object; it repeats this process until it gets all the
1047necessary objects. Because of this behavior, they are
1048sometimes also called 'commit walkers'.
1049+
1050The 'commit walkers' are sometimes also called 'dumb
1051transports', because they do not require any Git aware smart
1052server like Git Native transport does. Any stock HTTP server
1053that does not even support directory index would suffice. But
1054you must prepare your repository with 'git update-server-info'
1055to help dumb transport downloaders.
1056
1057Once you fetch from the remote repository, you `merge` that
1058with your current branch.
1059
1060However -- it's such a common thing to `fetch` and then
1061immediately `merge`, that it's called `git pull`, and you can
1062simply do
1063
1064----------------
1065$ git pull <remote-repository>
1066----------------
1067
1068and optionally give a branch-name for the remote end as a second
1069argument.
1070
1071[NOTE]
1072You could do without using any branches at all, by
1073keeping as many local repositories as you would like to have
1074branches, and merging between them with 'git pull', just like
1075you merge between branches. The advantage of this approach is
1076that it lets you keep a set of files for each `branch` checked
1077out and you may find it easier to switch back and forth if you
1078juggle multiple lines of development simultaneously. Of
1079course, you will pay the price of more disk usage to hold
1080multiple working trees, but disk space is cheap these days.
1081
1082It is likely that you will be pulling from the same remote
1083repository from time to time. As a short hand, you can store
1084the remote repository URL in the local repository's config file
1085like this:
1086
1087------------------------------------------------
1088$ git config remote.linus.url https://git.kernel.org/pub/scm/git/git.git/
1089------------------------------------------------
1090
1091and use the "linus" keyword with 'git pull' instead of the full URL.
1092
1093Examples.
1094
1095. `git pull linus`
1096. `git pull linus tag v0.99.1`
1097
1098the above are equivalent to:
1099
1100. `git pull http://www.kernel.org/pub/scm/git/git.git/ HEAD`
1101. `git pull http://www.kernel.org/pub/scm/git/git.git/ tag v0.99.1`
1102
1103
1104How does the merge work?
1105------------------------
1106
1107We said this tutorial shows what plumbing does to help you cope
1108with the porcelain that isn't flushing, but we so far did not
1109talk about how the merge really works. If you are following
1110this tutorial the first time, I'd suggest to skip to "Publishing
1111your work" section and come back here later.
1112
1113OK, still with me? To give us an example to look at, let's go
1114back to the earlier repository with "hello" and "example" file,
1115and bring ourselves back to the pre-merge state:
1116
1117------------
1118$ git show-branch --more=2 master mybranch
1119! [master] Merge work in mybranch
1120 * [mybranch] Merge work in mybranch
1121--
1122-- [master] Merge work in mybranch
1123+* [master^2] Some work.
1124+* [master^] Some fun.
1125------------
1126
1127Remember, before running 'git merge', our `master` head was at
1128"Some fun." commit, while our `mybranch` head was at "Some
1129work." commit.
1130
1131------------
1132$ git switch -C mybranch master^2
1133$ git switch master
1134$ git reset --hard master^
1135------------
1136
1137After rewinding, the commit structure should look like this:
1138
1139------------
1140$ git show-branch
1141* [master] Some fun.
1142 ! [mybranch] Some work.
1143--
1144* [master] Some fun.
1145 + [mybranch] Some work.
1146*+ [master^] Initial commit
1147------------
1148
1149Now we are ready to experiment with the merge by hand.
1150
1151`git merge` command, when merging two branches, uses 3-way merge
1152algorithm. First, it finds the common ancestor between them.
1153The command it uses is 'git merge-base':
1154
1155------------
1156$ mb=$(git merge-base HEAD mybranch)
1157------------
1158
1159The command writes the commit object name of the common ancestor
1160to the standard output, so we captured its output to a variable,
1161because we will be using it in the next step. By the way, the common
1162ancestor commit is the "Initial commit" commit in this case. You can
1163tell it by:
1164
1165------------
1166$ git name-rev --name-only --tags $mb
1167my-first-tag
1168------------
1169
1170After finding out a common ancestor commit, the second step is
1171this:
1172
1173------------
1174$ git read-tree -m -u $mb HEAD mybranch
1175------------
1176
1177This is the same 'git read-tree' command we have already seen,
1178but it takes three trees, unlike previous examples. This reads
1179the contents of each tree into different 'stage' in the index
1180file (the first tree goes to stage 1, the second to stage 2,
1181etc.). After reading three trees into three stages, the paths
1182that are the same in all three stages are 'collapsed' into stage
11830. Also paths that are the same in two of three stages are
1184collapsed into stage 0, taking the SHA-1 from either stage 2 or
1185stage 3, whichever is different from stage 1 (i.e. only one side
1186changed from the common ancestor).
1187
1188After 'collapsing' operation, paths that are different in three
1189trees are left in non-zero stages. At this point, you can
1190inspect the index file with this command:
1191
1192------------
1193$ git ls-files --stage
1194100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
1195100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1196100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1197100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
1198------------
1199
1200In our example of only two files, we did not have unchanged
1201files so only 'example' resulted in collapsing. But in real-life
1202large projects, when only a small number of files change in one commit,
1203this 'collapsing' tends to trivially merge most of the paths
1204fairly quickly, leaving only a handful of real changes in non-zero
1205stages.
1206
1207To look at only non-zero stages, use `--unmerged` flag:
1208
1209------------
1210$ git ls-files --unmerged
1211100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1212100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1213100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
1214------------
1215
1216The next step of merging is to merge these three versions of the
1217file, using 3-way merge. This is done by giving
1218'git merge-one-file' command as one of the arguments to
1219'git merge-index' command:
1220
1221------------
1222$ git merge-index git-merge-one-file hello
1223Auto-merging hello
1224ERROR: Merge conflict in hello
1225fatal: merge program failed
1226------------
1227
1228'git merge-one-file' script is called with parameters to
1229describe those three versions, and is responsible to leave the
1230merge results in the working tree.
1231It is a fairly straightforward shell script, and
1232eventually calls 'merge' program from RCS suite to perform a
1233file-level 3-way merge. In this case, 'merge' detects
1234conflicts, and the merge result with conflict marks is left in
1235the working tree.. This can be seen if you run `ls-files
1236--stage` again at this point:
1237
1238------------
1239$ git ls-files --stage
1240100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
1241100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1242100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1243100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
1244------------
1245
1246This is the state of the index file and the working file after
1247'git merge' returns control back to you, leaving the conflicting
1248merge for you to resolve. Notice that the path `hello` is still
1249unmerged, and what you see with 'git diff' at this point is
1250differences since stage 2 (i.e. your version).
1251
1252
1253Publishing your work
1254--------------------
1255
1256So, we can use somebody else's work from a remote repository, but
1257how can *you* prepare a repository to let other people pull from
1258it?
1259
1260You do your real work in your working tree that has your
1261primary repository hanging under it as its `.git` subdirectory.
1262You *could* make that repository accessible remotely and ask
1263people to pull from it, but in practice that is not the way
1264things are usually done. A recommended way is to have a public
1265repository, make it reachable by other people, and when the
1266changes you made in your primary working tree are in good shape,
1267update the public repository from it. This is often called
1268'pushing'.
1269
1270[NOTE]
1271This public repository could further be mirrored, and that is
1272how Git repositories at `kernel.org` are managed.
1273
1274Publishing the changes from your local (private) repository to
1275your remote (public) repository requires a write privilege on
1276the remote machine. You need to have an SSH account there to
1277run a single command, 'git-receive-pack'.
1278
1279First, you need to create an empty repository on the remote
1280machine that will house your public repository. This empty
1281repository will be populated and be kept up to date by pushing
1282into it later. Obviously, this repository creation needs to be
1283done only once.
1284
1285[NOTE]
1286'git push' uses a pair of commands,
1287'git send-pack' on your local machine, and 'git-receive-pack'
1288on the remote machine. The communication between the two over
1289the network internally uses an SSH connection.
1290
1291Your private repository's Git directory is usually `.git`, but
1292your public repository is often named after the project name,
1293i.e. `<project>.git`. Let's create such a public repository for
1294project `my-git`. After logging into the remote machine, create
1295an empty directory:
1296
1297------------
1298$ mkdir my-git.git
1299------------
1300
1301Then, make that directory into a Git repository by running
1302'git init', but this time, since its name is not the usual
1303`.git`, we do things slightly differently:
1304
1305------------
1306$ GIT_DIR=my-git.git git init
1307------------
1308
1309Make sure this directory is available for others you want your
1310changes to be pulled via the transport of your choice. Also
1311you need to make sure that you have the 'git-receive-pack'
1312program on the `$PATH`.
1313
1314[NOTE]
1315Many installations of sshd do not invoke your shell as the login
1316shell when you directly run programs; what this means is that if
1317your login shell is 'bash', only `.bashrc` is read and not
1318`.bash_profile`. As a workaround, make sure `.bashrc` sets up
1319`$PATH` so that you can run 'git-receive-pack' program.
1320
1321[NOTE]
1322If you plan to publish this repository to be accessed over http,
1323you should do `mv my-git.git/hooks/post-update.sample
1324my-git.git/hooks/post-update` at this point.
1325This makes sure that every time you push into this
1326repository, `git update-server-info` is run.
1327
1328Your "public repository" is now ready to accept your changes.
1329Come back to the machine you have your private repository. From
1330there, run this command:
1331
1332------------
1333$ git push <public-host>:/path/to/my-git.git master
1334------------
1335
1336This synchronizes your public repository to match the named
1337branch head (i.e. `master` in this case) and objects reachable
1338from them in your current repository.
1339
1340As a real example, this is how I update my public Git
1341repository. Kernel.org mirror network takes care of the
1342propagation to other publicly visible machines:
1343
1344------------
1345$ git push master.kernel.org:/pub/scm/git/git.git/
1346------------
1347
1348
1349Packing your repository
1350-----------------------
1351
1352Earlier, we saw that one file under `.git/objects/??/` directory
1353is stored for each Git object you create. This representation
1354is efficient to create atomically and safely, but
1355not so convenient to transport over the network. Since Git objects are
1356immutable once they are created, there is a way to optimize the
1357storage by "packing them together". The command
1358
1359------------
1360$ git repack
1361------------
1362
1363will do it for you. If you followed the tutorial examples, you
1364would have accumulated about 17 objects in `.git/objects/??/`
1365directories by now. 'git repack' tells you how many objects it
1366packed, and stores the packed file in the `.git/objects/pack`
1367directory.
1368
1369[NOTE]
1370You will see two files, `pack-*.pack` and `pack-*.idx`,
1371in `.git/objects/pack` directory. They are closely related to
1372each other, and if you ever copy them by hand to a different
1373repository for whatever reason, you should make sure you copy
1374them together. The former holds all the data from the objects
1375in the pack, and the latter holds the index for random
1376access.
1377
1378If you are paranoid, running 'git verify-pack' command would
1379detect if you have a corrupt pack, but do not worry too much.
1380Our programs are always perfect ;-).
1381
1382Once you have packed objects, you do not need to leave the
1383unpacked objects that are contained in the pack file anymore.
1384
1385------------
1386$ git prune-packed
1387------------
1388
1389would remove them for you.
1390
1391You can try running `find .git/objects -type f` before and after
1392you run `git prune-packed` if you are curious. Also `git
1393count-objects` would tell you how many unpacked objects are in
1394your repository and how much space they are consuming.
1395
1396[NOTE]
1397`git pull` is slightly cumbersome for HTTP transport, as a
1398packed repository may contain relatively few objects in a
1399relatively large pack. If you expect many HTTP pulls from your
1400public repository you might want to repack & prune often, or
1401never.
1402
1403If you run `git repack` again at this point, it will say
1404"Nothing new to pack.". Once you continue your development and
1405accumulate the changes, running `git repack` again will create a
1406new pack, that contains objects created since you packed your
1407repository the last time. We recommend that you pack your project
1408soon after the initial import (unless you are starting your
1409project from scratch), and then run `git repack` every once in a
1410while, depending on how active your project is.
1411
1412When a repository is synchronized via `git push` and `git pull`
1413objects packed in the source repository are usually stored
1414unpacked in the destination.
1415While this allows you to use different packing strategies on
1416both ends, it also means you may need to repack both
1417repositories every once in a while.
1418
1419
1420Working with Others
1421-------------------
1422
1423Although Git is a truly distributed system, it is often
1424convenient to organize your project with an informal hierarchy
1425of developers. Linux kernel development is run this way. There
1426is a nice illustration (page 17, "Merges to Mainline") in
1427https://web.archive.org/web/20120915203609/http://www.xenotime.net/linux/mentor/linux-mentoring-2006.pdf[Randy Dunlap's presentation].
1428
1429It should be stressed that this hierarchy is purely *informal*.
1430There is nothing fundamental in Git that enforces the "chain of
1431patch flow" this hierarchy implies. You do not have to pull
1432from only one remote repository.
1433
1434A recommended workflow for a "project lead" goes like this:
1435
14361. Prepare your primary repository on your local machine. Your
1437 work is done there.
1438
14392. Prepare a public repository accessible to others.
1440+
1441If other people are pulling from your repository over dumb
1442transport protocols (HTTP), you need to keep this repository
1443'dumb transport friendly'. After `git init`,
1444`$GIT_DIR/hooks/post-update.sample` copied from the standard templates
1445would contain a call to 'git update-server-info'
1446but you need to manually enable the hook with
1447`mv post-update.sample post-update`. This makes sure
1448'git update-server-info' keeps the necessary files up to date.
1449
14503. Push into the public repository from your primary
1451 repository.
1452
14534. 'git repack' the public repository. This establishes a big
1454 pack that contains the initial set of objects as the
1455 baseline, and possibly 'git prune' if the transport
1456 used for pulling from your repository supports packed
1457 repositories.
1458
14595. Keep working in your primary repository. Your changes
1460 include modifications of your own, patches you receive via
1461 e-mails, and merges resulting from pulling the "public"
1462 repositories of your "subsystem maintainers".
1463+
1464You can repack this private repository whenever you feel like.
1465
14666. Push your changes to the public repository, and announce it
1467 to the public.
1468
14697. Every once in a while, 'git repack' the public repository.
1470 Go back to step 5. and continue working.
1471
1472
1473A recommended work cycle for a "subsystem maintainer" who works
1474on that project and has an own "public repository" goes like this:
1475
14761. Prepare your work repository, by running 'git clone' on the public
1477 repository of the "project lead". The URL used for the
1478 initial cloning is stored in the remote.origin.url
1479 configuration variable.
1480
14812. Prepare a public repository accessible to others, just like
1482 the "project lead" person does.
1483
14843. Copy over the packed files from "project lead" public
1485 repository to your public repository, unless the "project
1486 lead" repository lives on the same machine as yours. In the
1487 latter case, you can use `objects/info/alternates` file to
1488 point at the repository you are borrowing from.
1489
14904. Push into the public repository from your primary
1491 repository. Run 'git repack', and possibly 'git prune' if the
1492 transport used for pulling from your repository supports
1493 packed repositories.
1494
14955. Keep working in your primary repository. Your changes
1496 include modifications of your own, patches you receive via
1497 e-mails, and merges resulting from pulling the "public"
1498 repositories of your "project lead" and possibly your
1499 "sub-subsystem maintainers".
1500+
1501You can repack this private repository whenever you feel
1502like.
1503
15046. Push your changes to your public repository, and ask your
1505 "project lead" and possibly your "sub-subsystem
1506 maintainers" to pull from it.
1507
15087. Every once in a while, 'git repack' the public repository.
1509 Go back to step 5. and continue working.
1510
1511
1512A recommended work cycle for an "individual developer" who does
1513not have a "public" repository is somewhat different. It goes
1514like this:
1515
15161. Prepare your work repository, by 'git clone' the public
1517 repository of the "project lead" (or a "subsystem
1518 maintainer", if you work on a subsystem). The URL used for
1519 the initial cloning is stored in the remote.origin.url
1520 configuration variable.
1521
15222. Do your work in your repository on 'master' branch.
1523
15243. Run `git fetch origin` from the public repository of your
1525 upstream every once in a while. This does only the first
1526 half of `git pull` but does not merge. The head of the
1527 public repository is stored in `.git/refs/remotes/origin/master`.
1528
15294. Use `git cherry origin` to see which ones of your patches
1530 were accepted, and/or use `git rebase origin` to port your
1531 unmerged changes forward to the updated upstream.
1532
15335. Use `git format-patch origin` to prepare patches for e-mail
1534 submission to your upstream and send it out. Go back to
1535 step 2. and continue.
1536
1537
1538Working with Others, Shared Repository Style
1539--------------------------------------------
1540
1541If you are coming from a CVS background, the style of cooperation
1542suggested in the previous section may be new to you. You do not
1543have to worry. Git supports the "shared public repository" style of
1544cooperation you are probably more familiar with as well.
1545
1546See linkgit:gitcvs-migration[7] for the details.
1547
1548Bundling your work together
1549---------------------------
1550
1551It is likely that you will be working on more than one thing at
1552a time. It is easy to manage those more-or-less independent tasks
1553using branches with Git.
1554
1555We have already seen how branches work previously,
1556with "fun and work" example using two branches. The idea is the
1557same if there are more than two branches. Let's say you started
1558out from "master" head, and have some new code in the "master"
1559branch, and two independent fixes in the "commit-fix" and
1560"diff-fix" branches:
1561
1562------------
1563$ git show-branch
1564! [commit-fix] Fix commit message normalization.
1565 ! [diff-fix] Fix rename detection.
1566 * [master] Release candidate #1
1567---
1568 + [diff-fix] Fix rename detection.
1569 + [diff-fix~1] Better common substring algorithm.
1570+ [commit-fix] Fix commit message normalization.
1571 * [master] Release candidate #1
1572++* [diff-fix~2] Pretty-print messages.
1573------------
1574
1575Both fixes are tested well, and at this point, you want to merge
1576in both of them. You could merge in 'diff-fix' first and then
1577'commit-fix' next, like this:
1578
1579------------
1580$ git merge -m "Merge fix in diff-fix" diff-fix
1581$ git merge -m "Merge fix in commit-fix" commit-fix
1582------------
1583
1584Which would result in:
1585
1586------------
1587$ git show-branch
1588! [commit-fix] Fix commit message normalization.
1589 ! [diff-fix] Fix rename detection.
1590 * [master] Merge fix in commit-fix
1591---
1592 - [master] Merge fix in commit-fix
1593+ * [commit-fix] Fix commit message normalization.
1594 - [master~1] Merge fix in diff-fix
1595 +* [diff-fix] Fix rename detection.
1596 +* [diff-fix~1] Better common substring algorithm.
1597 * [master~2] Release candidate #1
1598++* [master~3] Pretty-print messages.
1599------------
1600
1601However, there is no particular reason to merge in one branch
1602first and the other next, when what you have are a set of truly
1603independent changes (if the order mattered, then they are not
1604independent by definition). You could instead merge those two
1605branches into the current branch at once. First let's undo what
1606we just did and start over. We would want to get the master
1607branch before these two merges by resetting it to 'master~2':
1608
1609------------
1610$ git reset --hard master~2
1611------------
1612
1613You can make sure `git show-branch` matches the state before
1614those two 'git merge' you just did. Then, instead of running
1615two 'git merge' commands in a row, you would merge these two
1616branch heads (this is known as 'making an Octopus'):
1617
1618------------
1619$ git merge commit-fix diff-fix
1620$ git show-branch
1621! [commit-fix] Fix commit message normalization.
1622 ! [diff-fix] Fix rename detection.
1623 * [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
1624---
1625 - [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
1626+ * [commit-fix] Fix commit message normalization.
1627 +* [diff-fix] Fix rename detection.
1628 +* [diff-fix~1] Better common substring algorithm.
1629 * [master~1] Release candidate #1
1630++* [master~2] Pretty-print messages.
1631------------
1632
1633Note that you should not do Octopus just because you can. An octopus
1634is a valid thing to do and often makes it easier to view the
1635commit history if you are merging more than two independent
1636changes at the same time. However, if you have merge conflicts
1637with any of the branches you are merging in and need to hand
1638resolve, that is an indication that the development happened in
1639those branches were not independent after all, and you should
1640merge two at a time, documenting how you resolved the conflicts,
1641and the reason why you preferred changes made in one side over
1642the other. Otherwise it would make the project history harder
1643to follow, not easier.
1644
1645SEE ALSO
1646--------
1647linkgit:gittutorial[7],
1648linkgit:gitcvs-migration[7],
1649linkgit:git-help[1],
1650linkgit:giteveryday[7],
1651link:user-manual.html[The Git User's Manual]
1652
1653GIT
1654---
1655Part of the linkgit:git[1] suite