1 [[meta title="A tour of git: the basics"]]
2 A tour of git: the basics
6 This document is a modified version of a document originally titled
7 "Distributed revision control with Mercurial" and originally authored
8 by Bryan O’Sullivan. The original document was obtained from
9 <http://hgbook.red-bean.com/>.
11 Copyright © 2006, 2007 Bryan O’Sullivan.
13 This material may be distributed only subject to the terms and
14 conditions set forth in version 1.0 of the Open Publication
15 License. Please refer to Appendix D for the license text.
17 As this is a modified version, the name of Bryan O'Sullivan is used
18 only to properly credit him with the original text. The appearance of
19 his name here explicitly does not assert or imply his endorsement of
20 this modified document.
22 Portions Copyright © 2007 Carl Worth.
24 Changes made by Carl include the following:
27 * Convert from HTML to markdown source syntax
28 * Eliminate all content except Chapter 2 and Appendix D
29 * Eliminate line numbers from examples
30 * Modified to describe git instead of mercurial
32 The source of this modified version can be obtained via git:
34 git clone git://cworth.org/git/hgbook-git
38 git clone http://cworth.org/git/hgbook-git
40 and can be [browsed online](http://git.cworth.org/git/hgbook-git)
42 ### 2.1 Installing git on your system
44 Prebuilt binary packages of git are available for many popular
45 operating systems. These make it easy to start using git on your
50 Because each Linux distribution has its own packaging tools, policies,
51 and rate of development, it’s difficult to give a comprehensive set of
52 instructions on how to install git binaries. The version of
53 git that you will end up with can vary depending on how active
54 the person is who maintains the package for your distribution.
56 To keep things simple, I will focus on installing git from the
57 command line under the most popular Linux distributions. Most of these
58 distributions provide graphical package managers that will let you
59 install git with a single click. The package name to look for is
60 often git, but is sometimes git-core, (due to an unfortunate name
61 with git, meaning GNU Interactive Tools).
65 apt-get install git-core
81 apt-get install git-core
85 A git-core package is available through
86 [macports](http://macports.org). Once macports is enabled, the command
93 Git has long been available as part of cygwin, and works reasonably
94 well in that environment. Some people find cygwin a particularly
95 inelegant approach to running git and would prefer a "native"
96 solution. To this end, the [msysgit
97 project](http://code.google.com/p/msysgit/) is rapidly putting
98 together a solution including various packages with full
99 installers. These include GitMe, a package to install the entire
100 development environment necessary to work on improving the msysgit
101 port of git, and WinGit, a package for installing just git itself
102 without the development environment, (still in Alpha as of September
105 ### 2.2 Getting started
107 To begin, we’ll use the “git version” command to find out whether git
108 is actually installed properly. Versions 1.5 and newer of git are much
109 more friendly to new users than versions 1.4 and older. If you aren't
110 yet running version 1.5 or newer, it's highly recommended that you
116 #### 2.2.1 Built-in help
118 Git provides a built-in help system. This is invaluable for those
119 times when you find yourself stuck trying to remember how to run a
120 command. If you are completely stuck, simply run “git help”; it will
121 print a brief list of commonly-used commands, along with a description
122 of what each does. If you ask for help on a specific command (such as
123 "git help init"), it prints more detailed information. [XXX: Does `git
124 help <foo>` work universally as a built-in or does it expect man to be
125 present and just call out to `man git-<foo>`?]
127 [XXX: The original hgbook includes the complete output of "hg
128 help init" at this point. I'm not including the corresponding
129 "git help init" output as it would be excessively long. The
130 description alone is quite reasonable, (other than a
131 not-too-helpful aside about the obsolete git-init-db command),
132 but it only comes after a full screen's worth of options
133 details. Might it make sense to have a more summarized help
134 output for "git help <foo>" than all of the documentation
135 available for git-<foo>? And perhaps alos provide a "git -v
136 help" similar to "hg -v help" for more?]
138 ### 2.3 Working with a repository
140 In git, everything happens inside a repository. The repository
141 for a project contains all of the files that “belong to” that project,
142 along with a historical record of the project’s files.
144 There’s nothing particularly magical about a repository; it is simply
145 a directory tree in your filesystem that git treats as
146 special. You can rename or delete a repository any time you like,
147 using either the command line or your file browser.
149 #### 2.3.1 Creating a copy of a remote repository
151 The "git clone" command is used to create a local copy of a remote
152 repository. This is generally the first git operation you will use
153 when beginning to work with an existing project.
155 We've assembled a simple repository that will be used in the examples
156 throughout this chapter. Go ahead and clone this repository now so
157 that you will be able to follow along:
159 $ git clone git://cworth.org/git/hello
160 Initialized empty Git repository in /tmp/hello/.git/
161 remote: Generating pack...
162 remote: Done counting 15 objects.
163 remote: Deltifying 15 objects...
164 remote: 100% (15/15) done
165 remote: Total 15 (delta 2), reused 15 (delta remote: 2)
166 Indexing 15 objects...
168 Resolving 2 deltas...
171 If for some reason you are prevented from talking on the git: port,
172 then there is also the capability to clone a repository (less
173 efficiently) over http:
175 $ git clone http://cworth.org/git/hello
176 Initialized empty Git repository in /tmp/hello/.git/
177 Getting alternates list for http://cworth.org/git/hello
178 Getting pack list for http://cworth.org/git/hello
179 Getting index for pack 04ecb061314ecbd60fa0610ecf55a1cbf85ea294
180 Getting pack 04ecb061314ecbd60fa0610ecf55a1cbf85ea294
181 which contains a1a0e8b392b17caf50325498df54802fe3c03710
182 walk a1a0e8b392b17caf50325498df54802fe3c03710
183 walk 72d4f10e4a27dbb09ace1503c20dbac1912ee451
184 walk 13ed136b983a9c439eddeea8a1c2076cffbb685f
185 walk 0a633bf58b45fcf1a8299d3c82cd1fd26d3f48f2
186 walk db7117a9dd9a6e57e8632ea5848e1101eee0fbde
188 If our clone succeeded, we should now have a local directory called
189 hello. This directory will contain some files.
193 drwxr-xr-x 3 cworth cworth 4096 2007-09-27 16:40 hello
197 These files have the same contents and history in our repository as
198 they do in the repository we cloned.
200 Every git repository is complete, self-contained, and
201 independent. It contains its own private copy of a project’s files and
202 history. A cloned repository remembers the location of the repository
203 it was cloned from, but it does not communicate with that repository,
204 or any other, unless you tell it to.
206 What this means for now is that we’re free to experiment with our
207 repository, safe in the knowledge that it’s a private “sandbox” that
208 won’t affect anyone else.
210 #### 2.3.2 Creating copy of a local repository
212 As mentioned above, a repository can be copied through normal
213 file-copying commands. But it's useful to use "git clone" even when
214 just making a local copy of a repository. Using "git clone" will be
215 much faster and will use much less space than a normal copy. In fact,
216 local clones are impressively fast. Go ahead and make a local clone
219 $ git clone hello hello-clone
220 Initialized empty Git repository in /tmp/hello-clone/.git/
223 [XXX Git says "empty" here, (presumably from the git-init
224 part), but shouldn't the command also report the succesful
225 clone which makes it non-empty? And what the heck does "0
226 blocks" mean? This is not friendly output.]
228 In fact, let's make a couple more clones at the same time. This isn't
229 just to drive home the speed and storage benefits of local clones, but
230 we'll use each of these cloned repositories in Section 2.8 when
231 discussing how to move commits between repositories:
233 $ git clone hello hello-pull
234 $ git clone hello hello-fetch
235 $ git clone hello hello-remote
237 #### 2.3.2 What’s in a repository?
239 When we take a more detailed look inside a repository, we can see that
240 it contains a directory named .git. This is where git keeps all
241 of its metadata for the repository.
245 . .. .git hello.c Makefile
247 The contents of the .git directory and its subdirectories are private
248 to git. Every other file and directory in the repository is
249 yours to do with as you please.
251 To introduce a little terminology, the .git directory is the “real”
252 repository, and all of the files and directories that coexist with it
253 are said to live in the working directory. An easy way to remember the
254 distinction is that the repository contains the history of your
255 project, while the working directory contains a snapshot of your
256 project at a particular point in history.
258 ### 2.4 A tour through history
260 One of the first things we might want to do with a new, unfamiliar
261 repository is understand its history. The “git log” command gives us a
265 commit a1a0e8b392b17caf50325498df54802fe3c03710
266 Author: Bryan O'Sullivan <bos@serpentine.com>
267 Date: Tue Sep 6 15:43:07 2005 -0700
271 commit 72d4f10e4a27dbb09ace1503c20dbac1912ee451
272 Author: Bryan O'Sullivan <bos@serpentine.com>
273 Date: Tue Sep 6 13:15:58 2005 -0700
275 Get make to generate the final binary from a .o file.
277 commit 13ed136b983a9c439eddeea8a1c2076cffbb685f
278 Author: Bryan O'Sullivan <bos@serpentine.com>
279 Date: Tue Sep 6 13:15:43 2005 -0700
281 Introduce a typo into hello.c.
283 commit 0a633bf58b45fcf1a8299d3c82cd1fd26d3f48f2
284 Author: Bryan O'Sullivan <mpm@selenic.com>
285 Date: Fri Aug 26 01:21:28 2005 -0700
289 commit db7117a9dd9a6e57e8632ea5848e1101eee0fbde
290 Author: Bryan O'Sullivan <mpm@selenic.com>
291 Date: Fri Aug 26 01:20:50 2005 -0700
293 Create a standard "hello, world" program
295 This command prints a record of output for each change to the project
296 that was recorded. In git terminology, we call each of these recorded
299 The default fields in a record of output from “git log” are as follows.
301 * commit This field consists of a string of 40 hexadecimal characters.
302 This is a unique identifier for referring to particular commits.
303 * Author The identity of the person who authored the commit. This
304 field consist of two sub-fields for the user's name and email
305 address, (or at least an email-like idenitifer). Note that git
306 also stores a separate "Committer" field for the person who
307 commited the change, (since often an author will email a change to
308 a maintainer that commits it). See below for how to instruct "git
309 log" to display it as well.
310 * Date The date and time on which the commit was authored, (again
311 stored separately from the date the change was committed).
312 timezone in which it was created. (The date and time are displayed
313 in the timezone of the person who created the commit.)
314 * commit message The text message that the creator of the commit
315 entered to describe the commit, (generally a one-line summary
316 followed by more supporting text).
318 The output of the "git log" command can be made more or less verbose
319 by means of the --pretty option. For example, with "git log
320 --pretty=short" the commit identifier will be omitted and only the
321 first line of each commit message will be shown. And with "git log
322 --pretty=fuller", (the name 'fuller' is in contrast to the default
323 --pretty=full), the committer name and dates will be printed in
324 addition to the author name and dates.
326 #### 2.4.1 Commits, revisions, and talking to other people
328 As English is a notoriously sloppy language, and computer science has
329 a hallowed history of terminological confusion (why use one term when
330 four will do?), revision control has a variety of words and phrases
331 that mean the same thing. If you are talking about git history
332 with other people, you will find that what we have called a “commit”
333 is often called a "revision". In other systems, a similar notion
334 is referred to as a "changeset". You might even see abbreviations of
335 these terms such as "rev", "change", or even "cset".
337 While it may not matter much what word you use to refer to the concept
338 of “a commit”, it's important to know how to name “a specific
339 commit”. We have already seen one means of referring to a particular
340 commit, the 40-character hexadecimal string shown by "git log". These
341 commit identifiers are powerful because they are permanent, unique
342 identifiers that always identify the same commit in any copy of a
343 repository. If two users are examining a working directory associated
344 with the same commit identifier, then those two users have precisely
345 the same contents in all files, and exactly the same history leading
348 So there are places where it is often important to archive the
349 complete commit identifier, (perhaps in bug-tracking systems to
350 indicate a specific commit that fixes a bug, for example). But often,
351 in more casual settings, it's more convenient to use abbreviated
352 commit identifiers. Git accepts any unique prefix of a commit
353 identifier, (and for reasonably-sized projects the first 8 or 10
354 characters are almost always unique).
356 And unlike the permanent commit identifiers, git also provides
357 transient means of identifying commits. In fact, in day-to-day use of
358 git, you will probably use these names more than commit
359 identifiers. One example is branch names, (such as the default
360 "master" branch in any git repository), or any project-specific branch
361 names such as "stable", "experimental", or "crazy-insane-changes". Git
362 also provides a special name "HEAD" which always refers to the current
365 #### 2.4.2 Naming related commits
367 Git offers simple ways to name revisions that are related to
368 particular revisions in the history. One syntax is the ~ suffix which
369 refers to the parent of a commit, or if followed by a number, to the
370 Nth parent. For example, since "HEAD" refers to the most recent commit
371 in the current branch, "HEAD~", refers to the previous commit, and
372 "HEAD~2" refers to two commits back in the history.
374 Another useful syntax is .. which can be used to specify a range of
375 commits. So "origin..master" specifies everything that has been
376 committed to master since it diverged from origin.
378 #### 2.4.3 Viewing specific revisions
380 You can use "git log" to explore the range syntax just introduced. For
381 example, to see a list of the most recent 3 revisions you can use
382 "HEAD~3..", (the destination of the range is implicitly HEAD in this
386 commit a1a0e8b392b17caf50325498df54802fe3c03710
387 Author: Bryan O'Sullivan <bos@serpentine.com>
388 Date: Tue Sep 6 15:43:07 2005 -0700
392 commit 72d4f10e4a27dbb09ace1503c20dbac1912ee451
393 Author: Bryan O'Sullivan <bos@serpentine.com>
394 Date: Tue Sep 6 13:15:58 2005 -0700
396 Get make to generate the final binary from a .o file.
398 commit 13ed136b983a9c439eddeea8a1c2076cffbb685f
399 Author: Bryan O'Sullivan <bos@serpentine.com>
400 Date: Tue Sep 6 13:15:43 2005 -0700
402 Introduce a typo into hello.c.
404 #### 2.4.4 Other log filters
406 Besides filtering by commit identifiers, git allows you to easily
407 filter the log output according to which files (or directories) are
408 modified by listing them after "--" which is necessary to distinguish
409 commit names from file names:
411 $ git log -- Makefile
412 commit 72d4f10e4a27dbb09ace1503c20dbac1912ee451
413 Author: Bryan O'Sullivan <bos@serpentine.com>
414 Date: Tue Sep 6 13:15:58 2005 -0700
416 Get make to generate the final binary from a .o file.
418 commit 0a633bf58b45fcf1a8299d3c82cd1fd26d3f48f2
419 Author: Bryan O'Sullivan <mpm@selenic.com>
420 Date: Fri Aug 26 01:21:28 2005 -0700
424 And "git log" can also filter based on the dates at which commits were
427 $ git log --since="2 weeks ago" --until="yesterday"
429 [XXX: By default, "git log" displays author dates as "Date"
430 but then uses commit dates when given a --since option. That
431 seems like broken defaults to me. Why the inconsistency?]
433 Another useful option is -n or --max-count which, unsurprisingly,
434 limits the maximum number of commits to be displayed.
436 #### 2.4.5 More detailed information
438 While the default information printed by “git log” is useful if you
439 already know what you’re looking for, you may need to see more details
440 of the change, such as the "diffstat" information with --stat:
442 $ git log --stat --max-count=3
443 commit a1a0e8b392b17caf50325498df54802fe3c03710
444 Author: Bryan O'Sullivan <bos@serpentine.com>
445 Date: Tue Sep 6 15:43:07 2005 -0700
450 1 files changed, 2 insertions(+), 6 deletions(-)
452 commit 72d4f10e4a27dbb09ace1503c20dbac1912ee451
453 Author: Bryan O'Sullivan <bos@serpentine.com>
454 Date: Tue Sep 6 13:15:58 2005 -0700
456 Get make to generate the final binary from a .o file.
459 1 files changed, 2 insertions(+), 0 deletions(-)
461 commit 13ed136b983a9c439eddeea8a1c2076cffbb685f
462 Author: Bryan O'Sullivan <bos@serpentine.com>
463 Date: Tue Sep 6 13:15:43 2005 -0700
465 Introduce a typo into hello.c.
468 1 files changed, 1 insertions(+), 1 deletions(-)
470 Or perhaps you'd like to see the actual patch content of each change,
471 which you can get with -p. That commit with the word typo in its name
472 looks suspicious, so let's take a closer look. Remember that we can name
473 it as master~3, HEAD~3, or any prefix of its commit identifier, (such
476 $ git log -p -n 1 13ed136b
477 commit 13ed136b983a9c439eddeea8a1c2076cffbb685f
478 Author: Bryan O'Sullivan <bos@serpentine.com>
479 Date: Tue Sep 6 13:15:43 2005 -0700
481 Introduce a typo into hello.c.
483 diff --git a/hello.c b/hello.c
484 index ed55ec0..80b260c 100644
489 int main(int argc, char **argv)
491 - printf("hello, world!\n");
492 + printf("hello, world!\");
496 Of course, wanting to see all this information for a single commit is
497 such a common operation that it's given its own name in git, "git
498 show". So "git show 13ed136b" is a much easier way to get exactly the
502 commit 13ed136b983a9c439eddeea8a1c2076cffbb685f
503 Author: Bryan O'Sullivan <bos@serpentine.com>
504 Date: Tue Sep 6 13:15:43 2005 -0700
506 Introduce a typo into hello.c.
508 diff --git a/hello.c b/hello.c
509 index ed55ec0..80b260c 100644
514 int main(int argc, char **argv)
516 - printf("hello, world!\n");
517 + printf("hello, world!\");
521 ### 2.5 All about command options
523 Let’s take a brief break from exploring git commands to discuss
524 a pattern in the way that they work; you may find this useful to keep
525 in mind as we continue our tour.
527 Git has a consistent and straightforward approach to dealing
528 with the options that you can pass to commands. It follows the
529 conventions for options that are common to modern Linux and Unix
532 * Most options have long names. For example, as we’ve already seen,
533 the “git log" command accepts a `--max-count=<number>` option.
534 * Some options have short, single-character names. Often these are
535 aliases for long commands, (such as `-n <number>` instead of
536 `--max-count=<number>`), but sometimes the option exists in
537 short-form with no long-form equivalent, (such as `-p`). [XXX: It
538 wouldn't hurt to fix this by adding `--patch`, etc. right?]
539 * Long options start with two dashes (e.g. `--max-count`), while short
540 options start with one (e.g. `-n`).
542 * Option naming and usage is consistent across commands. For
543 example, every command that lets you specify a commit identifier
544 or range will accept the same expressions, (HEAD~3,
545 origin..master, 72d4f10e, etc), while any command that can be
546 limited by paths will accept the same expressions ("-- doc/
549 Many commands that print output of some kind can be made more quiet by
550 passing the -q or --quiet options.
552 ### 2.6 Making and reviewing changes
554 Now that we have a grasp of viewing history in git, let’s take a look
555 at making some changes and examining them. You should be working
556 within the "hello" directory that we originally cloned.
558 In the repository, we have a file hello.c that contains the classic
559 “hello, world” program. But this program currently has a syntax error
560 and won't compile, (there's a missing 'n' between the '\' and the '"'
561 on the line containing printf). Let's fix that now. You can use a text
562 editor to correct the file, or you can copy-and-paste the sed command
565 $ sed -i '/printf/s/\\"/\\n"/' hello.c
567 After you've made that change, the “git status” command will tell you
568 what git knows about the files in the repository.
574 # Changed but not updated:
575 # (use "git add <file>..." to update what will be committed)
579 no changes added to commit (use "git add" and/or "git commit -a")
581 First "git status" tells us that the current branch is "master". This
582 means that the master branch is what will be updated when we create a
585 Note: In git a branch is a very simple notion---it's simply a name
586 that points to a particular commit, (literally nothing more than a
587 pointer---look at the contents of .git/refs/heads/master if you're
588 curious). The fact that a branch is so light is what makes the
589 creation of new branches an instantaneous operation in git. Together
590 with the ease of merging, git makes branches a joy to work with. But
591 we'll delay talk of branching and merging to a future chapter.
593 Next “git status” prints a line with "modified" for each modified
594 file---in this case just hello.c. The “git status” command will not
595 print any output for files that have not been modified.
597 Notice that we didn’t need to inform git that we were going to modify
598 the file before we started, or that we had modified the file after we
599 were done; it was able to figure this out itself.
601 It’s a little bit helpful to know that we’ve modified hello.c, but we
602 might prefer to know exactly what changes we’ve made to it. To do
603 this, we use the “git diff” command.
606 diff --git a/hello.c b/hello.c
607 index 9a3ff79..ea364d3 100644
612 int main(int argc, char **argv)
614 - printf("hello, world!\");
615 + printf("hello, world!\n");
619 ### 2.7 Recording changes in a new commit
621 We can modify files, build and test our changes, and use “git status”
622 and “git diff” to review our changes, until we’re satisfied with what
623 we’ve done and arrive at a natural stopping point where we want to
624 record our work in a new commit.
626 If you'd like, go ahead and build the program now with "make" and run
627 it to ensure it works. Everything should look good so we're just about
628 ready to make a commit. Unsurprisingly, we'll be using "git commit" to
631 #### 2.7.1 Introducing yourself to git
633 Before you run "git commit" though, you should introduce yourself to
634 git. Git records your name and email address with each change that
635 you commit, (as both author and committer unless you tell it
636 otherwise), so that you and others will later be able to tell who made
639 Git tries to automatically figure out a sensible name and address to
640 attribute to both author and committer if you haven't explicitly told
641 it a name and address. And it tries a lot, (detailed below). If you're
642 not interested in these details, you might want to skip to the next
643 section which explains how to avoid all this guesswork and tell git
644 what your name and email address are.
646 Here is a list of all the guessing that git will attempt. It will
647 attempt each of the following methods, in order, (stopping for each of
648 the author and committer name and email as soon as a value is found):
650 1. If you specify a --author option to the “git commit” command on
651 the command line, followed by a `"Real Name <email@example.com>"`
652 string, then this name and addresss will be used for the author
653 fields. The committer fields will still be determined as
654 below. This option is very helpful for when applying a commit
655 originally authored by someone other than yourself.
656 2. If any of the `GIT_AUTHOR_NAME`, `GIT_AUTHOR_EMAIL`,
657 `GIT_COMMITTER`_NAME, or `GIT_COMMITER_EMAIL` environment variables
658 are set, then those values will be used for the corresponding
660 3. If you have a file in your home directory called .gitconfig, with
661 name or email settings in the [user] section, then these values
662 will be used to set any remaining author and committer
663 fields. For more details on the contents of this file, refer to
665 4. If you have a file in the local repository called .git/config,
666 again with name or email settings in the [user] section, then
667 these values will be used to set any remaining author and
669 5. If you have set the `EMAIL` environment variable, this will be used
670 to set author and committer email addresses if still unset.
671 6. git will query your system to find out your real name from
672 available GECOS field and your username, hostname, and domain to
673 construct an email address, (or at least an identifier resembling
676 If all of these mechanisms fail, "git commit" will fail, printing an
677 error message instructing you how to use "git config" to tell git your
678 name and email address.
680 You should think of the `GIT_AUTHOR`/`COMMITER_NAME`/`EMAIL` environment
681 variables and the --author option to the “git commit” command as ways
682 to override git’s default selection. For normal use, the simplest and
683 most robust way to set your information is by creating a .gitconfig
684 file, (either manually or with the "git config" command); see below
687 ##### Creating a git configuration file
689 To set your name and email address, just use the following commands:
691 git config --global user.name "Your Name"
692 git config --global user.email "you@example.com"
694 The --global option means that this command will set global
695 information, (affecting all repositories on this machine), in the
696 .gitconfig file in your home directory. Alternately, you could omit
697 the --global which would make the change take effect only in the local
698 repository. This is convenient if you want to have different email
699 addresses associated with different projects, for example.
701 Of course, git's configuration file is a simple-to-edit plain-text
702 file, so instead of using the above commands, you can also just edit
703 the files directly. Use your favorite editor to create a file called
704 .gitconfig in your home directory, (or if you ran the above commands
705 then it will be there already). The initial contents of your
706 .gitconfig should look like this.
708 # This is a git configuration file.
711 email = you@example.com
713 Similarly, you can make a repository-specific configuration by editing
714 .git/config in the local repository. It will already have some
715 sections present, (created by the "git clone"), just add a [user]
718 The “[user]” line begins a section of the config file, so you can read
719 the “name = ...” line as meaning “set the value of the name item in
720 the user section”. This is the same notion expressed with the
721 "user.name" syntax on the git-config command line. A section
722 continues until a new section begins, or the end of the file. Git
723 ignores empty lines and treats any text from “#” to the end of a line
726 ##### Choosing your name and email
728 You can use any text you like as the value of the name and email
729 configuration items, since this information is for reading by other
730 people, not for interpreting by git. It is conventional to use your
731 actual name as well as a valid email address. But some people, (notably
732 Linus Torvalds, the original author of git), actually like the default
733 username@hostname convention that git falls back on without any
734 additional information about an email address. There's no requirement
735 that the email address actually be valid, and perhaps it's useful to
736 be reminded which machine was used to create particular commits. So
737 choose the name and email you wish, or follow a particular project's
740 #### 2.7.2 Writing a commit message
742 When we commit a change, git drops us into a text editor to
743 enter a message that will describe the modifications we’ve made in
744 this commit. This is called the commit message. It will be a record
745 for readers of what we did and why, and it will be printed by “git log”
746 after we’ve finished committing.
750 Note: The -a on the command-line instructs git to commit the new
751 content of *all* tracked files that have been modified. This is a
752 convenience over explicitly listing filenames to be committed on the
753 `git commit` command line. It is useful to use `git commit <files>`
754 when there is a need to commit only some subset of the files that have
757 If new files need to be committed for the first time, just use `git
758 add <file>` before `git commit -a`. If a file needs to be removed,
759 just remove it as normal before committing and `git commit -a` will
760 notice that---it does not need to be explicitly told about the
763 The editor that the `git commit` command drops us into will contain an
764 empty line, followed by a number of lines starting with “#”. These
765 lines contain the same information as seen in `git status` before:
768 # Please enter the commit message for your changes.
769 # (Comment lines starting with '#' will not be included)
771 # Changes to be committed:
772 # (use "git reset HEAD <file>..." to unstage)
777 # (use "git add <file>..." to include in what will be committed)
782 Notice that two untracked files (hello and hello.o) have now appeared
783 from the build process. Git is reminding us of these in case we
784 intended to commit them as well, (in which case we would need to "git
785 add" them). We don't actually want to commit these files so we will
786 ignore them for now, (and we could tell git to ignore them as well by
787 listing them in a .gitignore file).
789 Git will ignore all lines in the commit message that start with “#”;
790 it uses them only to give us information on the commit. Modifying or
791 deleting these lines has no effect.
793 #### 2.7.3 Writing a good commit message
795 A good commit message will generally have a single line that
796 summarizes the commit, a blank line, and then one or more paragraphs
797 with supporting detail. Since many tools only print the first line of
798 a commit message by default, it’s important that the first line stands
801 One example of a first-line-only viewer is "git log
802 --pretty=short". Other examples include graphical history viewers such
803 as gitk and gitview, and web-based viewers such as gitweb and cgit.
805 Here’s a real example of a commit message that doesn’t follow
806 this guideline, and hence has a summary that is not readable.
808 $ git log --pretty=short
809 commit 3ef5535144da88a854f7930503845cd44506c2e2
810 Author: Censored Person <censored.person@example.org>
812 include buildmeister/commondefs. Add an exports and install
814 As far as the remainder of the contents of the commit message are
815 concerned, there are no hard-and-fast rules. git itself doesn’t
816 interpret or care about the contents of the commit message, though
817 your project may have policies that dictate a certain kind of
820 My personal preference is for short, but informative, commit messages
821 that tell me something that I can’t figure out with a quick glance at
822 the output of "git log --stat" or “git log -p", (so repeating the list
823 of all modified files is not useful, for example).
825 To follow along with the example here, go ahead and type something
826 like the following sentence into the editor. The misspelling here is
827 intentional. You'll see how to fix that up after the fact in just a
830 Fixed the typo so the program actuall complies now.
832 Then save the file, and exit from the editor. When you do that, git
833 will create the commit.
835 #### 2.7.4 Aborting a commit
837 If you decide that you don’t want to commit while in the middle of
838 editing a commit message, simply exit from your editor without saving
839 the file that it’s editing. This will cause nothing to happen to
840 either the repository or the working directory.
842 #### 2.7.5 Admiring our new handiwork
844 Once we’ve finished the commit, we can use the “git show” command to
845 display the commit we just created. As discussed previously, this
846 command produces output that is identical to “git log -p”, but for
847 only a single revision, (and the most recent revision by default):
850 commit fd21e5d6c5eedee70137229ebf348c25181812ab
851 Author: Carl Worth <cworth@cworth.org>
852 Date: Fri Sep 28 12:50:16 2007 -0700
854 Fixed the typo so the program actuall complies now.
856 diff --git a/hello.c b/hello.c
857 index 9a3ff79..ea364d3 100644
862 int main(int argc, char **argv)
864 - printf("hello, world!\");
865 + printf("hello, world!\n");
869 Note that you will not see the same commit identifier for your commit,
870 even if the change you made is identical to mine. The commit
871 identifier incorporates not only the contents of the files, but commit
872 message, the author and committer names and emails, and the author and
873 commit dates. (OK, so now you probably know enough to be able to guess
874 the right command to produce a commit with exactly the commit
875 identifier shown above. Can you do it?)
877 #### 2.7.6 Fixing up a broken commit (before anyone else sees it)
879 So now that we've cloned a local repository, made a change to the
880 code, setup our name and email address, and made a careful commit,
881 we're just about ready to share our change with the world. But wait,
882 that commit message has that embarrassing misspelling in
883 it. Wouldn't it be nice to touch that up before we post this commit
884 with a never-to-be-changed again commit identifier?
886 This is the exact situation for which "git commit --amend" was
887 invented. So you can just run that now and fix the broken commit
888 message in the editor:
892 Here's the final result:
895 commit 3c54ac672ec1130b36837f1b708054a7a1d402de
896 Author: Carl Worth <cworth@cworth.org>
897 Date: Fri Sep 28 12:50:16 2007 -0700
899 Fixed the typo so the program actually compiles now.
901 diff --git a/hello.c b/hello.c
902 index 9a3ff79..ea364d3 100644
907 int main(int argc, char **argv)
909 - printf("hello, world!\");
910 + printf("hello, world!\n");
914 Note that we didn't use "commit -a" this time. This means that "git
915 commit --amend" will amend only the commit message and not any of the
916 actual files being tracked, (even if some of them had been modified
917 between the commits).
919 It's also possible to use "git commit -a --amend" to similarly fix up
920 mistakes noticed in code. That will replace the most recent commit
921 with a different commit based on any new changes to files.
923 I do feel a little hesitant to mention "git commit -a --amend". It's a
924 handy command for fixing up something like a misspelling in a comment
925 in the code. But if there is anything more significant than that, then
926 it would generally be better to create an additional commit rather
927 than amending an existing commit. This is important for several
930 * The amend operation will destroy a state that was previously saved
931 in a commit. If it's just the commit message being changed, then
932 that's no big deal. But if the contents are being amended, then a
933 mistake could eliminate something valuable.
935 * All commits should be logically independent and as small as
936 possible. Abusing "git commit -a --amend" can cause a small commit
937 to grow and acquire unrelated changes.
939 It's worth emphasizing the value of minimal, independent commits. The
940 smaller the changes are the more useful the history will be when
941 actually using the history, not just viewing it. This is particularly
942 important when doing "git bisect"---that's a powerful tool for
943 isolating a single commit that introduces a bug. And it's much more
944 helpful when the commit it isolates is as small as possible.
946 One advantage of using git over some other systems is that the commit
947 speed is blazingly fast. The tool doesn't punish you at all for
948 committing every time your project is in a state worth saving. "Commit
949 early, commit often" is a well-supported mode of operation with git.
951 ### 2.8 Sharing changes
953 We mentioned earlier that repositories in git are
954 self-contained. This means that the commit we just created exists
955 only in our hello repository. Let’s look at a few ways that we can
956 propagate this change into other repositories.
958 #### 2.8.1 Pulling changes from the original repository
960 Recall that in Section 2.3.2 we made several local clones of the hello
961 repository before we made any commits. This allows us to simulate what
962 happens when upstream changes have been committed after you originally
965 The simplest, (and quite common), scenario is that you inherently
966 trust any changes in the original repository and you want to pull
967 these directly into your clone. This might be the case if you are
968 using git simply to track the progress of a project without making any
971 In this case, the operation is as simple as just calling "git pull":
975 remote: Generating pack...
976 Unpacking 3 objects...
978 remote: Done counting 5 objects.
979 Result has 3 objects.
980 Deltifying 3 objects...
981 100% remote: (3/3) done
982 Total 3 (delta 1), reused 0 (delta 0)
983 * refs/remotes/origin/master: fast forward to branch 'master' of /tmp/hello
984 old..new: a1a0e8b..3c54ac6
985 Updating a1a0e8b..3c54ac6
988 1 files changed, 1 insertions(+), 1 deletions(-)
990 XXX: Git is fairly noisy here, but a user has little need to care
991 about the several stages of operation involved here. As a tutorial
992 writer I'd like to say "ignore all that progress stuff, and look at
993 where the stat information starts" but it's hard for a new user to
994 even be able to understand that. I think it would be ideal if all of
995 the progress-tracking spew were reduced to a single line. Something
996 like "Computing (100%) Transferring (100%)" or whatever.
998 After (lots!) of progress indication, git gives a report of which
999 files were modified, (which is very useful for getting a quick feel
1000 for what happened). If you would like more details on what changes
1001 came in, git provides a range that is perfect for examining. Let's
1002 take a look (again, the commit identifiers will be different for you
1003 --- just copy-and-paste the range that git prints):
1005 $ git log a1a0e8b..3c54ac6
1006 commit 3c54ac672ec1130b36837f1b708054a7a1d402de
1007 Author: Carl Worth <cworth@cworth.org>
1008 Date: Fri Sep 28 12:50:16 2007 -0700
1010 Fixed the typo so the program actually compiles now.
1012 As expected, we received just the one commit.
1014 So that's all that's needed in the common case. Just run "git pull"
1015 everytime you want to pull in new changes that have landed in the
1016 upstream repository.
1018 Note: Mercurial users who are reading this might wonder if there's a
1019 need for the equivalent of "hg update" after doing a "git pull". And
1020 the answer is no. Unlike mercurial, "git pull", (and "git merge") will
1021 automatically update the working-directory files as necessary.
1023 #### 2.8.2 Using fetch and merge separately to pull
1025 Sometimes you may not know if you want to pull in the changes from the
1026 remote repository or not. It's useful to be able to examine them
1027 before accepting them into our branch. The "git pull" command shown in
1028 the previous section is conceptually the combination of two commands,
1029 "git fetch" and "git merge". We can use these commands separately to
1030 examine the change before accepting it.
1032 So let's do that within the hello-fetch clone we made earlier. First
1033 we will do the fetch:
1037 remote: Generating pack...
1038 Unpacking 3 objects...
1039 remote: Done counting 5 objects.
1040 Result has 3 objects.
1041 Deltifying 3 objects...
1042 100% remote: (3/3) done
1043 Total 3 (delta 1), reused 0 (delta 0)
1045 * refs/remotes/origin/master: fast forward to branch 'master' of /tmp/hello/
1046 old..new: a1a0e8b..3c54ac6
1048 You may notice that the output here looks very much like the first
1049 portion of the output from "git pull". This is no coincidence. The
1050 new changes have been "fetched" into the current repository and are
1051 stored into "origin/master" and have not been into the current
1052 "master" branch. Remember that "master" is our current branch. So now,
1053 "origin/master" is the state of the master branch that exists in the
1054 "origin" repository, (the one we cloned from).
1056 The most convenient way to examine the fetched changes is with the
1057 "master..origin" range notation:
1059 $ git log master..origin
1060 commit 3c54ac672ec1130b36837f1b708054a7a1d402de
1061 Author: Carl Worth <cworth@cworth.org>
1062 Date: Fri Sep 28 12:50:16 2007 -0700
1064 Fixed the typo so the program actually compiles now.
1066 Another helpful way of visualizing what happened with "git fetch" here
1067 is to run "gitk --all", which gives a graphical representation of all
1068 branches. Here is what it would look like:
1070 [[img gitk-fetch.png]]
1072 Notice that origin/master points to a single commit that was committed
1073 on top of the state pointed to by the "master" branch.
1075 Let's assume we are happy with the changes and we want to include them
1076 into our master branch. To do this we simply run "git merge origin":
1079 Updating a1a0e8b..3c54ac6
1082 1 files changed, 1 insertions(+), 1 deletions(-)
1084 Again, you'll see that this precisely matches the final portion of the
1085 output from "git pull". Using "git fetch" and "git merge" let us
1086 achieve exactly what "git pull" did, but we were able to stop in the
1087 middle to examine the situation, (and we could have decided to reject
1088 the changes and not merge them---leaving our master branch unchanged).
1090 ##### On merges and "fast forward"
1092 You'll notice that we've been seeing the phrase "fast forward" several
1093 times. This is a special-case operation performed by "git merge" where
1094 a branch can be advanced along a linear sequence. This happens
1095 whenever you pull changes that build directly on top of the same
1096 commit you have as your most recent commit. In other words, there was
1097 never any divergence or simultaneous commits created in parallel in
1098 multiple repositories. If there had been parallel commits, then "git
1099 merge" would actually introduce a new merge commit to tie the two
1102 When a non-fast-forward merge occurs, there is always the possibility
1103 that a conflict occurs. In this case, "git merge" will leave conflict
1104 markers in the files and instruct you to resolve the conflicts. When
1105 you are finished, you would issue a "git commit -a" to create the
1108 #### 2.8.3 Using "git remote" to pull changes from other repositories
1110 We've already described how "git pull" will pull in changes from the
1111 repository which was the origin of the clone operation. Git also
1112 provides excellent support for pulling changes from any other
1113 repository as well, (distributed, rather than centralized
1116 If you have a situation where you want to pull a single time from some
1117 repository, then you can simply give the path or URL of the repository
1118 on the "git pull" command line. However, it's often the case that if
1119 you want to pull changes from a repository once, you'll want to pull
1120 changes from that same repository again in the future. This is where
1121 the "git remote" notion is extremely useful---it allows you to
1122 associate simple names, (and behaviors), with remote repository URLs
1124 We've already seen one instance of "git remote" which is the creation
1125 of the "origin" remote which happens automatically during "git
1126 clone". Let's now create another. Let's assume you are going to be
1127 working in the hello-remote repository and you'd like to pull changes
1128 from the hello-pull repository, where your friend "fred" has been
1129 making changes. Here's how to setup the new remote:
1131 $ cd ../hello-remote
1132 $ git remote add fred ../hello-pull
1134 So that's a "git remote add" command line followed by an arbitrary
1135 name you'd like for the new remote (fred) and the URL of the remote
1136 (../hello-pull). Obviously, the URL could be a git:// URL or any other
1137 git-supported URL in addition to a local path.
1139 The "git remote" command is really just a helper for adding some
1140 entries to the .git/config file. You might find it more convenient to
1141 edit that file directly once you get comfortable with things.
1143 At this point the name "fred" will work much like the name "origin"
1144 has worked in previous examples. For example, we can fetch the changes
1145 fred has made with "git fetch fred":
1148 remote: Generating pack...
1149 Unpacking 3 objects...
1150 remote: Done counting 5 objects.
1151 Result has 3 objects.
1152 Deltifying 3 objects...
1153 100% remote: (3/3) done
1154 Total 3 (delta 1), reused 0 (delta 0)
1156 * refs/remotes/fred/master: storing branch 'master' of ../hello-pull
1159 Notice that this command-line only differs from the "git fetch" we did
1160 previously by explicitly naming which remote should be fetched. We
1161 could have explicitly said "git fetch origin" earlier.
1163 We can also list all known remote-tracking branches with "git branch
1171 These remote-tracking branches make it very easy to collaborate with
1172 people as they are working on experimental features not yet ready for
1173 upstream inclusion. For example, if fred's latest code is still
1174 trashing filesystems then he might not want to push it out to the
1175 project's primary repository. But he may still want my help with
1176 it. So he can push it to a branch in his own repository for which I've
1177 got a remote. Then on my next "git fetch fred" I might notice a new
1178 branch called fred/trashes-filesystems and I can examine his code with
1179 a command such as "git log ..fred/trashed-filesystems".
1181 So lots of side collaboration can go on easily, and people working
1182 only with the primary repository never even have to see this dangerous
1183 code. It's distributed development at its finest.
1185 #### 2.8.4 Checking out previous revisions
1187 It's often useful to examine the working-tree state of some specific
1188 revision other than the tip of some branch. For example, maybe you
1189 would like to build a particular tagged version, or maybe you'd like
1190 to test the behavior of the code before a particular change was
1191 introduced. To do this, use "git checkout" and pass it the name of any
1192 revision, (with a branch name, a tag name, or any other commit
1193 identifier). For example, to examine our project before the original
1194 typo was introduced:
1196 $ git checkout 0a633bf5
1197 Note: moving to "0a633bf5" which isn't a local branch
1198 If you want to create a new branch from this checkout, you may do so
1199 (now or later) by using -b with the checkout command again. Example:
1200 git checkout -b <new_branch_name>
1201 HEAD is now at 0a633bf... Create a makefile
1203 The note that git gives us is to indicate that we are checking out a
1204 non-branch revision. This is perfectly fine if we are just exploring
1205 history, but if we actually wanted to use this revision as the basis
1206 for new commits, we would first have to create a new branch name as it
1209 If we were to use "git checkout" with a branch name, then that would
1210 change the current branch, (meaning that any new commits would advance
1211 that branch pointer).
1213 For now, let's return back to the tip of the master branch by just
1214 checking it out again:
1216 $ git checkout master
1217 Previous HEAD position was 0a633bf... Create a makefile
1218 Switched to branch "master"
1220 #### 2.8.5 Pushing changes to another repository
1222 As an unsurprising parallel to "git pull", git also provides "git
1223 push" for pushing changes to another repository. Now, generally the
1224 purpose of pushing to a repository is to have some "collaboration
1225 point" where potentially multiple people might be pushing or
1226 pulling. Because there might be multiple people pushing into the
1227 repository at any point, it wouldn't make sense to have a
1228 working-directory associated with this repository.
1230 For this, git has the notion of a "bare" repository, which is simply a
1231 repository with no working directory. Let's create a new bare
1232 repository and push some changes into it:
1237 $ git --bare init --shared
1239 The --shared option sets up the necessary group file permissions so
1240 that other users in my group will be able to push into this repository
1243 Now lets return to our hello repository and push some changes to this
1244 new repository. Since this is our very first push into this repository
1245 we need to tell git which branches to push. The easiest way to do this
1246 is to use --all to indicate all branches:
1249 $ git push ../hello-bare --all
1250 updating 'refs/heads/master'
1251 from 0000000000000000000000000000000000000000
1252 to 3c54ac672ec1130b36837f1b708054a7a1d402de
1254 Done counting 18 objects.
1255 Deltifying 18 objects...
1257 Writing 18 objects...
1259 Total 18 (delta 3), reused 15 (delta 2)
1260 Unpacking 18 objects...
1262 refs/heads/master: 0000000000000000000000000000000000000000 -> 3c54ac672ec1130b36837f1b708054a7a1d402de
1264 For subsequent pushes we don't need to specify --all as "git push" by
1265 default pushes all branches that exist in both the local and remote
1266 repositories. Also, as with pull, instead of explicitly specifying a
1267 URL, you may also specify a remote to push to. And by default, after
1268 cloning a repository, "git push" with no other arguments will attempt
1269 to push back to the same origin repository. As this is often exactly
1270 what is wanted, you may find that "git push" alone is often exactly
1273 What happens if we try to pull or push changes and the receiving
1274 repository already has those changes? Nothing too exciting.
1276 $ git push ../hello-bare
1277 Everything up-to-date
1280 Open Publication License
1282 Version 1.0, 8 June 1999
1284 ### D.1 Requirements on both unmodified and modified versions
1286 The Open Publication works may be reproduced and distributed in whole
1287 or in part, in any medium physical or electronic, provided that the
1288 terms of this license are adhered to, and that this license or an
1289 incorporation of it by reference (with any options elected by the
1290 author(s) and/or publisher) is displayed in the reproduction.
1292 Proper form for an incorporation by reference is as follows:
1294 Copyright (c) year by author’s name or designee. This material may be
1295 distributed only subject to the terms and conditions set forth in the
1296 Open Publication License, vx.y or later (the latest version is
1297 presently available at
1298 [http://www.opencontent.org/openpub/][http://www.opencontent.org/openpub/]).
1300 The reference must be immediately followed with any options elected by
1301 the author(s) and/or publisher of the document (see section D.6).
1303 Commercial redistribution of Open Publication-licensed material is
1306 Any publication in standard (paper) book form shall require the
1307 citation of the original publisher and author. The publisher and
1308 author’s names shall appear on all outer surfaces of the book. On all
1309 outer surfaces of the book the original publisher’s name shall be as
1310 large as the title of the work and cited as possessive with respect to
1315 The copyright to each Open Publication is owned by its author(s) or
1318 ### D.3 Scope of license
1320 The following license terms apply to all Open Publication works,
1321 unless otherwise explicitly stated in the document.
1323 Mere aggregation of Open Publication works or a portion of an Open
1324 Publication work with other works or programs on the same media shall
1325 not cause this license to apply to those other works. The aggregate
1326 work shall contain a notice specifying the inclusion of the Open
1327 Publication material and appropriate copyright notice.
1329 Severability. If any part of this license is found to be unenforceable
1330 in any jurisdiction, the remaining portions of the license remain in
1333 No warranty. Open Publication works are licensed and provided “as is”
1334 without warranty of any kind, express or implied, including, but not
1335 limited to, the implied warranties of merchantability and fitness for
1336 a particular purpose or a warranty of non-infringement.
1338 ### D.4 Requirements on modified works
1340 All modified versions of documents covered by this license, including
1341 translations, anthologies, compilations and partial documents, must
1342 meet the following requirements:
1344 1. The modified version must be labeled as such.
1345 2. The person making the modifications must be identified and the
1346 modifications dated.
1347 3. Acknowledgement of the original author and publisher if
1348 applicable must be retained according to normal academic citation
1350 4. The location of the original unmodified document must be identified.
1351 5. The original author’s (or authors’) name(s) may not be used to
1352 assert or imply endorsement of the resulting document without the
1353 original author’s (or authors’) permission.
1355 ### D.5 Good-practice recommendations
1357 In addition to the requirements of this license, it is requested from
1358 and strongly recommended of redistributors that:
1360 1. If you are distributing Open Publication works on hardcopy or
1361 CD-ROM, you provide email notification to the authors of your
1362 intent to redistribute at least thirty days before your
1363 manuscript or media freeze, to give the authors time to provide
1364 updated documents. This notification should describe
1365 modifications, if any, made to the document.
1366 2. All substantive modifications (including deletions) be either
1367 clearly marked up in the document or else described in an
1368 attachment to the document.
1369 3. Finally, while it is not mandatory under this license, it is
1370 considered good form to offer a free copy of any hardcopy and
1371 CD-ROM expression of an Open Publication-licensed work to its
1374 ### D.6 License options
1376 The author(s) and/or publisher of an Open Publication-licensed
1377 document may elect certain options by appending language to the
1378 reference to or copy of the license. These options are considered part
1379 of the license instance and must be included with the license (or its
1380 incorporation by reference) in derived works.
1382 1. To prohibit distribution of substantively modified versions
1383 without the explicit permission of the author(s). “Substantive
1384 modification” is defined as a change to the semantic content of
1385 the document, and excludes mere changes in format or
1386 typographical corrections.
1388 To accomplish this, add the phrase “Distribution of substantively
1389 modified versions of this document is prohibited without the
1390 explicit permission of the copyright holder.” to the license
1393 2. To prohibit any publication of this work or derivative works in
1394 whole or in part in standard (paper) book form for commercial
1395 purposes is prohibited unless prior permission is obtained from
1396 the copyright holder.
1398 To accomplish this, add the phrase “Distribution of the work or
1399 derivative of the work in any standard (paper) book form is
1400 prohibited unless prior permission is obtained from the copyright
1401 holder.” to the license reference or copy.