1GITCORE-TUTORIAL(7) Git Manual GITCORE-TUTORIAL(7)
2
3
4
6 gitcore-tutorial - A Git core tutorial for developers
7
9 git *
10
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 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
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
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
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
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
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. You can emulate git log, git log -p, etc. with a trivial
494 script that pipes the output of git rev-list to git diff-tree --stdin,
495 which was exactly how early versions of git log were implemented.
496
498 In Git, there are two kinds of tags, a "light" one, and an "annotated
499 tag".
500
501 A "light" tag is technically nothing more than a branch, except we put
502 it in the .git/refs/tags/ subdirectory instead of calling it a head. So
503 the simplest form of tag involves nothing more than
504
505 $ git tag my-first-tag
506
507
508 which just writes the current HEAD into the .git/refs/tags/my-first-tag
509 file, after which point you can then use this symbolic name for that
510 particular state. You can, for example, do
511
512 $ git diff my-first-tag
513
514
515 to diff your current state against that tag which at this point will
516 obviously be an empty diff, but if you continue to develop and commit
517 stuff, you can use your tag as an "anchor-point" to see what has
518 changed since you tagged it.
519
520 An "annotated tag" is actually a real Git object, and contains not only
521 a pointer to the state you want to tag, but also a small tag name and
522 message, along with optionally a PGP signature that says that yes, you
523 really did that tag. You create these annotated tags with either the -a
524 or -s flag to git tag:
525
526 $ git tag -s <tagname>
527
528
529 which will sign the current HEAD (but you can also give it another
530 argument that specifies the thing to tag, e.g., you could have tagged
531 the current mybranch point by using git tag <tagname> mybranch).
532
533 You normally only do signed tags for major releases or things like
534 that, while the light-weight tags are useful for any marking you want
535 to do — any time you decide that you want to remember a certain point,
536 just create a private tag for it, and you have a nice symbolic name for
537 the state at that point.
538
540 Git repositories are normally totally self-sufficient and relocatable.
541 Unlike CVS, for example, there is no separate notion of "repository"
542 and "working tree". A Git repository normally is the working tree, with
543 the local Git information hidden in the .git subdirectory. There is
544 nothing else. What you see is what you got.
545
546 Note
547 You can tell Git to split the Git internal information from the
548 directory that it tracks, but we’ll ignore that for now: it’s not
549 how normal projects work, and it’s really only meant for special
550 uses. So the mental model of "the Git information is always tied
551 directly to the working tree that it describes" may not be
552 technically 100% accurate, but it’s a good model for all normal
553 use.
554
555 This has two implications:
556
557 · if you grow bored with the tutorial repository you created (or
558 you’ve made a mistake and want to start all over), you can just do
559 simple
560
561 $ rm -rf git-tutorial
562
563 and it will be gone. There’s no external repository, and there’s no
564 history outside the project you created.
565
566 · if you want to move or duplicate a Git repository, you can do so.
567 There is git clone command, but if all you want to do is just to
568 create a copy of your repository (with all the full history that
569 went along with it), you can do so with a regular cp -a
570 git-tutorial new-git-tutorial.
571
572 Note that when you’ve moved or copied a Git repository, your Git
573 index file (which caches various information, notably some of the
574 "stat" information for the files involved) will likely need to be
575 refreshed. So after you do a cp -a to create a new copy, you’ll
576 want to do
577
578 $ git update-index --refresh
579
580 in the new repository to make sure that the index file is up to
581 date.
582
583 Note that the second point is true even across machines. You can
584 duplicate a remote Git repository with any regular copy mechanism, be
585 it scp, rsync or wget.
586
587 When copying a remote repository, you’ll want to at a minimum update
588 the index cache when you do this, and especially with other peoples'
589 repositories you often want to make sure that the index cache is in
590 some known state (you don’t know what they’ve done and not yet checked
591 in), so usually you’ll precede the git update-index with a
592
593 $ git read-tree --reset HEAD
594 $ git update-index --refresh
595
596
597 which will force a total index re-build from the tree pointed to by
598 HEAD. It resets the index contents to HEAD, and then the git
599 update-index makes sure to match up all index entries with the
600 checked-out files. If the original repository had uncommitted changes
601 in its working tree, git update-index --refresh notices them and tells
602 you they need to be updated.
603
604 The above can also be written as simply
605
606 $ git reset
607
608
609 and in fact a lot of the common Git command combinations can be
610 scripted with the git xyz interfaces. You can learn things by just
611 looking at what the various git scripts do. For example, git reset used
612 to be the above two lines implemented in git reset, but some things
613 like git status and git commit are slightly more complex scripts around
614 the basic Git commands.
615
616 Many (most?) public remote repositories will not contain any of the
617 checked out files or even an index file, and will only contain the
618 actual core Git files. Such a repository usually doesn’t even have the
619 .git subdirectory, but has all the Git files directly in the
620 repository.
621
622 To create your own local live copy of such a "raw" Git repository,
623 you’d first create your own subdirectory for the project, and then copy
624 the raw repository contents into the .git directory. For example, to
625 create your own copy of the Git repository, you’d do the following
626
627 $ mkdir my-git
628 $ cd my-git
629 $ rsync -rL rsync://rsync.kernel.org/pub/scm/git/git.git/ .git
630
631
632 followed by
633
634 $ git read-tree HEAD
635
636
637 to populate the index. However, now you have populated the index, and
638 you have all the Git internal files, but you will notice that you don’t
639 actually have any of the working tree files to work on. To get those,
640 you’d check them out with
641
642 $ git checkout-index -u -a
643
644
645 where the -u flag means that you want the checkout to keep the index up
646 to date (so that you don’t have to refresh it afterward), and the -a
647 flag means "check out all files" (if you have a stale copy or an older
648 version of a checked out tree you may also need to add the -f flag
649 first, to tell git checkout-index to force overwriting of any old
650 files).
651
652 Again, this can all be simplified with
653
654 $ git clone git://git.kernel.org/pub/scm/git/git.git/ my-git
655 $ cd my-git
656 $ git checkout
657
658
659 which will end up doing all of the above for you.
660
661 You have now successfully copied somebody else’s (mine) remote
662 repository, and checked it out.
663
665 Branches in Git are really nothing more than pointers into the Git
666 object database from within the .git/refs/ subdirectory, and as we
667 already discussed, the HEAD branch is nothing but a symlink to one of
668 these object pointers.
669
670 You can at any time create a new branch by just picking an arbitrary
671 point in the project history, and just writing the SHA-1 name of that
672 object into a file under .git/refs/heads/. You can use any filename you
673 want (and indeed, subdirectories), but the convention is that the
674 "normal" branch is called master. That’s just a convention, though, and
675 nothing enforces it.
676
677 To show that as an example, let’s go back to the git-tutorial
678 repository we used earlier, and create a branch in it. You do that by
679 simply just saying that you want to check out a new branch:
680
681 $ git checkout -b mybranch
682
683
684 will create a new branch based at the current HEAD position, and switch
685 to it.
686
687 Note
688 If you make the decision to start your new branch at some other
689 point in the history than the current HEAD, you can do so by just
690 telling git checkout what the base of the checkout would be. In
691 other words, if you have an earlier tag or branch, you’d just do
692
693 $ git checkout -b mybranch earlier-commit
694
695
696 and it would create the new branch mybranch at the earlier commit,
697 and check out the state at that time.
698
699 You can always just jump back to your original master branch by doing
700
701 $ git checkout master
702
703
704 (or any other branch-name, for that matter) and if you forget which
705 branch you happen to be on, a simple
706
707 $ cat .git/HEAD
708
709
710 will tell you where it’s pointing. To get the list of branches you
711 have, you can say
712
713 $ git branch
714
715
716 which used to be nothing more than a simple script around ls
717 .git/refs/heads. There will be an asterisk in front of the branch you
718 are currently on.
719
720 Sometimes you may wish to create a new branch without actually checking
721 it out and switching to it. If so, just use the command
722
723 $ git branch <branchname> [startingpoint]
724
725
726 which will simply create the branch, but will not do anything further.
727 You can then later — once you decide that you want to actually develop
728 on that branch — switch to that branch with a regular git checkout with
729 the branchname as the argument.
730
732 One of the ideas of having a branch is that you do some (possibly
733 experimental) work in it, and eventually merge it back to the main
734 branch. So assuming you created the above mybranch that started out
735 being the same as the original master branch, let’s make sure we’re in
736 that branch, and do some work there.
737
738 $ git checkout mybranch
739 $ echo "Work, work, work" >>hello
740 $ git commit -m "Some work." -i hello
741
742
743 Here, we just added another line to hello, and we used a shorthand for
744 doing both git update-index hello and git commit by just giving the
745 filename directly to git commit, with an -i flag (it tells Git to
746 include that file in addition to what you have done to the index file
747 so far when making the commit). The -m flag is to give the commit log
748 message from the command line.
749
750 Now, to make it a bit more interesting, let’s assume that somebody else
751 does some work in the original branch, and simulate that by going back
752 to the master branch, and editing the same file differently there:
753
754 $ git checkout master
755
756
757 Here, take a moment to look at the contents of hello, and notice how
758 they don’t contain the work we just did in mybranch — because that work
759 hasn’t happened in the master branch at all. Then do
760
761 $ echo "Play, play, play" >>hello
762 $ echo "Lots of fun" >>example
763 $ git commit -m "Some fun." -i hello example
764
765
766 since the master branch is obviously in a much better mood.
767
768 Now, you’ve got two branches, and you decide that you want to merge the
769 work done. Before we do that, let’s introduce a cool graphical tool
770 that helps you view what’s going on:
771
772 $ gitk --all
773
774
775 will show you graphically both of your branches (that’s what the --all
776 means: normally it will just show you your current HEAD) and their
777 histories. You can also see exactly how they came to be from a common
778 source.
779
780 Anyway, let’s exit gitk (^Q or the File menu), and decide that we want
781 to merge the work we did on the mybranch branch into the master branch
782 (which is currently our HEAD too). To do that, there’s a nice script
783 called git merge, which wants to know which branches you want to
784 resolve and what the merge is all about:
785
786 $ git merge -m "Merge work in mybranch" mybranch
787
788
789 where the first argument is going to be used as the commit message if
790 the merge can be resolved automatically.
791
792 Now, in this case we’ve intentionally created a situation where the
793 merge will need to be fixed up by hand, though, so Git will do as much
794 of it as it can automatically (which in this case is just merge the
795 example file, which had no differences in the mybranch branch), and
796 say:
797
798 Auto-merging hello
799 CONFLICT (content): Merge conflict in hello
800 Automatic merge failed; fix conflicts and then commit the result.
801
802
803 It tells you that it did an "Automatic merge", which failed due to
804 conflicts in hello.
805
806 Not to worry. It left the (trivial) conflict in hello in the same form
807 you should already be well used to if you’ve ever used CVS, so let’s
808 just open hello in our editor (whatever that may be), and fix it up
809 somehow. I’d suggest just making it so that hello contains all four
810 lines:
811
812 Hello World
813 It's a new day for git
814 Play, play, play
815 Work, work, work
816
817
818 and once you’re happy with your manual merge, just do a
819
820 $ git commit -i hello
821
822
823 which will very loudly warn you that you’re now committing a merge
824 (which is correct, so never mind), and you can write a small merge
825 message about your adventures in git merge-land.
826
827 After you’re done, start up gitk --all to see graphically what the
828 history looks like. Notice that mybranch still exists, and you can
829 switch to it, and continue to work with it if you want to. The mybranch
830 branch will not contain the merge, but next time you merge it from the
831 master branch, Git will know how you merged it, so you’ll not have to
832 do that merge again.
833
834 Another useful tool, especially if you do not always work in X-Window
835 environment, is git show-branch.
836
837 $ git show-branch --topo-order --more=1 master mybranch
838 * [master] Merge work in mybranch
839 ! [mybranch] Some work.
840 --
841 - [master] Merge work in mybranch
842 *+ [mybranch] Some work.
843 * [master^] Some fun.
844
845
846 The first two lines indicate that it is showing the two branches with
847 the titles of their top-of-the-tree commits, you are currently on
848 master branch (notice the asterisk * character), and the first column
849 for the later output lines is used to show commits contained in the
850 master branch, and the second column for the mybranch branch. Three
851 commits are shown along with their titles. All of them have non blank
852 characters in the first column (* shows an ordinary commit on the
853 current branch, - is a merge commit), which means they are now part of
854 the master branch. Only the "Some work" commit has the plus + character
855 in the second column, because mybranch has not been merged to
856 incorporate these commits from the master branch. The string inside
857 brackets before the commit log message is a short name you can use to
858 name the commit. In the above example, master and mybranch are branch
859 heads. master^ is the first parent of master branch head. Please see
860 gitrevisions(7) if you want to see more complex cases.
861
862 Note
863 Without the --more=1 option, git show-branch would not output the
864 [master^] commit, as [mybranch] commit is a common ancestor of both
865 master and mybranch tips. Please see git-show-branch(1) for
866 details.
867
868 Note
869 If there were more commits on the master branch after the merge,
870 the merge commit itself would not be shown by git show-branch by
871 default. You would need to provide --sparse option to make the
872 merge commit visible in this case.
873
874 Now, let’s pretend you are the one who did all the work in mybranch,
875 and the fruit of your hard work has finally been merged to the master
876 branch. Let’s go back to mybranch, and run git merge to get the
877 "upstream changes" back to your branch.
878
879 $ git checkout mybranch
880 $ git merge -m "Merge upstream changes." master
881
882
883 This outputs something like this (the actual commit object names would
884 be different)
885
886 Updating from ae3a2da... to a80b4aa....
887 Fast-forward (no commit created; -m option ignored)
888 example | 1 +
889 hello | 1 +
890 2 files changed, 2 insertions(+)
891
892
893 Because your branch did not contain anything more than what had already
894 been merged into the master branch, the merge operation did not
895 actually do a merge. Instead, it just updated the top of the tree of
896 your branch to that of the master branch. This is often called
897 fast-forward merge.
898
899 You can run gitk --all again to see how the commit ancestry looks like,
900 or run show-branch, which tells you this.
901
902 $ git show-branch master mybranch
903 ! [master] Merge work in mybranch
904 * [mybranch] Merge work in mybranch
905 --
906 -- [master] Merge work in mybranch
907
908
910 It’s usually much more common that you merge with somebody else than
911 merging with your own branches, so it’s worth pointing out that Git
912 makes that very easy too, and in fact, it’s not that different from
913 doing a git merge. In fact, a remote merge ends up being nothing more
914 than "fetch the work from a remote repository into a temporary tag"
915 followed by a git merge.
916
917 Fetching from a remote repository is done by, unsurprisingly, git
918 fetch:
919
920 $ git fetch <remote-repository>
921
922
923 One of the following transports can be used to name the repository to
924 download from:
925
926 SSH
927 remote.machine:/path/to/repo.git/ or
928
929 ssh://remote.machine/path/to/repo.git/
930
931 This transport can be used for both uploading and downloading, and
932 requires you to have a log-in privilege over ssh to the remote
933 machine. It finds out the set of objects the other side lacks by
934 exchanging the head commits both ends have and transfers (close to)
935 minimum set of objects. It is by far the most efficient way to
936 exchange Git objects between repositories.
937
938 Local directory
939 /path/to/repo.git/
940
941 This transport is the same as SSH transport but uses sh to run both
942 ends on the local machine instead of running other end on the
943 remote machine via ssh.
944
945 Git Native
946 git://remote.machine/path/to/repo.git/
947
948 This transport was designed for anonymous downloading. Like SSH
949 transport, it finds out the set of objects the downstream side
950 lacks and transfers (close to) minimum set of objects.
951
952 HTTP(S)
953 http://remote.machine/path/to/repo.git/
954
955 Downloader from http and https URL first obtains the topmost commit
956 object name from the remote site by looking at the specified
957 refname under repo.git/refs/ directory, and then tries to obtain
958 the commit object by downloading from repo.git/objects/xx/xxx...
959 using the object name of that commit object. Then it reads the
960 commit object to find out its parent commits and the associate tree
961 object; it repeats this process until it gets all the necessary
962 objects. Because of this behavior, they are sometimes also called
963 commit walkers.
964
965 The commit walkers are sometimes also called dumb transports,
966 because they do not require any Git aware smart server like Git
967 Native transport does. Any stock HTTP server that does not even
968 support directory index would suffice. But you must prepare your
969 repository with git update-server-info to help dumb transport
970 downloaders.
971
972 Once you fetch from the remote repository, you merge that with your
973 current branch.
974
975 However — it’s such a common thing to fetch and then immediately merge,
976 that it’s called git pull, and you can simply do
977
978 $ git pull <remote-repository>
979
980
981 and optionally give a branch-name for the remote end as a second
982 argument.
983
984 Note
985 You could do without using any branches at all, by keeping as many
986 local repositories as you would like to have branches, and merging
987 between them with git pull, just like you merge between branches.
988 The advantage of this approach is that it lets you keep a set of
989 files for each branch checked out and you may find it easier to
990 switch back and forth if you juggle multiple lines of development
991 simultaneously. Of course, you will pay the price of more disk
992 usage to hold multiple working trees, but disk space is cheap these
993 days.
994
995 It is likely that you will be pulling from the same remote repository
996 from time to time. As a short hand, you can store the remote repository
997 URL in the local repository’s config file like this:
998
999 $ git config remote.linus.url http://www.kernel.org/pub/scm/git/git.git/
1000
1001
1002 and use the "linus" keyword with git pull instead of the full URL.
1003
1004 Examples.
1005
1006 1. git pull linus
1007
1008 2. git pull linus tag v0.99.1
1009
1010 the above are equivalent to:
1011
1012 1. git pull http://www.kernel.org/pub/scm/git/git.git/ HEAD
1013
1014 2. git pull http://www.kernel.org/pub/scm/git/git.git/ tag v0.99.1
1015
1017 We said this tutorial shows what plumbing does to help you cope with
1018 the porcelain that isn’t flushing, but we so far did not talk about how
1019 the merge really works. If you are following this tutorial the first
1020 time, I’d suggest to skip to "Publishing your work" section and come
1021 back here later.
1022
1023 OK, still with me? To give us an example to look at, let’s go back to
1024 the earlier repository with "hello" and "example" file, and bring
1025 ourselves back to the pre-merge state:
1026
1027 $ git show-branch --more=2 master mybranch
1028 ! [master] Merge work in mybranch
1029 * [mybranch] Merge work in mybranch
1030 --
1031 -- [master] Merge work in mybranch
1032 +* [master^2] Some work.
1033 +* [master^] Some fun.
1034
1035
1036 Remember, before running git merge, our master head was at "Some fun."
1037 commit, while our mybranch head was at "Some work." commit.
1038
1039 $ git checkout mybranch
1040 $ git reset --hard master^2
1041 $ git checkout master
1042 $ git reset --hard master^
1043
1044
1045 After rewinding, the commit structure should look like this:
1046
1047 $ git show-branch
1048 * [master] Some fun.
1049 ! [mybranch] Some work.
1050 --
1051 * [master] Some fun.
1052 + [mybranch] Some work.
1053 *+ [master^] Initial commit
1054
1055
1056 Now we are ready to experiment with the merge by hand.
1057
1058 git merge command, when merging two branches, uses 3-way merge
1059 algorithm. First, it finds the common ancestor between them. The
1060 command it uses is git merge-base:
1061
1062 $ mb=$(git merge-base HEAD mybranch)
1063
1064
1065 The command writes the commit object name of the common ancestor to the
1066 standard output, so we captured its output to a variable, because we
1067 will be using it in the next step. By the way, the common ancestor
1068 commit is the "Initial commit" commit in this case. You can tell it by:
1069
1070 $ git name-rev --name-only --tags $mb
1071 my-first-tag
1072
1073
1074 After finding out a common ancestor commit, the second step is this:
1075
1076 $ git read-tree -m -u $mb HEAD mybranch
1077
1078
1079 This is the same git read-tree command we have already seen, but it
1080 takes three trees, unlike previous examples. This reads the contents of
1081 each tree into different stage in the index file (the first tree goes
1082 to stage 1, the second to stage 2, etc.). After reading three trees
1083 into three stages, the paths that are the same in all three stages are
1084 collapsed into stage 0. Also paths that are the same in two of three
1085 stages are collapsed into stage 0, taking the SHA-1 from either stage 2
1086 or stage 3, whichever is different from stage 1 (i.e. only one side
1087 changed from the common ancestor).
1088
1089 After collapsing operation, paths that are different in three trees are
1090 left in non-zero stages. At this point, you can inspect the index file
1091 with this command:
1092
1093 $ git ls-files --stage
1094 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
1095 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1096 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1097 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
1098
1099
1100 In our example of only two files, we did not have unchanged files so
1101 only example resulted in collapsing. But in real-life large projects,
1102 when only a small number of files change in one commit, this collapsing
1103 tends to trivially merge most of the paths fairly quickly, leaving only
1104 a handful of real changes in non-zero stages.
1105
1106 To look at only non-zero stages, use --unmerged flag:
1107
1108 $ git ls-files --unmerged
1109 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1110 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1111 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
1112
1113
1114 The next step of merging is to merge these three versions of the file,
1115 using 3-way merge. This is done by giving git merge-one-file command as
1116 one of the arguments to git merge-index command:
1117
1118 $ git merge-index git-merge-one-file hello
1119 Auto-merging hello
1120 ERROR: Merge conflict in hello
1121 fatal: merge program failed
1122
1123
1124 git merge-one-file script is called with parameters to describe those
1125 three versions, and is responsible to leave the merge results in the
1126 working tree. It is a fairly straightforward shell script, and
1127 eventually calls merge program from RCS suite to perform a file-level
1128 3-way merge. In this case, merge detects conflicts, and the merge
1129 result with conflict marks is left in the working tree.. This can be
1130 seen if you run ls-files --stage again at this point:
1131
1132 $ git ls-files --stage
1133 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example
1134 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello
1135 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello
1136 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello
1137
1138
1139 This is the state of the index file and the working file after git
1140 merge returns control back to you, leaving the conflicting merge for
1141 you to resolve. Notice that the path hello is still unmerged, and what
1142 you see with git diff at this point is differences since stage 2 (i.e.
1143 your version).
1144
1146 So, we can use somebody else’s work from a remote repository, but how
1147 can you prepare a repository to let other people pull from it?
1148
1149 You do your real work in your working tree that has your primary
1150 repository hanging under it as its .git subdirectory. You could make
1151 that repository accessible remotely and ask people to pull from it, but
1152 in practice that is not the way things are usually done. A recommended
1153 way is to have a public repository, make it reachable by other people,
1154 and when the changes you made in your primary working tree are in good
1155 shape, update the public repository from it. This is often called
1156 pushing.
1157
1158 Note
1159 This public repository could further be mirrored, and that is how
1160 Git repositories at kernel.org are managed.
1161
1162 Publishing the changes from your local (private) repository to your
1163 remote (public) repository requires a write privilege on the remote
1164 machine. You need to have an SSH account there to run a single command,
1165 git-receive-pack.
1166
1167 First, you need to create an empty repository on the remote machine
1168 that will house your public repository. This empty repository will be
1169 populated and be kept up to date by pushing into it later. Obviously,
1170 this repository creation needs to be done only once.
1171
1172 Note
1173 git push uses a pair of commands, git send-pack on your local
1174 machine, and git-receive-pack on the remote machine. The
1175 communication between the two over the network internally uses an
1176 SSH connection.
1177
1178 Your private repository’s Git directory is usually .git, but your
1179 public repository is often named after the project name, i.e.
1180 <project>.git. Let’s create such a public repository for project
1181 my-git. After logging into the remote machine, create an empty
1182 directory:
1183
1184 $ mkdir my-git.git
1185
1186
1187 Then, make that directory into a Git repository by running git init,
1188 but this time, since its name is not the usual .git, we do things
1189 slightly differently:
1190
1191 $ GIT_DIR=my-git.git git init
1192
1193
1194 Make sure this directory is available for others you want your changes
1195 to be pulled via the transport of your choice. Also you need to make
1196 sure that you have the git-receive-pack program on the $PATH.
1197
1198 Note
1199 Many installations of sshd do not invoke your shell as the login
1200 shell when you directly run programs; what this means is that if
1201 your login shell is bash, only .bashrc is read and not
1202 .bash_profile. As a workaround, make sure .bashrc sets up $PATH so
1203 that you can run git-receive-pack program.
1204
1205 Note
1206 If you plan to publish this repository to be accessed over http,
1207 you should do mv my-git.git/hooks/post-update.sample
1208 my-git.git/hooks/post-update at this point. This makes sure that
1209 every time you push into this repository, git update-server-info is
1210 run.
1211
1212 Your "public repository" is now ready to accept your changes. Come back
1213 to the machine you have your private repository. From there, run this
1214 command:
1215
1216 $ git push <public-host>:/path/to/my-git.git master
1217
1218
1219 This synchronizes your public repository to match the named branch head
1220 (i.e. master in this case) and objects reachable from them in your
1221 current repository.
1222
1223 As a real example, this is how I update my public Git repository.
1224 Kernel.org mirror network takes care of the propagation to other
1225 publicly visible machines:
1226
1227 $ git push master.kernel.org:/pub/scm/git/git.git/
1228
1229
1231 Earlier, we saw that one file under .git/objects/??/ directory is
1232 stored for each Git object you create. This representation is efficient
1233 to create atomically and safely, but not so convenient to transport
1234 over the network. Since Git objects are immutable once they are
1235 created, there is a way to optimize the storage by "packing them
1236 together". The command
1237
1238 $ git repack
1239
1240
1241 will do it for you. If you followed the tutorial examples, you would
1242 have accumulated about 17 objects in .git/objects/??/ directories by
1243 now. git repack tells you how many objects it packed, and stores the
1244 packed file in the .git/objects/pack directory.
1245
1246 Note
1247 You will see two files, pack-*.pack and pack-*.idx, in
1248 .git/objects/pack directory. They are closely related to each
1249 other, and if you ever copy them by hand to a different repository
1250 for whatever reason, you should make sure you copy them together.
1251 The former holds all the data from the objects in the pack, and the
1252 latter holds the index for random access.
1253
1254 If you are paranoid, running git verify-pack command would detect if
1255 you have a corrupt pack, but do not worry too much. Our programs are
1256 always perfect ;-).
1257
1258 Once you have packed objects, you do not need to leave the unpacked
1259 objects that are contained in the pack file anymore.
1260
1261 $ git prune-packed
1262
1263
1264 would remove them for you.
1265
1266 You can try running find .git/objects -type f before and after you run
1267 git prune-packed if you are curious. Also git count-objects would tell
1268 you how many unpacked objects are in your repository and how much space
1269 they are consuming.
1270
1271 Note
1272 git pull is slightly cumbersome for HTTP transport, as a packed
1273 repository may contain relatively few objects in a relatively large
1274 pack. If you expect many HTTP pulls from your public repository you
1275 might want to repack & prune often, or never.
1276
1277 If you run git repack again at this point, it will say "Nothing new to
1278 pack.". Once you continue your development and accumulate the changes,
1279 running git repack again will create a new pack, that contains objects
1280 created since you packed your repository the last time. We recommend
1281 that you pack your project soon after the initial import (unless you
1282 are starting your project from scratch), and then run git repack every
1283 once in a while, depending on how active your project is.
1284
1285 When a repository is synchronized via git push and git pull objects
1286 packed in the source repository are usually stored unpacked in the
1287 destination. While this allows you to use different packing strategies
1288 on both ends, it also means you may need to repack both repositories
1289 every once in a while.
1290
1292 Although Git is a truly distributed system, it is often convenient to
1293 organize your project with an informal hierarchy of developers. Linux
1294 kernel development is run this way. There is a nice illustration (page
1295 17, "Merges to Mainline") in Randy Dunlap’s presentation[2].
1296
1297 It should be stressed that this hierarchy is purely informal. There is
1298 nothing fundamental in Git that enforces the "chain of patch flow" this
1299 hierarchy implies. You do not have to pull from only one remote
1300 repository.
1301
1302 A recommended workflow for a "project lead" goes like this:
1303
1304 1. Prepare your primary repository on your local machine. Your work is
1305 done there.
1306
1307 2. Prepare a public repository accessible to others.
1308
1309 If other people are pulling from your repository over dumb
1310 transport protocols (HTTP), you need to keep this repository dumb
1311 transport friendly. After git init,
1312 $GIT_DIR/hooks/post-update.sample copied from the standard
1313 templates would contain a call to git update-server-info but you
1314 need to manually enable the hook with mv post-update.sample
1315 post-update. This makes sure git update-server-info keeps the
1316 necessary files up to date.
1317
1318 3. Push into the public repository from your primary repository.
1319
1320 4. git repack the public repository. This establishes a big pack that
1321 contains the initial set of objects as the baseline, and possibly
1322 git prune if the transport used for pulling from your repository
1323 supports packed repositories.
1324
1325 5. Keep working in your primary repository. Your changes include
1326 modifications of your own, patches you receive via e-mails, and
1327 merges resulting from pulling the "public" repositories of your
1328 "subsystem maintainers".
1329
1330 You can repack this private repository whenever you feel like.
1331
1332 6. Push your changes to the public repository, and announce it to the
1333 public.
1334
1335 7. Every once in a while, git repack the public repository. Go back to
1336 step 5. and continue working.
1337
1338 A recommended work cycle for a "subsystem maintainer" who works on that
1339 project and has an own "public repository" goes like this:
1340
1341 1. Prepare your work repository, by running git clone on the public
1342 repository of the "project lead". The URL used for the initial
1343 cloning is stored in the remote.origin.url configuration variable.
1344
1345 2. Prepare a public repository accessible to others, just like the
1346 "project lead" person does.
1347
1348 3. Copy over the packed files from "project lead" public repository to
1349 your public repository, unless the "project lead" repository lives
1350 on the same machine as yours. In the latter case, you can use
1351 objects/info/alternates file to point at the repository you are
1352 borrowing from.
1353
1354 4. Push into the public repository from your primary repository. Run
1355 git repack, and possibly git prune if the transport used for
1356 pulling from your repository supports packed repositories.
1357
1358 5. Keep working in your primary repository. Your changes include
1359 modifications of your own, patches you receive via e-mails, and
1360 merges resulting from pulling the "public" repositories of your
1361 "project lead" and possibly your "sub-subsystem maintainers".
1362
1363 You can repack this private repository whenever you feel like.
1364
1365 6. Push your changes to your public repository, and ask your "project
1366 lead" and possibly your "sub-subsystem maintainers" to pull from
1367 it.
1368
1369 7. Every once in a while, git repack the public repository. Go back to
1370 step 5. and continue working.
1371
1372 A recommended work cycle for an "individual developer" who does not
1373 have a "public" repository is somewhat different. It goes like this:
1374
1375 1. Prepare your work repository, by git clone the public repository of
1376 the "project lead" (or a "subsystem maintainer", if you work on a
1377 subsystem). The URL used for the initial cloning is stored in the
1378 remote.origin.url configuration variable.
1379
1380 2. Do your work in your repository on master branch.
1381
1382 3. Run git fetch origin from the public repository of your upstream
1383 every once in a while. This does only the first half of git pull
1384 but does not merge. The head of the public repository is stored in
1385 .git/refs/remotes/origin/master.
1386
1387 4. Use git cherry origin to see which ones of your patches were
1388 accepted, and/or use git rebase origin to port your unmerged
1389 changes forward to the updated upstream.
1390
1391 5. Use git format-patch origin to prepare patches for e-mail
1392 submission to your upstream and send it out. Go back to step 2. and
1393 continue.
1394
1396 If you are coming from a CVS background, the style of cooperation
1397 suggested in the previous section may be new to you. You do not have to
1398 worry. Git supports the "shared public repository" style of cooperation
1399 you are probably more familiar with as well.
1400
1401 See gitcvs-migration(7) for the details.
1402
1404 It is likely that you will be working on more than one thing at a time.
1405 It is easy to manage those more-or-less independent tasks using
1406 branches with Git.
1407
1408 We have already seen how branches work previously, with "fun and work"
1409 example using two branches. The idea is the same if there are more than
1410 two branches. Let’s say you started out from "master" head, and have
1411 some new code in the "master" branch, and two independent fixes in the
1412 "commit-fix" and "diff-fix" branches:
1413
1414 $ git show-branch
1415 ! [commit-fix] Fix commit message normalization.
1416 ! [diff-fix] Fix rename detection.
1417 * [master] Release candidate #1
1418 ---
1419 + [diff-fix] Fix rename detection.
1420 + [diff-fix~1] Better common substring algorithm.
1421 + [commit-fix] Fix commit message normalization.
1422 * [master] Release candidate #1
1423 ++* [diff-fix~2] Pretty-print messages.
1424
1425
1426 Both fixes are tested well, and at this point, you want to merge in
1427 both of them. You could merge in diff-fix first and then commit-fix
1428 next, like this:
1429
1430 $ git merge -m "Merge fix in diff-fix" diff-fix
1431 $ git merge -m "Merge fix in commit-fix" commit-fix
1432
1433
1434 Which would result in:
1435
1436 $ git show-branch
1437 ! [commit-fix] Fix commit message normalization.
1438 ! [diff-fix] Fix rename detection.
1439 * [master] Merge fix in commit-fix
1440 ---
1441 - [master] Merge fix in commit-fix
1442 + * [commit-fix] Fix commit message normalization.
1443 - [master~1] Merge fix in diff-fix
1444 +* [diff-fix] Fix rename detection.
1445 +* [diff-fix~1] Better common substring algorithm.
1446 * [master~2] Release candidate #1
1447 ++* [master~3] Pretty-print messages.
1448
1449
1450 However, there is no particular reason to merge in one branch first and
1451 the other next, when what you have are a set of truly independent
1452 changes (if the order mattered, then they are not independent by
1453 definition). You could instead merge those two branches into the
1454 current branch at once. First let’s undo what we just did and start
1455 over. We would want to get the master branch before these two merges by
1456 resetting it to master~2:
1457
1458 $ git reset --hard master~2
1459
1460
1461 You can make sure git show-branch matches the state before those two
1462 git merge you just did. Then, instead of running two git merge commands
1463 in a row, you would merge these two branch heads (this is known as
1464 making an Octopus):
1465
1466 $ git merge commit-fix diff-fix
1467 $ git show-branch
1468 ! [commit-fix] Fix commit message normalization.
1469 ! [diff-fix] Fix rename detection.
1470 * [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
1471 ---
1472 - [master] Octopus merge of branches 'diff-fix' and 'commit-fix'
1473 + * [commit-fix] Fix commit message normalization.
1474 +* [diff-fix] Fix rename detection.
1475 +* [diff-fix~1] Better common substring algorithm.
1476 * [master~1] Release candidate #1
1477 ++* [master~2] Pretty-print messages.
1478
1479
1480 Note that you should not do Octopus just because you can. An octopus is
1481 a valid thing to do and often makes it easier to view the commit
1482 history if you are merging more than two independent changes at the
1483 same time. However, if you have merge conflicts with any of the
1484 branches you are merging in and need to hand resolve, that is an
1485 indication that the development happened in those branches were not
1486 independent after all, and you should merge two at a time, documenting
1487 how you resolved the conflicts, and the reason why you preferred
1488 changes made in one side over the other. Otherwise it would make the
1489 project history harder to follow, not easier.
1490
1492 gittutorial(7), gittutorial-2(7), gitcvs-migration(7), git-help(1),
1493 giteveryday(7), The Git User’s Manual[1]
1494
1496 Part of the git(1) suite
1497
1499 1. the Git User Manual
1500 file:///usr/share/doc/git/user-manual.html
1501
1502 2. Randy Dunlap’s presentation
1503 https://web.archive.org/web/20120915203609/http://www.xenotime.net/linux/mentor/linux-mentoring-2006.pdf
1504
1505
1506
1507Git 2.21.0 02/24/2019 GITCORE-TUTORIAL(7)