GIT 1.5.3.2
[git/git.git] / Documentation / user-manual.txt
CommitLineData
0eb4f7cd 1Git User's Manual (for version 1.5.3 or newer)
71f4b183 2______________________________________________
d19fbc3c 3
99eaefdd
BF
4
5Git is a fast distributed revision control system.
6
02783075 7This manual is designed to be readable by someone with basic UNIX
79c96c57 8command-line skills, but no previous knowledge of git.
d19fbc3c 9
2624d9a5
BF
10<<repositories-and-branches>> and <<exploring-git-history>> explain how
11to fetch and study a project using git--read these chapters to learn how
12to build and test a particular version of a software project, search for
13regressions, and so on.
ef89f701 14
2624d9a5
BF
15People needing to do actual development will also want to read
16<<Developing-with-git>> and <<sharing-development>>.
6bd9b682
BF
17
18Further chapters cover more specialized topics.
19
d19fbc3c
BF
20Comprehensive reference documentation is available through the man
21pages. For a command such as "git clone", just use
22
23------------------------------------------------
24$ man git-clone
25------------------------------------------------
26
2624d9a5
BF
27See also <<git-quick-start>> for a brief overview of git commands,
28without any explanation.
b181d57f 29
99f171bb 30Finally, see <<todo>> for ways that you can help make this manual more
2624d9a5 31complete.
b181d57f 32
b181d57f 33
e34caace 34[[repositories-and-branches]]
d19fbc3c
BF
35Repositories and Branches
36=========================
37
e34caace 38[[how-to-get-a-git-repository]]
d19fbc3c
BF
39How to get a git repository
40---------------------------
41
42It will be useful to have a git repository to experiment with as you
43read this manual.
44
a5f90f31
BF
45The best way to get one is by using the gitlink:git-clone[1] command to
46download a copy of an existing repository. If you don't already have a
47project in mind, here are some interesting examples:
d19fbc3c
BF
48
49------------------------------------------------
50 # git itself (approx. 10MB download):
51$ git clone git://git.kernel.org/pub/scm/git/git.git
52 # the linux kernel (approx. 150MB download):
53$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
54------------------------------------------------
55
56The initial clone may be time-consuming for a large project, but you
57will only need to clone once.
58
59The clone command creates a new directory named after the project
60("git" or "linux-2.6" in the examples above). After you cd into this
61directory, you will see that it contains a copy of the project files,
62together with a special top-level directory named ".git", which
63contains all the information about the history of the project.
64
e34caace 65[[how-to-check-out]]
d19fbc3c
BF
66How to check out a different version of a project
67-------------------------------------------------
68
a2ef9d63
BF
69Git is best thought of as a tool for storing the history of a collection
70of files. It stores the history as a compressed collection of
71interrelated snapshots of the project's contents. In git each such
72version is called a <<def_commit,commit>>.
d19fbc3c 73
81b6c950
BF
74A single git repository may contain multiple branches. It keeps track
75of them by keeping a list of <<def_head,heads>> which reference the
a2ef9d63 76latest commit on each branch; the gitlink:git-branch[1] command shows
81b6c950 77you the list of branch heads:
d19fbc3c
BF
78
79------------------------------------------------
80$ git branch
81* master
82------------------------------------------------
83
4f752407
BF
84A freshly cloned repository contains a single branch head, by default
85named "master", with the working directory initialized to the state of
86the project referred to by that branch head.
d19fbc3c 87
81b6c950
BF
88Most projects also use <<def_tag,tags>>. Tags, like heads, are
89references into the project's history, and can be listed using the
d19fbc3c
BF
90gitlink:git-tag[1] command:
91
92------------------------------------------------
93$ git tag -l
94v2.6.11
95v2.6.11-tree
96v2.6.12
97v2.6.12-rc2
98v2.6.12-rc3
99v2.6.12-rc4
100v2.6.12-rc5
101v2.6.12-rc6
102v2.6.13
103...
104------------------------------------------------
105
fe4b3e59 106Tags are expected to always point at the same version of a project,
81b6c950 107while heads are expected to advance as development progresses.
fe4b3e59 108
81b6c950 109Create a new branch head pointing to one of these versions and check it
d19fbc3c
BF
110out using gitlink:git-checkout[1]:
111
112------------------------------------------------
113$ git checkout -b new v2.6.13
114------------------------------------------------
115
116The working directory then reflects the contents that the project had
117when it was tagged v2.6.13, and gitlink:git-branch[1] shows two
118branches, with an asterisk marking the currently checked-out branch:
119
120------------------------------------------------
121$ git branch
122 master
123* new
124------------------------------------------------
125
126If you decide that you'd rather see version 2.6.17, you can modify
127the current branch to point at v2.6.17 instead, with
128
129------------------------------------------------
130$ git reset --hard v2.6.17
131------------------------------------------------
132
81b6c950 133Note that if the current branch head was your only reference to a
d19fbc3c 134particular point in history, then resetting that branch may leave you
81b6c950
BF
135with no way to find the history it used to point to; so use this command
136carefully.
d19fbc3c 137
e34caace 138[[understanding-commits]]
d19fbc3c
BF
139Understanding History: Commits
140------------------------------
141
142Every change in the history of a project is represented by a commit.
143The gitlink:git-show[1] command shows the most recent commit on the
144current branch:
145
146------------------------------------------------
147$ git show
e2618ff4
BF
148commit 17cf781661e6d38f737f15f53ab552f1e95960d7
149Author: Linus Torvalds <torvalds@ppc970.osdl.org.(none)>
150Date: Tue Apr 19 14:11:06 2005 -0700
151
152 Remove duplicate getenv(DB_ENVIRONMENT) call
153
154 Noted by Tony Luck.
155
156diff --git a/init-db.c b/init-db.c
157index 65898fa..b002dc6 100644
158--- a/init-db.c
159+++ b/init-db.c
160@@ -7,7 +7,7 @@
d19fbc3c 161
e2618ff4
BF
162 int main(int argc, char **argv)
163 {
164- char *sha1_dir = getenv(DB_ENVIRONMENT), *path;
165+ char *sha1_dir, *path;
166 int len, i;
167
168 if (mkdir(".git", 0755) < 0) {
d19fbc3c
BF
169------------------------------------------------
170
171As you can see, a commit shows who made the latest change, what they
172did, and why.
173
35121930
BF
174Every commit has a 40-hexdigit id, sometimes called the "object name" or the
175"SHA1 id", shown on the first line of the "git show" output. You can usually
176refer to a commit by a shorter name, such as a tag or a branch name, but this
177longer name can also be useful. Most importantly, it is a globally unique
178name for this commit: so if you tell somebody else the object name (for
179example in email), then you are guaranteed that name will refer to the same
180commit in their repository that it does in yours (assuming their repository
181has that commit at all). Since the object name is computed as a hash over the
182contents of the commit, you are guaranteed that the commit can never change
183without its name also changing.
184
036f8199 185In fact, in <<git-concepts>> we shall see that everything stored in git
35121930
BF
186history, including file data and directory contents, is stored in an object
187with a name that is a hash of its contents.
d19fbc3c 188
e34caace 189[[understanding-reachability]]
d19fbc3c
BF
190Understanding history: commits, parents, and reachability
191~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192
193Every commit (except the very first commit in a project) also has a
194parent commit which shows what happened before this commit.
195Following the chain of parents will eventually take you back to the
196beginning of the project.
197
198However, the commits do not form a simple list; git allows lines of
199development to diverge and then reconverge, and the point where two
200lines of development reconverge is called a "merge". The commit
201representing a merge can therefore have more than one parent, with
202each parent representing the most recent commit on one of the lines
203of development leading to that point.
204
205The best way to see how this works is using the gitlink:gitk[1]
206command; running gitk now on a git repository and looking for merge
207commits will help understand how the git organizes history.
208
209In the following, we say that commit X is "reachable" from commit Y
210if commit X is an ancestor of commit Y. Equivalently, you could say
02783075 211that Y is a descendant of X, or that there is a chain of parents
d19fbc3c
BF
212leading from commit Y to commit X.
213
e34caace 214[[history-diagrams]]
3dff5379
PR
215Understanding history: History diagrams
216~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c
BF
217
218We will sometimes represent git history using diagrams like the one
219below. Commits are shown as "o", and the links between them with
220lines drawn with - / and \. Time goes left to right:
221
1dc71a91
BF
222
223................................................
d19fbc3c
BF
224 o--o--o <-- Branch A
225 /
226 o--o--o <-- master
227 \
228 o--o--o <-- Branch B
1dc71a91 229................................................
d19fbc3c
BF
230
231If we need to talk about a particular commit, the character "o" may
232be replaced with another letter or number.
233
e34caace 234[[what-is-a-branch]]
d19fbc3c
BF
235Understanding history: What is a branch?
236~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237
81b6c950
BF
238When we need to be precise, we will use the word "branch" to mean a line
239of development, and "branch head" (or just "head") to mean a reference
240to the most recent commit on a branch. In the example above, the branch
241head named "A" is a pointer to one particular commit, but we refer to
242the line of three commits leading up to that point as all being part of
d19fbc3c
BF
243"branch A".
244
81b6c950
BF
245However, when no confusion will result, we often just use the term
246"branch" both for branches and for branch heads.
d19fbc3c 247
e34caace 248[[manipulating-branches]]
d19fbc3c
BF
249Manipulating branches
250---------------------
251
252Creating, deleting, and modifying branches is quick and easy; here's
253a summary of the commands:
254
255git branch::
256 list all branches
257git branch <branch>::
258 create a new branch named <branch>, referencing the same
259 point in history as the current branch
260git branch <branch> <start-point>::
261 create a new branch named <branch>, referencing
262 <start-point>, which may be specified any way you like,
263 including using a branch name or a tag name
264git branch -d <branch>::
265 delete the branch <branch>; if the branch you are deleting
c64415e2
BF
266 points to a commit which is not reachable from the current
267 branch, this command will fail with a warning.
d19fbc3c
BF
268git branch -D <branch>::
269 even if the branch points to a commit not reachable
270 from the current branch, you may know that that commit
271 is still reachable from some other branch or tag. In that
272 case it is safe to use this command to force git to delete
273 the branch.
274git checkout <branch>::
275 make the current branch <branch>, updating the working
276 directory to reflect the version referenced by <branch>
277git checkout -b <new> <start-point>::
278 create a new branch <new> referencing <start-point>, and
279 check it out.
280
72a76c95
BF
281The special symbol "HEAD" can always be used to refer to the current
282branch. In fact, git uses a file named "HEAD" in the .git directory to
283remember which branch is current:
284
285------------------------------------------------
286$ cat .git/HEAD
287ref: refs/heads/master
288------------------------------------------------
289
25d9f3fa 290[[detached-head]]
72a76c95
BF
291Examining an old version without creating a new branch
292------------------------------------------------------
293
294The git-checkout command normally expects a branch head, but will also
295accept an arbitrary commit; for example, you can check out the commit
296referenced by a tag:
297
298------------------------------------------------
299$ git checkout v2.6.17
300Note: moving to "v2.6.17" which isn't a local branch
301If you want to create a new branch from this checkout, you may do so
302(now or later) by using -b with the checkout command again. Example:
303 git checkout -b <new_branch_name>
304HEAD is now at 427abfa... Linux v2.6.17
305------------------------------------------------
306
307The HEAD then refers to the SHA1 of the commit instead of to a branch,
308and git branch shows that you are no longer on a branch:
309
310------------------------------------------------
311$ cat .git/HEAD
312427abfa28afedffadfca9dd8b067eb6d36bac53f
953f3d6f 313$ git branch
72a76c95
BF
314* (no branch)
315 master
316------------------------------------------------
317
318In this case we say that the HEAD is "detached".
319
953f3d6f
BF
320This is an easy way to check out a particular version without having to
321make up a name for the new branch. You can still create a new branch
322(or tag) for this version later if you decide to.
d19fbc3c 323
e34caace 324[[examining-remote-branches]]
d19fbc3c
BF
325Examining branches from a remote repository
326-------------------------------------------
327
328The "master" branch that was created at the time you cloned is a copy
329of the HEAD in the repository that you cloned from. That repository
330may also have had other branches, though, and your local repository
331keeps branches which track each of those remote branches, which you
332can view using the "-r" option to gitlink:git-branch[1]:
333
334------------------------------------------------
335$ git branch -r
336 origin/HEAD
337 origin/html
338 origin/maint
339 origin/man
340 origin/master
341 origin/next
342 origin/pu
343 origin/todo
344------------------------------------------------
345
346You cannot check out these remote-tracking branches, but you can
347examine them on a branch of your own, just as you would a tag:
348
349------------------------------------------------
350$ git checkout -b my-todo-copy origin/todo
351------------------------------------------------
352
353Note that the name "origin" is just the name that git uses by default
354to refer to the repository that you cloned from.
355
356[[how-git-stores-references]]
f60b9642
BF
357Naming branches, tags, and other references
358-------------------------------------------
d19fbc3c
BF
359
360Branches, remote-tracking branches, and tags are all references to
f60b9642
BF
361commits. All references are named with a slash-separated path name
362starting with "refs"; the names we've been using so far are actually
363shorthand:
d19fbc3c 364
f60b9642
BF
365 - The branch "test" is short for "refs/heads/test".
366 - The tag "v2.6.18" is short for "refs/tags/v2.6.18".
367 - "origin/master" is short for "refs/remotes/origin/master".
d19fbc3c 368
f60b9642
BF
369The full name is occasionally useful if, for example, there ever
370exists a tag and a branch with the same name.
d19fbc3c 371
c64415e2
BF
372As another useful shortcut, the "HEAD" of a repository can be referred
373to just using the name of that repository. So, for example, "origin"
374is usually a shortcut for the HEAD branch in the repository "origin".
d19fbc3c
BF
375
376For the complete list of paths which git checks for references, and
f60b9642
BF
377the order it uses to decide which to choose when there are multiple
378references with the same shorthand name, see the "SPECIFYING
379REVISIONS" section of gitlink:git-rev-parse[1].
d19fbc3c
BF
380
381[[Updating-a-repository-with-git-fetch]]
382Updating a repository with git fetch
383------------------------------------
384
385Eventually the developer cloned from will do additional work in her
386repository, creating new commits and advancing the branches to point
387at the new commits.
388
389The command "git fetch", with no arguments, will update all of the
390remote-tracking branches to the latest version found in her
391repository. It will not touch any of your own branches--not even the
392"master" branch that was created for you on clone.
393
e34caace 394[[fetching-branches]]
d5cd5de4
BF
395Fetching branches from other repositories
396-----------------------------------------
397
398You can also track branches from repositories other than the one you
399cloned from, using gitlink:git-remote[1]:
400
401-------------------------------------------------
402$ git remote add linux-nfs git://linux-nfs.org/pub/nfs-2.6.git
04483524 403$ git fetch linux-nfs
d5cd5de4
BF
404* refs/remotes/linux-nfs/master: storing branch 'master' ...
405 commit: bf81b46
406-------------------------------------------------
407
408New remote-tracking branches will be stored under the shorthand name
409that you gave "git remote add", in this case linux-nfs:
410
411-------------------------------------------------
412$ git branch -r
413linux-nfs/master
414origin/master
415-------------------------------------------------
416
417If you run "git fetch <remote>" later, the tracking branches for the
418named <remote> will be updated.
419
420If you examine the file .git/config, you will see that git has added
421a new stanza:
422
423-------------------------------------------------
424$ cat .git/config
425...
426[remote "linux-nfs"]
923642fe
BF
427 url = git://linux-nfs.org/pub/nfs-2.6.git
428 fetch = +refs/heads/*:refs/remotes/linux-nfs/*
d5cd5de4
BF
429...
430-------------------------------------------------
431
fc90c536
BF
432This is what causes git to track the remote's branches; you may modify
433or delete these configuration options by editing .git/config with a
434text editor. (See the "CONFIGURATION FILE" section of
435gitlink:git-config[1] for details.)
d5cd5de4 436
e34caace 437[[exploring-git-history]]
d19fbc3c
BF
438Exploring git history
439=====================
440
441Git is best thought of as a tool for storing the history of a
442collection of files. It does this by storing compressed snapshots of
1130845b 443the contents of a file hierarchy, together with "commits" which show
d19fbc3c
BF
444the relationships between these snapshots.
445
446Git provides extremely flexible and fast tools for exploring the
447history of a project.
448
aacd404e 449We start with one specialized tool that is useful for finding the
d19fbc3c
BF
450commit that introduced a bug into a project.
451
e34caace 452[[using-bisect]]
d19fbc3c
BF
453How to use bisect to find a regression
454--------------------------------------
455
456Suppose version 2.6.18 of your project worked, but the version at
457"master" crashes. Sometimes the best way to find the cause of such a
458regression is to perform a brute-force search through the project's
459history to find the particular commit that caused the problem. The
460gitlink:git-bisect[1] command can help you do this:
461
462-------------------------------------------------
463$ git bisect start
464$ git bisect good v2.6.18
465$ git bisect bad master
466Bisecting: 3537 revisions left to test after this
467[65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6]
468-------------------------------------------------
469
470If you run "git branch" at this point, you'll see that git has
471temporarily moved you to a new branch named "bisect". This branch
472points to a commit (with commit id 65934...) that is reachable from
473v2.6.19 but not from v2.6.18. Compile and test it, and see whether
474it crashes. Assume it does crash. Then:
475
476-------------------------------------------------
477$ git bisect bad
478Bisecting: 1769 revisions left to test after this
479[7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings
480-------------------------------------------------
481
482checks out an older version. Continue like this, telling git at each
483stage whether the version it gives you is good or bad, and notice
484that the number of revisions left to test is cut approximately in
485half each time.
486
487After about 13 tests (in this case), it will output the commit id of
488the guilty commit. You can then examine the commit with
489gitlink:git-show[1], find out who wrote it, and mail them your bug
490report with the commit id. Finally, run
491
492-------------------------------------------------
493$ git bisect reset
494-------------------------------------------------
495
496to return you to the branch you were on before and delete the
497temporary "bisect" branch.
498
499Note that the version which git-bisect checks out for you at each
500point is just a suggestion, and you're free to try a different
501version if you think it would be a good idea. For example,
502occasionally you may land on a commit that broke something unrelated;
503run
504
505-------------------------------------------------
04483524 506$ git bisect visualize
d19fbc3c
BF
507-------------------------------------------------
508
509which will run gitk and label the commit it chose with a marker that
510says "bisect". Chose a safe-looking commit nearby, note its commit
511id, and check it out with:
512
513-------------------------------------------------
514$ git reset --hard fb47ddb2db...
515-------------------------------------------------
516
517then test, run "bisect good" or "bisect bad" as appropriate, and
518continue.
519
e34caace 520[[naming-commits]]
d19fbc3c
BF
521Naming commits
522--------------
523
524We have seen several ways of naming commits already:
525
d55ae921 526 - 40-hexdigit object name
d19fbc3c
BF
527 - branch name: refers to the commit at the head of the given
528 branch
529 - tag name: refers to the commit pointed to by the given tag
530 (we've seen branches and tags are special cases of
531 <<how-git-stores-references,references>>).
532 - HEAD: refers to the head of the current branch
533
eb6ae7f4 534There are many more; see the "SPECIFYING REVISIONS" section of the
aec053bb 535gitlink:git-rev-parse[1] man page for the complete list of ways to
d19fbc3c
BF
536name revisions. Some examples:
537
538-------------------------------------------------
d55ae921 539$ git show fb47ddb2 # the first few characters of the object name
d19fbc3c
BF
540 # are usually enough to specify it uniquely
541$ git show HEAD^ # the parent of the HEAD commit
542$ git show HEAD^^ # the grandparent
543$ git show HEAD~4 # the great-great-grandparent
544-------------------------------------------------
545
546Recall that merge commits may have more than one parent; by default,
547^ and ~ follow the first parent listed in the commit, but you can
548also choose:
549
550-------------------------------------------------
551$ git show HEAD^1 # show the first parent of HEAD
552$ git show HEAD^2 # show the second parent of HEAD
553-------------------------------------------------
554
555In addition to HEAD, there are several other special names for
556commits:
557
558Merges (to be discussed later), as well as operations such as
559git-reset, which change the currently checked-out commit, generally
560set ORIG_HEAD to the value HEAD had before the current operation.
561
562The git-fetch operation always stores the head of the last fetched
563branch in FETCH_HEAD. For example, if you run git fetch without
564specifying a local branch as the target of the operation
565
566-------------------------------------------------
567$ git fetch git://example.com/proj.git theirbranch
568-------------------------------------------------
569
570the fetched commits will still be available from FETCH_HEAD.
571
572When we discuss merges we'll also see the special name MERGE_HEAD,
573which refers to the other branch that we're merging in to the current
574branch.
575
aec053bb 576The gitlink:git-rev-parse[1] command is a low-level command that is
d55ae921
BF
577occasionally useful for translating some name for a commit to the object
578name for that commit:
aec053bb
BF
579
580-------------------------------------------------
581$ git rev-parse origin
582e05db0fd4f31dde7005f075a84f96b360d05984b
583-------------------------------------------------
584
e34caace 585[[creating-tags]]
d19fbc3c
BF
586Creating tags
587-------------
588
589We can also create a tag to refer to a particular commit; after
590running
591
592-------------------------------------------------
04483524 593$ git tag stable-1 1b2e1d63ff
d19fbc3c
BF
594-------------------------------------------------
595
596You can use stable-1 to refer to the commit 1b2e1d63ff.
597
c64415e2
BF
598This creates a "lightweight" tag. If you would also like to include a
599comment with the tag, and possibly sign it cryptographically, then you
600should create a tag object instead; see the gitlink:git-tag[1] man page
601for details.
d19fbc3c 602
e34caace 603[[browsing-revisions]]
d19fbc3c
BF
604Browsing revisions
605------------------
606
607The gitlink:git-log[1] command can show lists of commits. On its
608own, it shows all commits reachable from the parent commit; but you
609can also make more specific requests:
610
611-------------------------------------------------
612$ git log v2.5.. # commits since (not reachable from) v2.5
613$ git log test..master # commits reachable from master but not test
614$ git log master..test # ...reachable from test but not master
615$ git log master...test # ...reachable from either test or master,
616 # but not both
617$ git log --since="2 weeks ago" # commits from the last 2 weeks
618$ git log Makefile # commits which modify Makefile
619$ git log fs/ # ... which modify any file under fs/
620$ git log -S'foo()' # commits which add or remove any file data
621 # matching the string 'foo()'
622-------------------------------------------------
623
624And of course you can combine all of these; the following finds
625commits since v2.5 which touch the Makefile or any file under fs:
626
627-------------------------------------------------
628$ git log v2.5.. Makefile fs/
629-------------------------------------------------
630
631You can also ask git log to show patches:
632
633-------------------------------------------------
634$ git log -p
635-------------------------------------------------
636
637See the "--pretty" option in the gitlink:git-log[1] man page for more
638display options.
639
640Note that git log starts with the most recent commit and works
641backwards through the parents; however, since git history can contain
3dff5379 642multiple independent lines of development, the particular order that
d19fbc3c
BF
643commits are listed in may be somewhat arbitrary.
644
e34caace 645[[generating-diffs]]
d19fbc3c
BF
646Generating diffs
647----------------
648
649You can generate diffs between any two versions using
650gitlink:git-diff[1]:
651
652-------------------------------------------------
653$ git diff master..test
654-------------------------------------------------
655
656Sometimes what you want instead is a set of patches:
657
658-------------------------------------------------
659$ git format-patch master..test
660-------------------------------------------------
661
662will generate a file with a patch for each commit reachable from test
663but not from master. Note that if master also has commits which are
664not reachable from test, then the combined result of these patches
665will not be the same as the diff produced by the git-diff example.
666
e34caace 667[[viewing-old-file-versions]]
d19fbc3c
BF
668Viewing old file versions
669-------------------------
670
671You can always view an old version of a file by just checking out the
672correct revision first. But sometimes it is more convenient to be
673able to view an old version of a single file without checking
674anything out; this command does that:
675
676-------------------------------------------------
677$ git show v2.5:fs/locks.c
678-------------------------------------------------
679
680Before the colon may be anything that names a commit, and after it
681may be any path to a file tracked by git.
682
e34caace 683[[history-examples]]
aec053bb
BF
684Examples
685--------
686
46acd3fa
BF
687[[counting-commits-on-a-branch]]
688Counting the number of commits on a branch
689~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
690
691Suppose you want to know how many commits you've made on "mybranch"
692since it diverged from "origin":
693
694-------------------------------------------------
695$ git log --pretty=oneline origin..mybranch | wc -l
696-------------------------------------------------
697
698Alternatively, you may often see this sort of thing done with the
699lower-level command gitlink:git-rev-list[1], which just lists the SHA1's
700of all the given commits:
701
702-------------------------------------------------
703$ git rev-list origin..mybranch | wc -l
704-------------------------------------------------
705
e34caace 706[[checking-for-equal-branches]]
aec053bb 707Check whether two branches point at the same history
2f99710c 708~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aec053bb
BF
709
710Suppose you want to check whether two branches point at the same point
711in history.
712
713-------------------------------------------------
714$ git diff origin..master
715-------------------------------------------------
716
69f7ad73
BF
717will tell you whether the contents of the project are the same at the
718two branches; in theory, however, it's possible that the same project
719contents could have been arrived at by two different historical
d55ae921 720routes. You could compare the object names:
aec053bb
BF
721
722-------------------------------------------------
723$ git rev-list origin
724e05db0fd4f31dde7005f075a84f96b360d05984b
725$ git rev-list master
726e05db0fd4f31dde7005f075a84f96b360d05984b
727-------------------------------------------------
728
69f7ad73
BF
729Or you could recall that the ... operator selects all commits
730contained reachable from either one reference or the other but not
731both: so
aec053bb
BF
732
733-------------------------------------------------
734$ git log origin...master
735-------------------------------------------------
736
737will return no commits when the two branches are equal.
738
e34caace 739[[finding-tagged-descendants]]
b181d57f
BF
740Find first tagged version including a given fix
741~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aec053bb 742
69f7ad73
BF
743Suppose you know that the commit e05db0fd fixed a certain problem.
744You'd like to find the earliest tagged release that contains that
745fix.
746
747Of course, there may be more than one answer--if the history branched
748after commit e05db0fd, then there could be multiple "earliest" tagged
749releases.
750
751You could just visually inspect the commits since e05db0fd:
752
753-------------------------------------------------
754$ gitk e05db0fd..
755-------------------------------------------------
756
b181d57f
BF
757Or you can use gitlink:git-name-rev[1], which will give the commit a
758name based on any tag it finds pointing to one of the commit's
759descendants:
760
761-------------------------------------------------
04483524 762$ git name-rev --tags e05db0fd
b181d57f
BF
763e05db0fd tags/v1.5.0-rc1^0~23
764-------------------------------------------------
765
766The gitlink:git-describe[1] command does the opposite, naming the
767revision using a tag on which the given commit is based:
768
769-------------------------------------------------
770$ git describe e05db0fd
04483524 771v1.5.0-rc0-260-ge05db0f
b181d57f
BF
772-------------------------------------------------
773
774but that may sometimes help you guess which tags might come after the
775given commit.
776
777If you just want to verify whether a given tagged version contains a
778given commit, you could use gitlink:git-merge-base[1]:
779
780-------------------------------------------------
781$ git merge-base e05db0fd v1.5.0-rc1
782e05db0fd4f31dde7005f075a84f96b360d05984b
783-------------------------------------------------
784
785The merge-base command finds a common ancestor of the given commits,
786and always returns one or the other in the case where one is a
787descendant of the other; so the above output shows that e05db0fd
788actually is an ancestor of v1.5.0-rc1.
789
790Alternatively, note that
791
792-------------------------------------------------
4a7979ca 793$ git log v1.5.0-rc1..e05db0fd
b181d57f
BF
794-------------------------------------------------
795
4a7979ca 796will produce empty output if and only if v1.5.0-rc1 includes e05db0fd,
b181d57f 797because it outputs only commits that are not reachable from v1.5.0-rc1.
aec053bb 798
4a7979ca
BF
799As yet another alternative, the gitlink:git-show-branch[1] command lists
800the commits reachable from its arguments with a display on the left-hand
801side that indicates which arguments that commit is reachable from. So,
802you can run something like
803
804-------------------------------------------------
805$ git show-branch e05db0fd v1.5.0-rc0 v1.5.0-rc1 v1.5.0-rc2
806! [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
807available
808 ! [v1.5.0-rc0] GIT v1.5.0 preview
809 ! [v1.5.0-rc1] GIT v1.5.0-rc1
810 ! [v1.5.0-rc2] GIT v1.5.0-rc2
811...
812-------------------------------------------------
813
814then search for a line that looks like
815
816-------------------------------------------------
817+ ++ [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
818available
819-------------------------------------------------
820
821Which shows that e05db0fd is reachable from itself, from v1.5.0-rc1, and
822from v1.5.0-rc2, but not from v1.5.0-rc0.
823
629d9f78
BF
824[[showing-commits-unique-to-a-branch]]
825Showing commits unique to a given branch
826~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4a7979ca 827
629d9f78
BF
828Suppose you would like to see all the commits reachable from the branch
829head named "master" but not from any other head in your repository.
d19fbc3c 830
629d9f78
BF
831We can list all the heads in this repository with
832gitlink:git-show-ref[1]:
d19fbc3c 833
629d9f78
BF
834-------------------------------------------------
835$ git show-ref --heads
836bf62196b5e363d73353a9dcf094c59595f3153b7 refs/heads/core-tutorial
837db768d5504c1bb46f63ee9d6e1772bd047e05bf9 refs/heads/maint
838a07157ac624b2524a059a3414e99f6f44bebc1e7 refs/heads/master
83924dbc180ea14dc1aebe09f14c8ecf32010690627 refs/heads/tutorial-2
8401e87486ae06626c2f31eaa63d26fc0fd646c8af2 refs/heads/tutorial-fixes
841-------------------------------------------------
d19fbc3c 842
629d9f78
BF
843We can get just the branch-head names, and remove "master", with
844the help of the standard utilities cut and grep:
845
846-------------------------------------------------
847$ git show-ref --heads | cut -d' ' -f2 | grep -v '^refs/heads/master'
848refs/heads/core-tutorial
849refs/heads/maint
850refs/heads/tutorial-2
851refs/heads/tutorial-fixes
852-------------------------------------------------
853
854And then we can ask to see all the commits reachable from master
855but not from these other heads:
856
857-------------------------------------------------
858$ gitk master --not $( git show-ref --heads | cut -d' ' -f2 |
859 grep -v '^refs/heads/master' )
860-------------------------------------------------
861
862Obviously, endless variations are possible; for example, to see all
863commits reachable from some head but not from any tag in the repository:
864
865-------------------------------------------------
c78974f7 866$ gitk $( git show-ref --heads ) --not $( git show-ref --tags )
629d9f78
BF
867-------------------------------------------------
868
869(See gitlink:git-rev-parse[1] for explanations of commit-selecting
870syntax such as `--not`.)
871
82c8bf28
BF
872[[making-a-release]]
873Creating a changelog and tarball for a software release
874~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
875
876The gitlink:git-archive[1] command can create a tar or zip archive from
877any version of a project; for example:
878
879-------------------------------------------------
880$ git archive --format=tar --prefix=project/ HEAD | gzip >latest.tar.gz
881-------------------------------------------------
882
883will use HEAD to produce a tar archive in which each filename is
ccd71866 884preceded by "project/".
82c8bf28
BF
885
886If you're releasing a new version of a software project, you may want
887to simultaneously make a changelog to include in the release
888announcement.
889
890Linus Torvalds, for example, makes new kernel releases by tagging them,
891then running:
892
893-------------------------------------------------
894$ release-script 2.6.12 2.6.13-rc6 2.6.13-rc7
895-------------------------------------------------
896
897where release-script is a shell script that looks like:
898
899-------------------------------------------------
900#!/bin/sh
901stable="$1"
902last="$2"
903new="$3"
904echo "# git tag v$new"
905echo "git archive --prefix=linux-$new/ v$new | gzip -9 > ../linux-$new.tar.gz"
906echo "git diff v$stable v$new | gzip -9 > ../patch-$new.gz"
907echo "git log --no-merges v$new ^v$last > ../ChangeLog-$new"
908echo "git shortlog --no-merges v$new ^v$last > ../ShortLog"
909echo "git diff --stat --summary -M v$last v$new > ../diffstat-$new"
910-------------------------------------------------
911
912and then he just cut-and-pastes the output commands after verifying that
913they look OK.
4a7979ca 914
8ceca74a 915[[Finding-comments-with-given-content]]
187b0d80 916Finding commits referencing a file with given content
d5821de2 917~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187b0d80
BF
918
919Somebody hands you a copy of a file, and asks which commits modified a
920file such that it contained the given content either before or after the
921commit. You can find out with this:
922
923-------------------------------------------------
170c0438 924$ git log --raw --abbrev=40 --pretty=oneline -- filename |
187b0d80
BF
925 grep -B 1 `git hash-object filename`
926-------------------------------------------------
927
928Figuring out why this works is left as an exercise to the (advanced)
929student. The gitlink:git-log[1], gitlink:git-diff-tree[1], and
930gitlink:git-hash-object[1] man pages may prove helpful.
931
e34caace 932[[Developing-with-git]]
d19fbc3c
BF
933Developing with git
934===================
935
e34caace 936[[telling-git-your-name]]
d19fbc3c
BF
937Telling git your name
938---------------------
939
940Before creating any commits, you should introduce yourself to git. The
58c19d1f
BF
941easiest way to do so is to make sure the following lines appear in a
942file named .gitconfig in your home directory:
d19fbc3c
BF
943
944------------------------------------------------
d19fbc3c
BF
945[user]
946 name = Your Name Comes Here
947 email = you@yourdomain.example.com
d19fbc3c
BF
948------------------------------------------------
949
fc90c536
BF
950(See the "CONFIGURATION FILE" section of gitlink:git-config[1] for
951details on the configuration file.)
952
d19fbc3c 953
e34caace 954[[creating-a-new-repository]]
d19fbc3c
BF
955Creating a new repository
956-------------------------
957
958Creating a new repository from scratch is very easy:
959
960-------------------------------------------------
961$ mkdir project
962$ cd project
f1d2b477 963$ git init
d19fbc3c
BF
964-------------------------------------------------
965
966If you have some initial content (say, a tarball):
967
968-------------------------------------------------
969$ tar -xzvf project.tar.gz
970$ cd project
f1d2b477 971$ git init
d19fbc3c
BF
972$ git add . # include everything below ./ in the first commit:
973$ git commit
974-------------------------------------------------
975
976[[how-to-make-a-commit]]
ae25c67a 977How to make a commit
d19fbc3c
BF
978--------------------
979
980Creating a new commit takes three steps:
981
982 1. Making some changes to the working directory using your
983 favorite editor.
984 2. Telling git about your changes.
985 3. Creating the commit using the content you told git about
986 in step 2.
987
988In practice, you can interleave and repeat steps 1 and 2 as many
989times as you want: in order to keep track of what you want committed
990at step 3, git maintains a snapshot of the tree's contents in a
991special staging area called "the index."
992
01997b4a
BF
993At the beginning, the content of the index will be identical to
994that of the HEAD. The command "git diff --cached", which shows
995the difference between the HEAD and the index, should therefore
996produce no output at that point.
eb6ae7f4 997
d19fbc3c
BF
998Modifying the index is easy:
999
1000To update the index with the new contents of a modified file, use
1001
1002-------------------------------------------------
1003$ git add path/to/file
1004-------------------------------------------------
1005
1006To add the contents of a new file to the index, use
1007
1008-------------------------------------------------
1009$ git add path/to/file
1010-------------------------------------------------
1011
eb6ae7f4 1012To remove a file from the index and from the working tree,
d19fbc3c
BF
1013
1014-------------------------------------------------
1015$ git rm path/to/file
1016-------------------------------------------------
1017
1018After each step you can verify that
1019
1020-------------------------------------------------
1021$ git diff --cached
1022-------------------------------------------------
1023
1024always shows the difference between the HEAD and the index file--this
1025is what you'd commit if you created the commit now--and that
1026
1027-------------------------------------------------
1028$ git diff
1029-------------------------------------------------
1030
1031shows the difference between the working tree and the index file.
1032
1033Note that "git add" always adds just the current contents of a file
1034to the index; further changes to the same file will be ignored unless
1035you run git-add on the file again.
1036
1037When you're ready, just run
1038
1039-------------------------------------------------
1040$ git commit
1041-------------------------------------------------
1042
1043and git will prompt you for a commit message and then create the new
3dff5379 1044commit. Check to make sure it looks like what you expected with
d19fbc3c
BF
1045
1046-------------------------------------------------
1047$ git show
1048-------------------------------------------------
1049
1050As a special shortcut,
a6080a0a 1051
d19fbc3c
BF
1052-------------------------------------------------
1053$ git commit -a
1054-------------------------------------------------
1055
1056will update the index with any files that you've modified or removed
1057and create a commit, all in one step.
1058
1059A number of commands are useful for keeping track of what you're
1060about to commit:
1061
1062-------------------------------------------------
1063$ git diff --cached # difference between HEAD and the index; what
1130845b 1064 # would be committed if you ran "commit" now.
d19fbc3c
BF
1065$ git diff # difference between the index file and your
1066 # working directory; changes that would not
1067 # be included if you ran "commit" now.
c64415e2
BF
1068$ git diff HEAD # difference between HEAD and working tree; what
1069 # would be committed if you ran "commit -a" now.
d19fbc3c
BF
1070$ git status # a brief per-file summary of the above.
1071-------------------------------------------------
1072
407c0c87
BF
1073You can also use gitlink:git-gui[1] to create commits, view changes in
1074the index and the working tree files, and individually select diff hunks
1075for inclusion in the index (by right-clicking on the diff hunk and
1076choosing "Stage Hunk For Commit").
1077
e34caace 1078[[creating-good-commit-messages]]
ae25c67a 1079Creating good commit messages
d19fbc3c
BF
1080-----------------------------
1081
1082Though not required, it's a good idea to begin the commit message
1083with a single short (less than 50 character) line summarizing the
1084change, followed by a blank line and then a more thorough
1085description. Tools that turn commits into email, for example, use
1086the first line on the Subject line and the rest of the commit in the
1087body.
1088
2dc53617
JH
1089[[ignoring-files]]
1090Ignoring files
1091--------------
1092
1093A project will often generate files that you do 'not' want to track with git.
1094This typically includes files generated by a build process or temporary
1095backup files made by your editor. Of course, 'not' tracking files with git
1096is just a matter of 'not' calling "`git add`" on them. But it quickly becomes
1097annoying to have these untracked files lying around; e.g. they make
1098"`git add .`" and "`git commit -a`" practically useless, and they keep
464a8a7a 1099showing up in the output of "`git status`".
2dc53617 1100
464a8a7a
BF
1101You can tell git to ignore certain files by creating a file called .gitignore
1102in the top level of your working directory, with contents such as:
2dc53617
JH
1103
1104-------------------------------------------------
1105# Lines starting with '#' are considered comments.
464a8a7a 1106# Ignore any file named foo.txt.
2dc53617
JH
1107foo.txt
1108# Ignore (generated) html files,
1109*.html
1110# except foo.html which is maintained by hand.
1111!foo.html
1112# Ignore objects and archives.
1113*.[oa]
1114-------------------------------------------------
1115
464a8a7a
BF
1116See gitlink:gitignore[5] for a detailed explanation of the syntax. You can
1117also place .gitignore files in other directories in your working tree, and they
1118will apply to those directories and their subdirectories. The `.gitignore`
1119files can be added to your repository like any other files (just run `git add
1120.gitignore` and `git commit`, as usual), which is convenient when the exclude
1121patterns (such as patterns matching build output files) would also make sense
1122for other users who clone your repository.
1123
1124If you wish the exclude patterns to affect only certain repositories
1125(instead of every repository for a given project), you may instead put
1126them in a file in your repository named .git/info/exclude, or in any file
1127specified by the `core.excludesfile` configuration variable. Some git
1128commands can also take exclude patterns directly on the command line.
1129See gitlink:gitignore[5] for the details.
2dc53617 1130
e34caace 1131[[how-to-merge]]
ae25c67a 1132How to merge
d19fbc3c
BF
1133------------
1134
1135You can rejoin two diverging branches of development using
1136gitlink:git-merge[1]:
1137
1138-------------------------------------------------
1139$ git merge branchname
1140-------------------------------------------------
1141
1142merges the development in the branch "branchname" into the current
1143branch. If there are conflicts--for example, if the same file is
1144modified in two different ways in the remote branch and the local
1145branch--then you are warned; the output may look something like this:
1146
1147-------------------------------------------------
fabbd8f6
BF
1148$ git merge next
1149 100% (4/4) done
1150Auto-merged file.txt
d19fbc3c
BF
1151CONFLICT (content): Merge conflict in file.txt
1152Automatic merge failed; fix conflicts and then commit the result.
1153-------------------------------------------------
1154
1155Conflict markers are left in the problematic files, and after
1156you resolve the conflicts manually, you can update the index
1157with the contents and run git commit, as you normally would when
1158creating a new file.
1159
1160If you examine the resulting commit using gitk, you will see that it
1161has two parents, one pointing to the top of the current branch, and
1162one to the top of the other branch.
1163
d19fbc3c
BF
1164[[resolving-a-merge]]
1165Resolving a merge
1166-----------------
1167
1168When a merge isn't resolved automatically, git leaves the index and
1169the working tree in a special state that gives you all the
1170information you need to help resolve the merge.
1171
1172Files with conflicts are marked specially in the index, so until you
ef561ac7
BF
1173resolve the problem and update the index, gitlink:git-commit[1] will
1174fail:
d19fbc3c
BF
1175
1176-------------------------------------------------
1177$ git commit
1178file.txt: needs merge
1179-------------------------------------------------
1180
ef561ac7
BF
1181Also, gitlink:git-status[1] will list those files as "unmerged", and the
1182files with conflicts will have conflict markers added, like this:
1183
1184-------------------------------------------------
1185<<<<<<< HEAD:file.txt
1186Hello world
1187=======
1188Goodbye
1189>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1190-------------------------------------------------
1191
1192All you need to do is edit the files to resolve the conflicts, and then
1193
1194-------------------------------------------------
1195$ git add file.txt
1196$ git commit
1197-------------------------------------------------
1198
1199Note that the commit message will already be filled in for you with
1200some information about the merge. Normally you can just use this
1201default message unchanged, but you may add additional commentary of
1202your own if desired.
1203
1204The above is all you need to know to resolve a simple merge. But git
1205also provides more information to help resolve conflicts:
1206
e34caace 1207[[conflict-resolution]]
ef561ac7
BF
1208Getting conflict-resolution help during a merge
1209~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c
BF
1210
1211All of the changes that git was able to merge automatically are
1212already added to the index file, so gitlink:git-diff[1] shows only
ef561ac7 1213the conflicts. It uses an unusual syntax:
d19fbc3c
BF
1214
1215-------------------------------------------------
1216$ git diff
1217diff --cc file.txt
1218index 802992c,2b60207..0000000
1219--- a/file.txt
1220+++ b/file.txt
1221@@@ -1,1 -1,1 +1,5 @@@
1222++<<<<<<< HEAD:file.txt
1223 +Hello world
1224++=======
1225+ Goodbye
1226++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1227-------------------------------------------------
1228
1130845b 1229Recall that the commit which will be committed after we resolve this
d19fbc3c
BF
1230conflict will have two parents instead of the usual one: one parent
1231will be HEAD, the tip of the current branch; the other will be the
1232tip of the other branch, which is stored temporarily in MERGE_HEAD.
1233
ef561ac7
BF
1234During the merge, the index holds three versions of each file. Each of
1235these three "file stages" represents a different version of the file:
1236
1237-------------------------------------------------
1238$ git show :1:file.txt # the file in a common ancestor of both branches
1239$ git show :2:file.txt # the version from HEAD, but including any
1240 # nonconflicting changes from MERGE_HEAD
1241$ git show :3:file.txt # the version from MERGE_HEAD, but including any
1242 # nonconflicting changes from HEAD.
1243-------------------------------------------------
1244
1245Since the stage 2 and stage 3 versions have already been updated with
1246nonconflicting changes, the only remaining differences between them are
1247the important ones; thus gitlink:git-diff[1] can use the information in
1248the index to show only those conflicts.
1249
1250The diff above shows the differences between the working-tree version of
1251file.txt and the stage 2 and stage 3 versions. So instead of preceding
1252each line by a single "+" or "-", it now uses two columns: the first
1253column is used for differences between the first parent and the working
1254directory copy, and the second for differences between the second parent
1255and the working directory copy. (See the "COMBINED DIFF FORMAT" section
1256of gitlink:git-diff-files[1] for a details of the format.)
1257
1258After resolving the conflict in the obvious way (but before updating the
1259index), the diff will look like:
d19fbc3c
BF
1260
1261-------------------------------------------------
1262$ git diff
1263diff --cc file.txt
1264index 802992c,2b60207..0000000
1265--- a/file.txt
1266+++ b/file.txt
1267@@@ -1,1 -1,1 +1,1 @@@
1268- Hello world
1269 -Goodbye
1270++Goodbye world
1271-------------------------------------------------
1272
1273This shows that our resolved version deleted "Hello world" from the
1274first parent, deleted "Goodbye" from the second parent, and added
1275"Goodbye world", which was previously absent from both.
1276
ef561ac7
BF
1277Some special diff options allow diffing the working directory against
1278any of these stages:
1279
1280-------------------------------------------------
1281$ git diff -1 file.txt # diff against stage 1
1282$ git diff --base file.txt # same as the above
1283$ git diff -2 file.txt # diff against stage 2
1284$ git diff --ours file.txt # same as the above
1285$ git diff -3 file.txt # diff against stage 3
1286$ git diff --theirs file.txt # same as the above.
1287-------------------------------------------------
1288
1289The gitlink:git-log[1] and gitk[1] commands also provide special help
1290for merges:
d19fbc3c
BF
1291
1292-------------------------------------------------
1293$ git log --merge
ef561ac7 1294$ gitk --merge
d19fbc3c
BF
1295-------------------------------------------------
1296
ef561ac7
BF
1297These will display all commits which exist only on HEAD or on
1298MERGE_HEAD, and which touch an unmerged file.
d19fbc3c 1299
61d72564 1300You may also use gitlink:git-mergetool[1], which lets you merge the
c64415e2
BF
1301unmerged files using external tools such as emacs or kdiff3.
1302
ef561ac7 1303Each time you resolve the conflicts in a file and update the index:
d19fbc3c
BF
1304
1305-------------------------------------------------
1306$ git add file.txt
d19fbc3c
BF
1307-------------------------------------------------
1308
ef561ac7
BF
1309the different stages of that file will be "collapsed", after which
1310git-diff will (by default) no longer show diffs for that file.
d19fbc3c
BF
1311
1312[[undoing-a-merge]]
ae25c67a 1313Undoing a merge
d19fbc3c
BF
1314---------------
1315
1316If you get stuck and decide to just give up and throw the whole mess
1317away, you can always return to the pre-merge state with
1318
1319-------------------------------------------------
1320$ git reset --hard HEAD
1321-------------------------------------------------
1322
1130845b 1323Or, if you've already committed the merge that you want to throw away,
d19fbc3c
BF
1324
1325-------------------------------------------------
1c73bb0e 1326$ git reset --hard ORIG_HEAD
d19fbc3c
BF
1327-------------------------------------------------
1328
1329However, this last command can be dangerous in some cases--never
1330throw away a commit you have already committed if that commit may
1331itself have been merged into another branch, as doing so may confuse
1332further merges.
1333
e34caace 1334[[fast-forwards]]
d19fbc3c
BF
1335Fast-forward merges
1336-------------------
1337
1338There is one special case not mentioned above, which is treated
1339differently. Normally, a merge results in a merge commit, with two
1340parents, one pointing at each of the two lines of development that
1341were merged.
1342
59723040
BF
1343However, if the current branch is a descendant of the other--so every
1344commit present in the one is already contained in the other--then git
1345just performs a "fast forward"; the head of the current branch is moved
1346forward to point at the head of the merged-in branch, without any new
1347commits being created.
d19fbc3c 1348
e34caace 1349[[fixing-mistakes]]
b684f830
BF
1350Fixing mistakes
1351---------------
1352
1353If you've messed up the working tree, but haven't yet committed your
1354mistake, you can return the entire working tree to the last committed
1355state with
1356
1357-------------------------------------------------
1358$ git reset --hard HEAD
1359-------------------------------------------------
1360
1361If you make a commit that you later wish you hadn't, there are two
1362fundamentally different ways to fix the problem:
1363
1364 1. You can create a new commit that undoes whatever was done
1365 by the previous commit. This is the correct thing if your
1366 mistake has already been made public.
1367
1368 2. You can go back and modify the old commit. You should
1369 never do this if you have already made the history public;
1370 git does not normally expect the "history" of a project to
1371 change, and cannot correctly perform repeated merges from
1372 a branch that has had its history changed.
1373
e34caace 1374[[reverting-a-commit]]
b684f830
BF
1375Fixing a mistake with a new commit
1376~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1377
1378Creating a new commit that reverts an earlier change is very easy;
1379just pass the gitlink:git-revert[1] command a reference to the bad
1380commit; for example, to revert the most recent commit:
1381
1382-------------------------------------------------
1383$ git revert HEAD
1384-------------------------------------------------
1385
1386This will create a new commit which undoes the change in HEAD. You
1387will be given a chance to edit the commit message for the new commit.
1388
1389You can also revert an earlier change, for example, the next-to-last:
1390
1391-------------------------------------------------
1392$ git revert HEAD^
1393-------------------------------------------------
1394
1395In this case git will attempt to undo the old change while leaving
1396intact any changes made since then. If more recent changes overlap
1397with the changes to be reverted, then you will be asked to fix
1398conflicts manually, just as in the case of <<resolving-a-merge,
1399resolving a merge>>.
1400
365aa199 1401[[fixing-a-mistake-by-editing-history]]
b684f830
BF
1402Fixing a mistake by editing history
1403~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1404
1405If the problematic commit is the most recent commit, and you have not
1406yet made that commit public, then you may just
1407<<undoing-a-merge,destroy it using git-reset>>.
1408
1409Alternatively, you
1410can edit the working directory and update the index to fix your
1411mistake, just as if you were going to <<how-to-make-a-commit,create a
1412new commit>>, then run
1413
1414-------------------------------------------------
1415$ git commit --amend
1416-------------------------------------------------
1417
1418which will replace the old commit by a new commit incorporating your
1419changes, giving you a chance to edit the old commit message first.
1420
1421Again, you should never do this to a commit that may already have
1422been merged into another branch; use gitlink:git-revert[1] instead in
1423that case.
1424
1425It is also possible to edit commits further back in the history, but
1426this is an advanced topic to be left for
1427<<cleaning-up-history,another chapter>>.
1428
e34caace 1429[[checkout-of-path]]
b684f830
BF
1430Checking out an old version of a file
1431~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1432
1433In the process of undoing a previous bad change, you may find it
1434useful to check out an older version of a particular file using
1435gitlink:git-checkout[1]. We've used git checkout before to switch
1436branches, but it has quite different behavior if it is given a path
1437name: the command
1438
1439-------------------------------------------------
1440$ git checkout HEAD^ path/to/file
1441-------------------------------------------------
1442
1443replaces path/to/file by the contents it had in the commit HEAD^, and
1444also updates the index to match. It does not change branches.
1445
1446If you just want to look at an old version of the file, without
1447modifying the working directory, you can do that with
1448gitlink:git-show[1]:
1449
1450-------------------------------------------------
ed4eb0d8 1451$ git show HEAD^:path/to/file
b684f830
BF
1452-------------------------------------------------
1453
1454which will display the given version of the file.
1455
7a7cc594
JH
1456[[interrupted-work]]
1457Temporarily setting aside work in progress
1458~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1459
1460While you are in the middle of working on something complicated, you
1461find an unrelated but obvious and trivial bug. You would like to fix it
1462before continuing. You can use gitlink:git-stash[1] to save the current
1463state of your work, and after fixing the bug (or, optionally after doing
1464so on a different branch and then coming back), unstash the
1465work-in-progress changes.
1466
1467------------------------------------------------
1468$ git stash "work in progress for foo feature"
1469------------------------------------------------
1470
1471This command will save your changes away to the `stash`, and
1472reset your working tree and the index to match the tip of your
1473current branch. Then you can make your fix as usual.
1474
1475------------------------------------------------
1476... edit and test ...
1477$ git commit -a -m "blorpl: typofix"
1478------------------------------------------------
1479
1480After that, you can go back to what you were working on with
1481`git stash apply`:
1482
1483------------------------------------------------
1484$ git stash apply
1485------------------------------------------------
1486
1487
e34caace 1488[[ensuring-good-performance]]
d19fbc3c
BF
1489Ensuring good performance
1490-------------------------
1491
1492On large repositories, git depends on compression to keep the history
1493information from taking up to much space on disk or in memory.
1494
1495This compression is not performed automatically. Therefore you
17217090 1496should occasionally run gitlink:git-gc[1]:
d19fbc3c
BF
1497
1498-------------------------------------------------
1499$ git gc
1500-------------------------------------------------
1501
17217090
BF
1502to recompress the archive. This can be very time-consuming, so
1503you may prefer to run git-gc when you are not doing other work.
d19fbc3c 1504
e34caace
BF
1505
1506[[ensuring-reliability]]
11e016a3
BF
1507Ensuring reliability
1508--------------------
1509
e34caace 1510[[checking-for-corruption]]
11e016a3
BF
1511Checking the repository for corruption
1512~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1513
1191ee18
BF
1514The gitlink:git-fsck[1] command runs a number of self-consistency checks
1515on the repository, and reports on any problems. This may take some
21dcb3b7
BF
1516time. The most common warning by far is about "dangling" objects:
1517
1518-------------------------------------------------
04e50e94 1519$ git fsck
21dcb3b7
BF
1520dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1521dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1522dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1523dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb
1524dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f
1525dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e
1526dangling tree d50bb86186bf27b681d25af89d3b5b68382e4085
1527dangling tree b24c2473f1fd3d91352a624795be026d64c8841f
1528...
1529-------------------------------------------------
1530
59723040 1531Dangling objects are not a problem. At worst they may take up a little
54782859
AP
1532extra disk space. They can sometimes provide a last-resort method for
1533recovering lost work--see <<dangling-objects>> for details. However, if
1534you wish, you can remove them with gitlink:git-prune[1] or the --prune
1191ee18 1535option to gitlink:git-gc[1]:
21dcb3b7
BF
1536
1537-------------------------------------------------
1538$ git gc --prune
1539-------------------------------------------------
1540
1191ee18
BF
1541This may be time-consuming. Unlike most other git operations (including
1542git-gc when run without any options), it is not safe to prune while
1543other git operations are in progress in the same repository.
21dcb3b7 1544
e34caace 1545[[recovering-lost-changes]]
11e016a3
BF
1546Recovering lost changes
1547~~~~~~~~~~~~~~~~~~~~~~~
1548
e34caace 1549[[reflogs]]
559e4d7a
BF
1550Reflogs
1551^^^^^^^
1552
1553Say you modify a branch with gitlink:git-reset[1] --hard, and then
1554realize that the branch was the only reference you had to that point in
1555history.
1556
1557Fortunately, git also keeps a log, called a "reflog", of all the
1558previous values of each branch. So in this case you can still find the
a6080a0a 1559old history using, for example,
559e4d7a
BF
1560
1561-------------------------------------------------
1562$ git log master@{1}
1563-------------------------------------------------
1564
1565This lists the commits reachable from the previous version of the head.
1566This syntax can be used to with any git command that accepts a commit,
1567not just with git log. Some other examples:
1568
1569-------------------------------------------------
1570$ git show master@{2} # See where the branch pointed 2,
1571$ git show master@{3} # 3, ... changes ago.
1572$ gitk master@{yesterday} # See where it pointed yesterday,
1573$ gitk master@{"1 week ago"} # ... or last week
953f3d6f
BF
1574$ git log --walk-reflogs master # show reflog entries for master
1575-------------------------------------------------
1576
1577A separate reflog is kept for the HEAD, so
1578
1579-------------------------------------------------
1580$ git show HEAD@{"1 week ago"}
559e4d7a
BF
1581-------------------------------------------------
1582
953f3d6f
BF
1583will show what HEAD pointed to one week ago, not what the current branch
1584pointed to one week ago. This allows you to see the history of what
1585you've checked out.
1586
559e4d7a 1587The reflogs are kept by default for 30 days, after which they may be
036be17e 1588pruned. See gitlink:git-reflog[1] and gitlink:git-gc[1] to learn
559e4d7a
BF
1589how to control this pruning, and see the "SPECIFYING REVISIONS"
1590section of gitlink:git-rev-parse[1] for details.
1591
1592Note that the reflog history is very different from normal git history.
1593While normal history is shared by every repository that works on the
1594same project, the reflog history is not shared: it tells you only about
1595how the branches in your local repository have changed over time.
1596
59723040 1597[[dangling-object-recovery]]
559e4d7a
BF
1598Examining dangling objects
1599^^^^^^^^^^^^^^^^^^^^^^^^^^
1600
59723040
BF
1601In some situations the reflog may not be able to save you. For example,
1602suppose you delete a branch, then realize you need the history it
1603contained. The reflog is also deleted; however, if you have not yet
1604pruned the repository, then you may still be able to find the lost
1605commits in the dangling objects that git-fsck reports. See
1606<<dangling-objects>> for the details.
559e4d7a
BF
1607
1608-------------------------------------------------
1609$ git fsck
1610dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1611dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1612dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1613...
1614-------------------------------------------------
1615
aacd404e 1616You can examine
559e4d7a
BF
1617one of those dangling commits with, for example,
1618
1619------------------------------------------------
1620$ gitk 7281251ddd --not --all
1621------------------------------------------------
1622
1623which does what it sounds like: it says that you want to see the commit
1624history that is described by the dangling commit(s), but not the
1625history that is described by all your existing branches and tags. Thus
1626you get exactly the history reachable from that commit that is lost.
1627(And notice that it might not be just one commit: we only report the
1628"tip of the line" as being dangling, but there might be a whole deep
79c96c57 1629and complex commit history that was dropped.)
559e4d7a
BF
1630
1631If you decide you want the history back, you can always create a new
1632reference pointing to it, for example, a new branch:
1633
1634------------------------------------------------
a6080a0a 1635$ git branch recovered-branch 7281251ddd
559e4d7a
BF
1636------------------------------------------------
1637
59723040
BF
1638Other types of dangling objects (blobs and trees) are also possible, and
1639dangling objects can arise in other situations.
1640
11e016a3 1641
e34caace 1642[[sharing-development]]
d19fbc3c 1643Sharing development with others
b684f830 1644===============================
d19fbc3c
BF
1645
1646[[getting-updates-with-git-pull]]
1647Getting updates with git pull
b684f830 1648-----------------------------
d19fbc3c
BF
1649
1650After you clone a repository and make a few changes of your own, you
1651may wish to check the original repository for updates and merge them
1652into your own work.
1653
1654We have already seen <<Updating-a-repository-with-git-fetch,how to
1655keep remote tracking branches up to date>> with gitlink:git-fetch[1],
1656and how to merge two branches. So you can merge in changes from the
1657original repository's master branch with:
1658
1659-------------------------------------------------
1660$ git fetch
1661$ git merge origin/master
1662-------------------------------------------------
1663
1664However, the gitlink:git-pull[1] command provides a way to do this in
1665one step:
1666
1667-------------------------------------------------
1668$ git pull origin master
1669-------------------------------------------------
1670
0eb4f7cd
BF
1671In fact, if you have "master" checked out, then by default "git pull"
1672merges from the HEAD branch of the origin repository. So often you can
1673accomplish the above with just a simple
d19fbc3c
BF
1674
1675-------------------------------------------------
1676$ git pull
1677-------------------------------------------------
1678
0eb4f7cd
BF
1679More generally, a branch that is created from a remote branch will pull
1680by default from that branch. See the descriptions of the
1681branch.<name>.remote and branch.<name>.merge options in
1682gitlink:git-config[1], and the discussion of the --track option in
1683gitlink:git-checkout[1], to learn how to control these defaults.
d19fbc3c
BF
1684
1685In addition to saving you keystrokes, "git pull" also helps you by
1686producing a default commit message documenting the branch and
1687repository that you pulled from.
1688
1689(But note that no such commit will be created in the case of a
1690<<fast-forwards,fast forward>>; instead, your branch will just be
79c96c57 1691updated to point to the latest commit from the upstream branch.)
d19fbc3c 1692
1191ee18
BF
1693The git-pull command can also be given "." as the "remote" repository,
1694in which case it just merges in a branch from the current repository; so
4c63ff45
BF
1695the commands
1696
1697-------------------------------------------------
1698$ git pull . branch
1699$ git merge branch
1700-------------------------------------------------
1701
1702are roughly equivalent. The former is actually very commonly used.
1703
e34caace 1704[[submitting-patches]]
d19fbc3c 1705Submitting patches to a project
b684f830 1706-------------------------------
d19fbc3c
BF
1707
1708If you just have a few changes, the simplest way to submit them may
1709just be to send them as patches in email:
1710
036be17e 1711First, use gitlink:git-format-patch[1]; for example:
d19fbc3c
BF
1712
1713-------------------------------------------------
eb6ae7f4 1714$ git format-patch origin
d19fbc3c
BF
1715-------------------------------------------------
1716
1717will produce a numbered series of files in the current directory, one
1718for each patch in the current branch but not in origin/HEAD.
1719
1720You can then import these into your mail client and send them by
1721hand. However, if you have a lot to send at once, you may prefer to
1722use the gitlink:git-send-email[1] script to automate the process.
1723Consult the mailing list for your project first to determine how they
1724prefer such patches be handled.
1725
e34caace 1726[[importing-patches]]
d19fbc3c 1727Importing patches to a project
b684f830 1728------------------------------
d19fbc3c
BF
1729
1730Git also provides a tool called gitlink:git-am[1] (am stands for
1731"apply mailbox"), for importing such an emailed series of patches.
1732Just save all of the patch-containing messages, in order, into a
1733single mailbox file, say "patches.mbox", then run
1734
1735-------------------------------------------------
eb6ae7f4 1736$ git am -3 patches.mbox
d19fbc3c
BF
1737-------------------------------------------------
1738
1739Git will apply each patch in order; if any conflicts are found, it
1740will stop, and you can fix the conflicts as described in
01997b4a
BF
1741"<<resolving-a-merge,Resolving a merge>>". (The "-3" option tells
1742git to perform a merge; if you would prefer it just to abort and
1743leave your tree and index untouched, you may omit that option.)
1744
1745Once the index is updated with the results of the conflict
1746resolution, instead of creating a new commit, just run
d19fbc3c
BF
1747
1748-------------------------------------------------
1749$ git am --resolved
1750-------------------------------------------------
1751
1752and git will create the commit for you and continue applying the
1753remaining patches from the mailbox.
1754
1755The final result will be a series of commits, one for each patch in
1756the original mailbox, with authorship and commit log message each
1757taken from the message containing each patch.
1758
eda69449
BF
1759[[public-repositories]]
1760Public git repositories
1761-----------------------
d19fbc3c 1762
6e30fb0c
DK
1763Another way to submit changes to a project is to tell the maintainer
1764of that project to pull the changes from your repository using
1765gitlink:git-pull[1]. In the section "<<getting-updates-with-git-pull,
1766Getting updates with git pull>>" we described this as a way to get
1767updates from the "main" repository, but it works just as well in the
1768other direction.
d19fbc3c 1769
eda69449
BF
1770If you and the maintainer both have accounts on the same machine, then
1771you can just pull changes from each other's repositories directly;
11d51533 1772commands that accept repository URLs as arguments will also accept a
eda69449 1773local directory name:
d19fbc3c
BF
1774
1775-------------------------------------------------
1776$ git clone /path/to/repository
1777$ git pull /path/to/other/repository
1778-------------------------------------------------
1779
11d51533
BF
1780or an ssh url:
1781
1782-------------------------------------------------
1783$ git clone ssh://yourhost/~you/repository
1784-------------------------------------------------
1785
1786For projects with few developers, or for synchronizing a few private
1787repositories, this may be all you need.
1788
eda69449
BF
1789However, the more common way to do this is to maintain a separate public
1790repository (usually on a different host) for others to pull changes
1791from. This is usually more convenient, and allows you to cleanly
1792separate private work in progress from publicly visible work.
d19fbc3c
BF
1793
1794You will continue to do your day-to-day work in your personal
1795repository, but periodically "push" changes from your personal
1796repository into your public repository, allowing other developers to
1797pull from that repository. So the flow of changes, in a situation
1798where there is one other developer with a public repository, looks
1799like this:
1800
1801 you push
1802 your personal repo ------------------> your public repo
a6080a0a 1803 ^ |
d19fbc3c
BF
1804 | |
1805 | you pull | they pull
1806 | |
1807 | |
1808 | they push V
1809 their public repo <------------------- their repo
1810
11d51533
BF
1811We explain how to do this in the following sections.
1812
eda69449
BF
1813[[setting-up-a-public-repository]]
1814Setting up a public repository
1815~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1816
1817Assume your personal repository is in the directory ~/proj. We
1818first create a new clone of the repository and tell git-daemon that it
1819is meant to be public:
d19fbc3c
BF
1820
1821-------------------------------------------------
52c80037 1822$ git clone --bare ~/proj proj.git
eda69449 1823$ touch proj.git/git-daemon-export-ok
d19fbc3c
BF
1824-------------------------------------------------
1825
52c80037 1826The resulting directory proj.git contains a "bare" git repository--it is
eda69449
BF
1827just the contents of the ".git" directory, without any files checked out
1828around it.
d19fbc3c 1829
c64415e2 1830Next, copy proj.git to the server where you plan to host the
d19fbc3c
BF
1831public repository. You can use scp, rsync, or whatever is most
1832convenient.
1833
eda69449
BF
1834[[exporting-via-git]]
1835Exporting a git repository via the git protocol
1836~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1837
1838This is the preferred method.
1839
1840If someone else administers the server, they should tell you what
1841directory to put the repository in, and what git:// url it will appear
1842at. You can then skip to the section
d19fbc3c
BF
1843"<<pushing-changes-to-a-public-repository,Pushing changes to a public
1844repository>>", below.
1845
eda69449
BF
1846Otherwise, all you need to do is start gitlink:git-daemon[1]; it will
1847listen on port 9418. By default, it will allow access to any directory
1848that looks like a git directory and contains the magic file
1849git-daemon-export-ok. Passing some directory paths as git-daemon
1850arguments will further restrict the exports to those paths.
1851
1852You can also run git-daemon as an inetd service; see the
1853gitlink:git-daemon[1] man page for details. (See especially the
1854examples section.)
d19fbc3c
BF
1855
1856[[exporting-via-http]]
1857Exporting a git repository via http
eda69449 1858~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c
BF
1859
1860The git protocol gives better performance and reliability, but on a
1861host with a web server set up, http exports may be simpler to set up.
1862
1863All you need to do is place the newly created bare git repository in
1864a directory that is exported by the web server, and make some
1865adjustments to give web clients some extra information they need:
1866
1867-------------------------------------------------
1868$ mv proj.git /home/you/public_html/proj.git
1869$ cd proj.git
c64415e2 1870$ git --bare update-server-info
d19fbc3c
BF
1871$ chmod a+x hooks/post-update
1872-------------------------------------------------
1873
1874(For an explanation of the last two lines, see
1875gitlink:git-update-server-info[1], and the documentation
a2983cb7 1876link:hooks.html[Hooks used by git].)
d19fbc3c
BF
1877
1878Advertise the url of proj.git. Anybody else should then be able to
02783075 1879clone or pull from that url, for example with a command line like:
d19fbc3c
BF
1880
1881-------------------------------------------------
1882$ git clone http://yourserver.com/~you/proj.git
1883-------------------------------------------------
1884
1885(See also
1886link:howto/setup-git-server-over-http.txt[setup-git-server-over-http]
1887for a slightly more sophisticated setup using WebDAV which also
1888allows pushing over http.)
1889
d19fbc3c
BF
1890[[pushing-changes-to-a-public-repository]]
1891Pushing changes to a public repository
eda69449 1892~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 1893
eda69449 1894Note that the two techniques outlined above (exporting via
d19fbc3c
BF
1895<<exporting-via-http,http>> or <<exporting-via-git,git>>) allow other
1896maintainers to fetch your latest changes, but they do not allow write
1897access, which you will need to update the public repository with the
1898latest changes created in your private repository.
1899
1900The simplest way to do this is using gitlink:git-push[1] and ssh; to
1901update the remote branch named "master" with the latest state of your
1902branch named "master", run
1903
1904-------------------------------------------------
1905$ git push ssh://yourserver.com/~you/proj.git master:master
1906-------------------------------------------------
1907
1908or just
1909
1910-------------------------------------------------
1911$ git push ssh://yourserver.com/~you/proj.git master
1912-------------------------------------------------
1913
1914As with git-fetch, git-push will complain if this does not result in
1915a <<fast-forwards,fast forward>>. Normally this is a sign of
1916something wrong. However, if you are sure you know what you're
1917doing, you may force git-push to perform the update anyway by
1918proceeding the branch name by a plus sign:
1919
1920-------------------------------------------------
1921$ git push ssh://yourserver.com/~you/proj.git +master
1922-------------------------------------------------
1923
11d51533
BF
1924Note that the target of a "push" is normally a
1925<<def_bare_repository,bare>> repository. You can also push to a
1926repository that has a checked-out working tree, but the working tree
1927will not be updated by the push. This may lead to unexpected results if
1928the branch you push to is the currently checked-out branch!
1929
d19fbc3c
BF
1930As with git-fetch, you may also set up configuration options to
1931save typing; so, for example, after
1932
1933-------------------------------------------------
c64415e2 1934$ cat >>.git/config <<EOF
d19fbc3c
BF
1935[remote "public-repo"]
1936 url = ssh://yourserver.com/~you/proj.git
1937EOF
1938-------------------------------------------------
1939
1940you should be able to perform the above push with just
1941
1942-------------------------------------------------
1943$ git push public-repo master
1944-------------------------------------------------
1945
1946See the explanations of the remote.<name>.url, branch.<name>.remote,
9d13bda3 1947and remote.<name>.push options in gitlink:git-config[1] for
d19fbc3c
BF
1948details.
1949
e34caace 1950[[setting-up-a-shared-repository]]
d19fbc3c 1951Setting up a shared repository
eda69449 1952~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c
BF
1953
1954Another way to collaborate is by using a model similar to that
1955commonly used in CVS, where several developers with special rights
1956all push to and pull from a single shared repository. See
a2983cb7 1957link:cvs-migration.html[git for CVS users] for instructions on how to
d19fbc3c
BF
1958set this up.
1959
8fae2225
BF
1960However, while there is nothing wrong with git's support for shared
1961repositories, this mode of operation is not generally recommended,
1962simply because the mode of collaboration that git supports--by
1963exchanging patches and pulling from public repositories--has so many
1964advantages over the central shared repository:
1965
1966 - Git's ability to quickly import and merge patches allows a
1967 single maintainer to process incoming changes even at very
1968 high rates. And when that becomes too much, git-pull provides
1969 an easy way for that maintainer to delegate this job to other
1970 maintainers while still allowing optional review of incoming
1971 changes.
1972 - Since every developer's repository has the same complete copy
1973 of the project history, no repository is special, and it is
1974 trivial for another developer to take over maintenance of a
1975 project, either by mutual agreement, or because a maintainer
1976 becomes unresponsive or difficult to work with.
1977 - The lack of a central group of "committers" means there is
1978 less need for formal decisions about who is "in" and who is
1979 "out".
1980
e34caace 1981[[setting-up-gitweb]]
eda69449
BF
1982Allowing web browsing of a repository
1983~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d19fbc3c 1984
a8cd1402
BF
1985The gitweb cgi script provides users an easy way to browse your
1986project's files and history without having to install git; see the file
04483524 1987gitweb/INSTALL in the git source tree for instructions on setting it up.
d19fbc3c 1988
e34caace 1989[[sharing-development-examples]]
b684f830
BF
1990Examples
1991--------
d19fbc3c 1992
9e2163ea
BF
1993[[maintaining-topic-branches]]
1994Maintaining topic branches for a Linux subsystem maintainer
1995~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1996
1997This describes how Tony Luck uses git in his role as maintainer of the
1998IA64 architecture for the Linux kernel.
1999
2000He uses two public branches:
2001
2002 - A "test" tree into which patches are initially placed so that they
2003 can get some exposure when integrated with other ongoing development.
2004 This tree is available to Andrew for pulling into -mm whenever he
2005 wants.
2006
2007 - A "release" tree into which tested patches are moved for final sanity
2008 checking, and as a vehicle to send them upstream to Linus (by sending
2009 him a "please pull" request.)
2010
2011He also uses a set of temporary branches ("topic branches"), each
2012containing a logical grouping of patches.
2013
2014To set this up, first create your work tree by cloning Linus's public
2015tree:
2016
2017-------------------------------------------------
2018$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git work
2019$ cd work
2020-------------------------------------------------
2021
2022Linus's tree will be stored in the remote branch named origin/master,
2023and can be updated using gitlink:git-fetch[1]; you can track other
2024public trees using gitlink:git-remote[1] to set up a "remote" and
6e30fb0c
DK
2025gitlink:git-fetch[1] to keep them up-to-date; see
2026<<repositories-and-branches>>.
9e2163ea
BF
2027
2028Now create the branches in which you are going to work; these start out
2029at the current tip of origin/master branch, and should be set up (using
2030the --track option to gitlink:git-branch[1]) to merge changes in from
2031Linus by default.
2032
2033-------------------------------------------------
2034$ git branch --track test origin/master
2035$ git branch --track release origin/master
2036-------------------------------------------------
2037
2038These can be easily kept up to date using gitlink:git-pull[1]
2039
2040-------------------------------------------------
2041$ git checkout test && git pull
2042$ git checkout release && git pull
2043-------------------------------------------------
2044
2045Important note! If you have any local changes in these branches, then
2046this merge will create a commit object in the history (with no local
2047changes git will simply do a "Fast forward" merge). Many people dislike
2048the "noise" that this creates in the Linux history, so you should avoid
2049doing this capriciously in the "release" branch, as these noisy commits
2050will become part of the permanent history when you ask Linus to pull
2051from the release branch.
2052
2053A few configuration variables (see gitlink:git-config[1]) can
2054make it easy to push both branches to your public tree. (See
2055<<setting-up-a-public-repository>>.)
2056
2057-------------------------------------------------
2058$ cat >> .git/config <<EOF
2059[remote "mytree"]
2060 url = master.kernel.org:/pub/scm/linux/kernel/git/aegl/linux-2.6.git
2061 push = release
2062 push = test
2063EOF
2064-------------------------------------------------
2065
2066Then you can push both the test and release trees using
2067gitlink:git-push[1]:
2068
2069-------------------------------------------------
2070$ git push mytree
2071-------------------------------------------------
2072
2073or push just one of the test and release branches using:
2074
2075-------------------------------------------------
2076$ git push mytree test
2077-------------------------------------------------
2078
2079or
2080
2081-------------------------------------------------
2082$ git push mytree release
2083-------------------------------------------------
2084
2085Now to apply some patches from the community. Think of a short
2086snappy name for a branch to hold this patch (or related group of
2087patches), and create a new branch from the current tip of Linus's
2088branch:
2089
2090-------------------------------------------------
2091$ git checkout -b speed-up-spinlocks origin
2092-------------------------------------------------
2093
2094Now you apply the patch(es), run some tests, and commit the change(s). If
2095the patch is a multi-part series, then you should apply each as a separate
2096commit to this branch.
2097
2098-------------------------------------------------
2099$ ... patch ... test ... commit [ ... patch ... test ... commit ]*
2100-------------------------------------------------
2101
2102When you are happy with the state of this change, you can pull it into the
2103"test" branch in preparation to make it public:
2104
2105-------------------------------------------------
2106$ git checkout test && git pull . speed-up-spinlocks
2107-------------------------------------------------
2108
2109It is unlikely that you would have any conflicts here ... but you might if you
2110spent a while on this step and had also pulled new versions from upstream.
2111
2112Some time later when enough time has passed and testing done, you can pull the
2113same branch into the "release" tree ready to go upstream. This is where you
2114see the value of keeping each patch (or patch series) in its own branch. It
2115means that the patches can be moved into the "release" tree in any order.
2116
2117-------------------------------------------------
2118$ git checkout release && git pull . speed-up-spinlocks
2119-------------------------------------------------
2120
2121After a while, you will have a number of branches, and despite the
2122well chosen names you picked for each of them, you may forget what
2123they are for, or what status they are in. To get a reminder of what
2124changes are in a specific branch, use:
2125
2126-------------------------------------------------
2127$ git log linux..branchname | git-shortlog
2128-------------------------------------------------
2129
2130To see whether it has already been merged into the test or release branches
2131use:
2132
2133-------------------------------------------------
2134$ git log test..branchname
2135-------------------------------------------------
2136
2137or
2138
2139-------------------------------------------------
2140$ git log release..branchname
2141-------------------------------------------------
2142
2143(If this branch has not yet been merged you will see some log entries.
2144If it has been merged, then there will be no output.)
2145
2146Once a patch completes the great cycle (moving from test to release,
2147then pulled by Linus, and finally coming back into your local
2148"origin/master" branch) the branch for this change is no longer needed.
2149You detect this when the output from:
2150
2151-------------------------------------------------
2152$ git log origin..branchname
2153-------------------------------------------------
2154
2155is empty. At this point the branch can be deleted:
2156
2157-------------------------------------------------
2158$ git branch -d branchname
2159-------------------------------------------------
2160
2161Some changes are so trivial that it is not necessary to create a separate
2162branch and then merge into each of the test and release branches. For
2163these changes, just apply directly to the "release" branch, and then
2164merge that into the "test" branch.
2165
2166To create diffstat and shortlog summaries of changes to include in a "please
2167pull" request to Linus you can use:
2168
2169-------------------------------------------------
2170$ git diff --stat origin..release
2171-------------------------------------------------
2172
2173and
2174
2175-------------------------------------------------
2176$ git log -p origin..release | git shortlog
2177-------------------------------------------------
2178
2179Here are some of the scripts that simplify all this even further.
2180
2181-------------------------------------------------
2182==== update script ====
2183# Update a branch in my GIT tree. If the branch to be updated
2184# is origin, then pull from kernel.org. Otherwise merge
2185# origin/master branch into test|release branch
2186
2187case "$1" in
2188test|release)
2189 git checkout $1 && git pull . origin
2190 ;;
2191origin)
2192 before=$(cat .git/refs/remotes/origin/master)
2193 git fetch origin
2194 after=$(cat .git/refs/remotes/origin/master)
2195 if [ $before != $after ]
2196 then
2197 git log $before..$after | git shortlog
2198 fi
2199 ;;
2200*)
2201 echo "Usage: $0 origin|test|release" 1>&2
2202 exit 1
2203 ;;
2204esac
2205-------------------------------------------------
2206
2207-------------------------------------------------
2208==== merge script ====
2209# Merge a branch into either the test or release branch
2210
2211pname=$0
2212
2213usage()
2214{
2215 echo "Usage: $pname branch test|release" 1>&2
2216 exit 1
2217}
2218
2219if [ ! -f .git/refs/heads/"$1" ]
2220then
2221 echo "Can't see branch <$1>" 1>&2
2222 usage
2223fi
2224
2225case "$2" in
2226test|release)
2227 if [ $(git log $2..$1 | wc -c) -eq 0 ]
2228 then
2229 echo $1 already merged into $2 1>&2
2230 exit 1
2231 fi
2232 git checkout $2 && git pull . $1
2233 ;;
2234*)
2235 usage
2236 ;;
2237esac
2238-------------------------------------------------
2239
2240-------------------------------------------------
2241==== status script ====
2242# report on status of my ia64 GIT tree
2243
2244gb=$(tput setab 2)
2245rb=$(tput setab 1)
2246restore=$(tput setab 9)
2247
2248if [ `git rev-list test..release | wc -c` -gt 0 ]
2249then
2250 echo $rb Warning: commits in release that are not in test $restore
2251 git log test..release
2252fi
2253
2254for branch in `ls .git/refs/heads`
2255do
2256 if [ $branch = test -o $branch = release ]
2257 then
2258 continue
2259 fi
2260
2261 echo -n $gb ======= $branch ====== $restore " "
2262 status=
2263 for ref in test release origin/master
2264 do
2265 if [ `git rev-list $ref..$branch | wc -c` -gt 0 ]
2266 then
2267 status=$status${ref:0:1}
2268 fi
2269 done
2270 case $status in
2271 trl)
2272 echo $rb Need to pull into test $restore
2273 ;;
2274 rl)
2275 echo "In test"
2276 ;;
2277 l)
2278 echo "Waiting for linus"
2279 ;;
2280 "")
2281 echo $rb All done $restore
2282 ;;
2283 *)
2284 echo $rb "<$status>" $restore
2285 ;;
2286 esac
2287 git log origin/master..$branch | git shortlog
2288done
2289-------------------------------------------------
d19fbc3c 2290
d19fbc3c 2291
d19fbc3c 2292[[cleaning-up-history]]
4c63ff45
BF
2293Rewriting history and maintaining patch series
2294==============================================
2295
2296Normally commits are only added to a project, never taken away or
2297replaced. Git is designed with this assumption, and violating it will
2298cause git's merge machinery (for example) to do the wrong thing.
2299
2300However, there is a situation in which it can be useful to violate this
2301assumption.
2302
e34caace 2303[[patch-series]]
4c63ff45
BF
2304Creating the perfect patch series
2305---------------------------------
2306
2307Suppose you are a contributor to a large project, and you want to add a
2308complicated feature, and to present it to the other developers in a way
2309that makes it easy for them to read your changes, verify that they are
2310correct, and understand why you made each change.
2311
b181d57f 2312If you present all of your changes as a single patch (or commit), they
79c96c57 2313may find that it is too much to digest all at once.
4c63ff45
BF
2314
2315If you present them with the entire history of your work, complete with
2316mistakes, corrections, and dead ends, they may be overwhelmed.
2317
2318So the ideal is usually to produce a series of patches such that:
2319
2320 1. Each patch can be applied in order.
2321
2322 2. Each patch includes a single logical change, together with a
2323 message explaining the change.
2324
2325 3. No patch introduces a regression: after applying any initial
2326 part of the series, the resulting project still compiles and
2327 works, and has no bugs that it didn't have before.
2328
2329 4. The complete series produces the same end result as your own
2330 (probably much messier!) development process did.
2331
b181d57f
BF
2332We will introduce some tools that can help you do this, explain how to
2333use them, and then explain some of the problems that can arise because
2334you are rewriting history.
4c63ff45 2335
e34caace 2336[[using-git-rebase]]
4c63ff45
BF
2337Keeping a patch series up to date using git-rebase
2338--------------------------------------------------
2339
79c96c57
MC
2340Suppose that you create a branch "mywork" on a remote-tracking branch
2341"origin", and create some commits on top of it:
4c63ff45
BF
2342
2343-------------------------------------------------
2344$ git checkout -b mywork origin
2345$ vi file.txt
2346$ git commit
2347$ vi otherfile.txt
2348$ git commit
2349...
2350-------------------------------------------------
2351
2352You have performed no merges into mywork, so it is just a simple linear
2353sequence of patches on top of "origin":
2354
1dc71a91 2355................................................
4c63ff45
BF
2356 o--o--o <-- origin
2357 \
2358 o--o--o <-- mywork
1dc71a91 2359................................................
4c63ff45
BF
2360
2361Some more interesting work has been done in the upstream project, and
2362"origin" has advanced:
2363
1dc71a91 2364................................................
4c63ff45
BF
2365 o--o--O--o--o--o <-- origin
2366 \
2367 a--b--c <-- mywork
1dc71a91 2368................................................
4c63ff45
BF
2369
2370At this point, you could use "pull" to merge your changes back in;
2371the result would create a new merge commit, like this:
2372
1dc71a91 2373................................................
4c63ff45
BF
2374 o--o--O--o--o--o <-- origin
2375 \ \
2376 a--b--c--m <-- mywork
1dc71a91 2377................................................
a6080a0a 2378
4c63ff45
BF
2379However, if you prefer to keep the history in mywork a simple series of
2380commits without any merges, you may instead choose to use
2381gitlink:git-rebase[1]:
2382
2383-------------------------------------------------
2384$ git checkout mywork
2385$ git rebase origin
2386-------------------------------------------------
2387
b181d57f
BF
2388This will remove each of your commits from mywork, temporarily saving
2389them as patches (in a directory named ".dotest"), update mywork to
2390point at the latest version of origin, then apply each of the saved
2391patches to the new mywork. The result will look like:
4c63ff45
BF
2392
2393
1dc71a91 2394................................................
4c63ff45
BF
2395 o--o--O--o--o--o <-- origin
2396 \
2397 a'--b'--c' <-- mywork
1dc71a91 2398................................................
4c63ff45 2399
b181d57f
BF
2400In the process, it may discover conflicts. In that case it will stop
2401and allow you to fix the conflicts; after fixing conflicts, use "git
2402add" to update the index with those contents, and then, instead of
2403running git-commit, just run
4c63ff45
BF
2404
2405-------------------------------------------------
2406$ git rebase --continue
2407-------------------------------------------------
2408
2409and git will continue applying the rest of the patches.
2410
2411At any point you may use the --abort option to abort this process and
2412return mywork to the state it had before you started the rebase:
2413
2414-------------------------------------------------
2415$ git rebase --abort
2416-------------------------------------------------
2417
e34caace 2418[[modifying-one-commit]]
365aa199
BF
2419Modifying a single commit
2420-------------------------
2421
2422We saw in <<fixing-a-mistake-by-editing-history>> that you can replace the
2423most recent commit using
2424
2425-------------------------------------------------
2426$ git commit --amend
2427-------------------------------------------------
2428
2429which will replace the old commit by a new commit incorporating your
2430changes, giving you a chance to edit the old commit message first.
2431
2432You can also use a combination of this and gitlink:git-rebase[1] to edit
2433commits further back in your history. First, tag the problematic commit with
2434
2435-------------------------------------------------
2436$ git tag bad mywork~5
2437-------------------------------------------------
2438
2439(Either gitk or git-log may be useful for finding the commit.)
2440
25d9f3fa
BF
2441Then check out that commit, edit it, and rebase the rest of the series
2442on top of it (note that we could check out the commit on a temporary
2443branch, but instead we're using a <<detached-head,detached head>>):
365aa199
BF
2444
2445-------------------------------------------------
25d9f3fa 2446$ git checkout bad
365aa199
BF
2447$ # make changes here and update the index
2448$ git commit --amend
25d9f3fa 2449$ git rebase --onto HEAD bad mywork
365aa199
BF
2450-------------------------------------------------
2451
25d9f3fa
BF
2452When you're done, you'll be left with mywork checked out, with the top
2453patches on mywork reapplied on top of your modified commit. You can
365aa199
BF
2454then clean up with
2455
2456-------------------------------------------------
365aa199
BF
2457$ git tag -d bad
2458-------------------------------------------------
2459
2460Note that the immutable nature of git history means that you haven't really
2461"modified" existing commits; instead, you have replaced the old commits with
2462new commits having new object names.
2463
e34caace 2464[[reordering-patch-series]]
4c63ff45
BF
2465Reordering or selecting from a patch series
2466-------------------------------------------
2467
b181d57f
BF
2468Given one existing commit, the gitlink:git-cherry-pick[1] command
2469allows you to apply the change introduced by that commit and create a
2470new commit that records it. So, for example, if "mywork" points to a
2471series of patches on top of "origin", you might do something like:
2472
2473-------------------------------------------------
2474$ git checkout -b mywork-new origin
2475$ gitk origin..mywork &
2476-------------------------------------------------
2477
2478And browse through the list of patches in the mywork branch using gitk,
2479applying them (possibly in a different order) to mywork-new using
407c0c87 2480cherry-pick, and possibly modifying them as you go using commit --amend.
6e30fb0c
DK
2481The gitlink:git-gui[1] command may also help as it allows you to
2482individually select diff hunks for inclusion in the index (by
2483right-clicking on the diff hunk and choosing "Stage Hunk for Commit").
b181d57f
BF
2484
2485Another technique is to use git-format-patch to create a series of
2486patches, then reset the state to before the patches:
4c63ff45 2487
b181d57f
BF
2488-------------------------------------------------
2489$ git format-patch origin
2490$ git reset --hard origin
2491-------------------------------------------------
4c63ff45 2492
b181d57f
BF
2493Then modify, reorder, or eliminate patches as preferred before applying
2494them again with gitlink:git-am[1].
4c63ff45 2495
e34caace 2496[[patch-series-tools]]
4c63ff45
BF
2497Other tools
2498-----------
2499
02783075 2500There are numerous other tools, such as StGIT, which exist for the
79c96c57 2501purpose of maintaining a patch series. These are outside of the scope of
b181d57f 2502this manual.
4c63ff45 2503
e34caace 2504[[problems-with-rewriting-history]]
4c63ff45
BF
2505Problems with rewriting history
2506-------------------------------
2507
b181d57f
BF
2508The primary problem with rewriting the history of a branch has to do
2509with merging. Suppose somebody fetches your branch and merges it into
2510their branch, with a result something like this:
2511
1dc71a91 2512................................................
b181d57f
BF
2513 o--o--O--o--o--o <-- origin
2514 \ \
2515 t--t--t--m <-- their branch:
1dc71a91 2516................................................
b181d57f
BF
2517
2518Then suppose you modify the last three commits:
2519
1dc71a91 2520................................................
b181d57f
BF
2521 o--o--o <-- new head of origin
2522 /
2523 o--o--O--o--o--o <-- old head of origin
1dc71a91 2524................................................
b181d57f
BF
2525
2526If we examined all this history together in one repository, it will
2527look like:
2528
1dc71a91 2529................................................
b181d57f
BF
2530 o--o--o <-- new head of origin
2531 /
2532 o--o--O--o--o--o <-- old head of origin
2533 \ \
2534 t--t--t--m <-- their branch:
1dc71a91 2535................................................
b181d57f
BF
2536
2537Git has no way of knowing that the new head is an updated version of
2538the old head; it treats this situation exactly the same as it would if
2539two developers had independently done the work on the old and new heads
2540in parallel. At this point, if someone attempts to merge the new head
2541in to their branch, git will attempt to merge together the two (old and
2542new) lines of development, instead of trying to replace the old by the
2543new. The results are likely to be unexpected.
2544
2545You may still choose to publish branches whose history is rewritten,
2546and it may be useful for others to be able to fetch those branches in
2547order to examine or test them, but they should not attempt to pull such
2548branches into their own work.
2549
2550For true distributed development that supports proper merging,
2551published branches should never be rewritten.
2552
e34caace 2553[[advanced-branch-management]]
b181d57f
BF
2554Advanced branch management
2555==========================
4c63ff45 2556
e34caace 2557[[fetching-individual-branches]]
b181d57f
BF
2558Fetching individual branches
2559----------------------------
2560
2561Instead of using gitlink:git-remote[1], you can also choose just
2562to update one branch at a time, and to store it locally under an
2563arbitrary name:
2564
2565-------------------------------------------------
2566$ git fetch origin todo:my-todo-work
2567-------------------------------------------------
2568
2569The first argument, "origin", just tells git to fetch from the
2570repository you originally cloned from. The second argument tells git
2571to fetch the branch named "todo" from the remote repository, and to
2572store it locally under the name refs/heads/my-todo-work.
2573
2574You can also fetch branches from other repositories; so
2575
2576-------------------------------------------------
2577$ git fetch git://example.com/proj.git master:example-master
2578-------------------------------------------------
2579
2580will create a new branch named "example-master" and store in it the
2581branch named "master" from the repository at the given URL. If you
2582already have a branch named example-master, it will attempt to
59723040
BF
2583<<fast-forwards,fast-forward>> to the commit given by example.com's
2584master branch. In more detail:
b181d57f 2585
59723040
BF
2586[[fetch-fast-forwards]]
2587git fetch and fast-forwards
2588---------------------------
b181d57f
BF
2589
2590In the previous example, when updating an existing branch, "git
2591fetch" checks to make sure that the most recent commit on the remote
2592branch is a descendant of the most recent commit on your copy of the
2593branch before updating your copy of the branch to point at the new
59723040 2594commit. Git calls this process a <<fast-forwards,fast forward>>.
b181d57f
BF
2595
2596A fast forward looks something like this:
2597
1dc71a91 2598................................................
b181d57f
BF
2599 o--o--o--o <-- old head of the branch
2600 \
2601 o--o--o <-- new head of the branch
1dc71a91 2602................................................
b181d57f
BF
2603
2604
2605In some cases it is possible that the new head will *not* actually be
2606a descendant of the old head. For example, the developer may have
2607realized she made a serious mistake, and decided to backtrack,
2608resulting in a situation like:
2609
1dc71a91 2610................................................
b181d57f
BF
2611 o--o--o--o--a--b <-- old head of the branch
2612 \
2613 o--o--o <-- new head of the branch
1dc71a91 2614................................................
b181d57f
BF
2615
2616In this case, "git fetch" will fail, and print out a warning.
2617
2618In that case, you can still force git to update to the new head, as
2619described in the following section. However, note that in the
2620situation above this may mean losing the commits labeled "a" and "b",
2621unless you've already created a reference of your own pointing to
2622them.
2623
e34caace 2624[[forcing-fetch]]
b181d57f
BF
2625Forcing git fetch to do non-fast-forward updates
2626------------------------------------------------
2627
2628If git fetch fails because the new head of a branch is not a
2629descendant of the old head, you may force the update with:
2630
2631-------------------------------------------------
2632$ git fetch git://example.com/proj.git +master:refs/remotes/example/master
2633-------------------------------------------------
2634
c64415e2
BF
2635Note the addition of the "+" sign. Alternatively, you can use the "-f"
2636flag to force updates of all the fetched branches, as in:
2637
2638-------------------------------------------------
2639$ git fetch -f origin
2640-------------------------------------------------
2641
2642Be aware that commits that the old version of example/master pointed at
2643may be lost, as we saw in the previous section.
b181d57f 2644
e34caace 2645[[remote-branch-configuration]]
b181d57f
BF
2646Configuring remote branches
2647---------------------------
2648
2649We saw above that "origin" is just a shortcut to refer to the
79c96c57 2650repository that you originally cloned from. This information is
b181d57f 2651stored in git configuration variables, which you can see using
9d13bda3 2652gitlink:git-config[1]:
b181d57f
BF
2653
2654-------------------------------------------------
9d13bda3 2655$ git config -l
b181d57f
BF
2656core.repositoryformatversion=0
2657core.filemode=true
2658core.logallrefupdates=true
2659remote.origin.url=git://git.kernel.org/pub/scm/git/git.git
2660remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
2661branch.master.remote=origin
2662branch.master.merge=refs/heads/master
2663-------------------------------------------------
2664
2665If there are other repositories that you also use frequently, you can
2666create similar configuration options to save typing; for example,
2667after
2668
2669-------------------------------------------------
9d13bda3 2670$ git config remote.example.url git://example.com/proj.git
b181d57f
BF
2671-------------------------------------------------
2672
2673then the following two commands will do the same thing:
2674
2675-------------------------------------------------
2676$ git fetch git://example.com/proj.git master:refs/remotes/example/master
2677$ git fetch example master:refs/remotes/example/master
2678-------------------------------------------------
2679
2680Even better, if you add one more option:
2681
2682-------------------------------------------------
9d13bda3 2683$ git config remote.example.fetch master:refs/remotes/example/master
b181d57f
BF
2684-------------------------------------------------
2685
2686then the following commands will all do the same thing:
2687
2688-------------------------------------------------
52c80037
BF
2689$ git fetch git://example.com/proj.git master:refs/remotes/example/master
2690$ git fetch example master:refs/remotes/example/master
b181d57f
BF
2691$ git fetch example
2692-------------------------------------------------
2693
2694You can also add a "+" to force the update each time:
2695
2696-------------------------------------------------
9d13bda3 2697$ git config remote.example.fetch +master:ref/remotes/example/master
b181d57f
BF
2698-------------------------------------------------
2699
2700Don't do this unless you're sure you won't mind "git fetch" possibly
2701throwing away commits on mybranch.
2702
2703Also note that all of the above configuration can be performed by
2704directly editing the file .git/config instead of using
9d13bda3 2705gitlink:git-config[1].
b181d57f 2706
9d13bda3 2707See gitlink:git-config[1] for more details on the configuration
b181d57f 2708options mentioned above.
d19fbc3c 2709
d19fbc3c 2710
036f8199
BF
2711[[git-concepts]]
2712Git concepts
2713============
d19fbc3c 2714
036f8199
BF
2715Git is built on a small number of simple but powerful ideas. While it
2716is possible to get things done without understanding them, you will find
2717git much more intuitive if you do.
2718
2719We start with the most important, the <<def_object_database,object
2720database>> and the <<def_index,index>>.
b181d57f 2721
e34caace 2722[[the-object-database]]
b181d57f
BF
2723The Object Database
2724-------------------
2725
1bbf1c79
BF
2726
2727We already saw in <<understanding-commits>> that all commits are stored
2728under a 40-digit "object name". In fact, all the information needed to
2729represent the history of a project is stored in objects with such names.
2730In each case the name is calculated by taking the SHA1 hash of the
2731contents of the object. The SHA1 hash is a cryptographic hash function.
2732What that means to us is that it is impossible to find two different
2733objects with the same name. This has a number of advantages; among
2734others:
2735
2736- Git can quickly determine whether two objects are identical or not,
2737 just by comparing names.
2738- Since object names are computed the same way in ever repository, the
2739 same content stored in two repositories will always be stored under
2740 the same name.
2741- Git can detect errors when it reads an object, by checking that the
2742 object's name is still the SHA1 hash of its contents.
2743
2744(See <<object-details>> for the details of the object formatting and
2745SHA1 calculation.)
2746
2747There are four different types of objects: "blob", "tree", "commit", and
2748"tag".
2749
2750- A <<def_blob_object,"blob" object>> is used to store file data.
2751- A <<def_tree_object,"tree" object>> is an object that ties one or more
2752 "blob" objects into a directory structure. In addition, a tree object
2753 can refer to other tree objects, thus creating a directory hierarchy.
2754- A <<def_commit_object,"commit" object>> ties such directory hierarchies
2755 together into a <<def_DAG,directed acyclic graph>> of revisions - each
2756 commit contains the object name of exactly one tree designating the
2757 directory hierarchy at the time of the commit. In addition, a commit
2758 refers to "parent" commit objects that describe the history of how we
2759 arrived at that directory hierarchy.
2760- A <<def_tag_object,"tag" object>> symbolically identifies and can be
2761 used to sign other objects. It contains the object name and type of
2762 another object, a symbolic name (of course!) and, optionally, a
2763 signature.
b181d57f 2764
b181d57f
BF
2765The object types in some more detail:
2766
513d419c
BF
2767[[commit-object]]
2768Commit Object
2769~~~~~~~~~~~~~
b181d57f 2770
1bbf1c79
BF
2771The "commit" object links a physical state of a tree with a description
2772of how we got there and why. Use the --pretty=raw option to
2773gitlink:git-show[1] or gitlink:git-log[1] to examine your favorite
2774commit:
2775
2776------------------------------------------------
2777$ git show -s --pretty=raw 2be7fcb476
2778commit 2be7fcb4764f2dbcee52635b91fedb1b3dcf7ab4
2779tree fb3a8bdd0ceddd019615af4d57a53f43d8cee2bf
2780parent 257a84d9d02e90447b149af58b271c19405edb6a
2781author Dave Watson <dwatson@mimvista.com> 1187576872 -0400
2782committer Junio C Hamano <gitster@pobox.com> 1187591163 -0700
2783
2784 Fix misspelling of 'suppress' in docs
2785
2786 Signed-off-by: Junio C Hamano <gitster@pobox.com>
2787------------------------------------------------
2788
2789As you can see, a commit is defined by:
2790
2791- a tree: The SHA1 name of a tree object (as defined below), representing
2792 the contents of a directory at a certain point in time.
2793- parent(s): The SHA1 name of some number of commits which represent the
2794 immediately prevoius step(s) in the history of the project. The
2795 example above has one parent; merge commits may have more than
2796 one. A commit with no parents is called a "root" commit, and
2797 represents the initial revision of a project. Each project must have
2798 at least one root. A project can also have multiple roots, though
2799 that isn't common (or necessarily a good idea).
2800- an author: The name of the person responsible for this change, together
2801 with its date.
2802- a committer: The name of the person who actually created the commit,
2803 with the date it was done. This may be different from the author, for
2804 example, if the author was someone who wrote a patch and emailed it
2805 to the person who used it to create the commit.
2806- a comment describing this commit.
2807
2808Note that a commit does not itself contain any information about what
2809actually changed; all changes are calculated by comparing the contents
2810of the tree referred to by this commit with the trees associated with
2811its parents. In particular, git does not attempt to record file renames
2812explicitly, though it can identify cases where the existence of the same
2813file data at changing paths suggests a rename. (See, for example, the
2814-M option to gitlink:git-diff[1]).
2815
2816A commit is usually created by gitlink:git-commit[1], which creates a
2817commit whose parent is normally the current HEAD, and whose tree is
2818taken from the content currently stored in the index.
b181d57f 2819
e34caace 2820[[tree-object]]
b181d57f 2821Tree Object
971aa71f 2822~~~~~~~~~~~
b181d57f 2823
1bbf1c79
BF
2824The ever-versatile gitlink:git-show[1] command can also be used to
2825examine tree objects, but gitlink:git-ls-tree[1] will give you more
2826details:
2827
2828------------------------------------------------
2829$ git ls-tree fb3a8bdd0ce
2830100644 blob 63c918c667fa005ff12ad89437f2fdc80926e21c .gitignore
2831100644 blob 5529b198e8d14decbe4ad99db3f7fb632de0439d .mailmap
2832100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3 COPYING
2833040000 tree 2fb783e477100ce076f6bf57e4a6f026013dc745 Documentation
2834100755 blob 3c0032cec592a765692234f1cba47dfdcc3a9200 GIT-VERSION-GEN
2835100644 blob 289b046a443c0647624607d471289b2c7dcd470b INSTALL
2836100644 blob 4eb463797adc693dc168b926b6932ff53f17d0b1 Makefile
2837100644 blob 548142c327a6790ff8821d67c2ee1eff7a656b52 README
2838...
2839------------------------------------------------
2840
2841As you can see, a tree object contains a list of entries, each with a
2842mode, object type, SHA1 name, and name, sorted by name. It represents
2843the contents of a single directory tree.
2844
2845The object type may be a blob, representing the contents of a file, or
2846another tree, representing the contents of a subdirectory. Since trees
2847and blobs, like all other objects, are named by the SHA1 hash of their
2848contents, two trees have the same SHA1 name if and only if their
2849contents (including, recursively, the contents of all subdirectories)
2850are identical. This allows git to quickly determine the differences
2851between two related tree objects, since it can ignore any entries with
2852identical object names.
2853
2854(Note: in the presence of submodules, trees may also have commits as
2855entries. See gitlink:git-submodule[1] and gitlink:gitmodules.txt[1]
2856for partial documentation.)
2857
2858Note that the files all have mode 644 or 755: git actually only pays
2859attention to the executable bit.
b181d57f 2860
513d419c
BF
2861[[blob-object]]
2862Blob Object
2863~~~~~~~~~~~
b181d57f 2864
1bbf1c79
BF
2865You can use gitlink:git-show[1] to examine the contents of a blob; take,
2866for example, the blob in the entry for "COPYING" from the tree above:
b181d57f 2867
1bbf1c79
BF
2868------------------------------------------------
2869$ git show 6ff87c4664
2870
2871 Note that the only valid version of the GPL as far as this project
2872 is concerned is _this_ particular version of the license (ie v2, not
2873 v2.2 or v3.x or whatever), unless explicitly otherwise stated.
2874...
2875------------------------------------------------
b181d57f 2876
1bbf1c79
BF
2877A "blob" object is nothing but a binary blob of data. It doesn't refer
2878to anything else or have attributes of any kind.
2879
2880Since the blob is entirely defined by its data, if two files in a
2881directory tree (or in multiple different versions of the repository)
2882have the same contents, they will share the same blob object. The object
2883is totally independent of its location in the directory tree, and
2884renaming a file does not change the object that file is associated with.
2885
2886Note that any tree or blob object can be examined using
2887gitlink:git-show[1] with the <revision>:<path> syntax. This can
2888sometimes be useful for browsing the contents of a tree that is not
2889currently checked out.
b181d57f 2890
e34caace 2891[[trust]]
b181d57f 2892Trust
971aa71f 2893~~~~~
b181d57f 2894
1bbf1c79
BF
2895If you receive the SHA1 name of a blob from one source, and its contents
2896from another (possibly untrusted) source, you can still trust that those
2897contents are correct as long as the SHA1 name agrees. This is because
2898the SHA1 is designed so that it is infeasible to find different contents
2899that produce the same hash.
b181d57f 2900
1bbf1c79
BF
2901Similarly, you need only trust the SHA1 name of a top-level tree object
2902to trust the contents of the entire directory that it refers to, and if
2903you receive the SHA1 name of a commit from a trusted source, then you
2904can easily verify the entire history of commits reachable through
2905parents of that commit, and all of those contents of the trees referred
2906to by those commits.
b181d57f
BF
2907
2908So to introduce some real trust in the system, the only thing you need
2909to do is to digitally sign just 'one' special note, which includes the
2910name of a top-level commit. Your digital signature shows others
2911that you trust that commit, and the immutability of the history of
2912commits tells others that they can trust the whole history.
2913
2914In other words, you can easily validate a whole archive by just
2915sending out a single email that tells the people the name (SHA1 hash)
2916of the top commit, and digitally sign that email using something
2917like GPG/PGP.
2918
2919To assist in this, git also provides the tag object...
2920
e34caace 2921[[tag-object]]
b181d57f 2922Tag Object
971aa71f 2923~~~~~~~~~~
b181d57f 2924
1bbf1c79
BF
2925A tag object contains an object, object type, tag name, the name of the
2926person ("tagger") who created the tag, and a message, which may contain
2927a signature, as can be seen using the gitlink:git-cat-file[1]:
b181d57f 2928
1bbf1c79
BF
2929------------------------------------------------
2930$ git cat-file tag v1.5.0
2931object 437b1b20df4b356c9342dac8d38849f24ef44f27
2932type commit
2933tag v1.5.0
2934tagger Junio C Hamano <junkio@cox.net> 1171411200 +0000
2935
2936GIT 1.5.0
2937-----BEGIN PGP SIGNATURE-----
2938Version: GnuPG v1.4.6 (GNU/Linux)
2939
2940iD8DBQBF0lGqwMbZpPMRm5oRAuRiAJ9ohBLd7s2kqjkKlq1qqC57SbnmzQCdG4ui
2941nLE/L9aUXdWeTFPron96DLA=
2942=2E+0
2943-----END PGP SIGNATURE-----
2944------------------------------------------------
b181d57f 2945
1bbf1c79
BF
2946See the gitlink:git-tag[1] command to learn how to create and verify tag
2947objects. (Note that gitlink:git-tag[1] can also be used to create
2948"lightweight tags", which are not tag objects at all, but just simple
2949references in .git/refs/tags/).
b181d57f 2950
09eff7b0
BF
2951[[pack-files]]
2952How git stores objects efficiently: pack files
2953~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2954
9644ffdd
BF
2955Newly created objects are initially created in a file named after the
2956object's SHA1 hash (stored in .git/objects).
09eff7b0
BF
2957
2958Unfortunately this system becomes inefficient once a project has a
2959lot of objects. Try this on an old project:
2960
2961------------------------------------------------
2962$ git count-objects
29636930 objects, 47620 kilobytes
2964------------------------------------------------
2965
2966The first number is the number of objects which are kept in
2967individual files. The second is the amount of space taken up by
2968those "loose" objects.
2969
2970You can save space and make git faster by moving these loose objects in
2971to a "pack file", which stores a group of objects in an efficient
2972compressed format; the details of how pack files are formatted can be
2973found in link:technical/pack-format.txt[technical/pack-format.txt].
2974
2975To put the loose objects into a pack, just run git repack:
2976
2977------------------------------------------------
2978$ git repack
2979Generating pack...
2980Done counting 6020 objects.
2981Deltifying 6020 objects.
2982 100% (6020/6020) done
2983Writing 6020 objects.
2984 100% (6020/6020) done
2985Total 6020, written 6020 (delta 4070), reused 0 (delta 0)
2986Pack pack-3e54ad29d5b2e05838c75df582c65257b8d08e1c created.
2987------------------------------------------------
2988
2989You can then run
2990
2991------------------------------------------------
2992$ git prune
2993------------------------------------------------
2994
2995to remove any of the "loose" objects that are now contained in the
2996pack. This will also remove any unreferenced objects (which may be
2997created when, for example, you use "git reset" to remove a commit).
2998You can verify that the loose objects are gone by looking at the
2999.git/objects directory or by running
3000
3001------------------------------------------------
3002$ git count-objects
30030 objects, 0 kilobytes
3004------------------------------------------------
3005
3006Although the object files are gone, any commands that refer to those
3007objects will work exactly as they did before.
3008
3009The gitlink:git-gc[1] command performs packing, pruning, and more for
3010you, so is normally the only high-level command you need.
3011
3012[[dangling-objects]]
3013Dangling objects
3014~~~~~~~~~~~~~~~~
3015
3016The gitlink:git-fsck[1] command will sometimes complain about dangling
3017objects. They are not a problem.
3018
3019The most common cause of dangling objects is that you've rebased a
3020branch, or you have pulled from somebody else who rebased a branch--see
3021<<cleaning-up-history>>. In that case, the old head of the original
3022branch still exists, as does everything it pointed to. The branch
3023pointer itself just doesn't, since you replaced it with another one.
3024
3025There are also other situations that cause dangling objects. For
3026example, a "dangling blob" may arise because you did a "git add" of a
3027file, but then, before you actually committed it and made it part of the
3028bigger picture, you changed something else in that file and committed
3029that *updated* thing - the old state that you added originally ends up
3030not being pointed to by any commit or tree, so it's now a dangling blob
3031object.
3032
3033Similarly, when the "recursive" merge strategy runs, and finds that
3034there are criss-cross merges and thus more than one merge base (which is
3035fairly unusual, but it does happen), it will generate one temporary
3036midway tree (or possibly even more, if you had lots of criss-crossing
3037merges and more than two merge bases) as a temporary internal merge
3038base, and again, those are real objects, but the end result will not end
3039up pointing to them, so they end up "dangling" in your repository.
3040
3041Generally, dangling objects aren't anything to worry about. They can
3042even be very useful: if you screw something up, the dangling objects can
3043be how you recover your old tree (say, you did a rebase, and realized
3044that you really didn't want to - you can look at what dangling objects
3045you have, and decide to reset your head to some old dangling state).
3046
3047For commits, you can just use:
3048
3049------------------------------------------------
3050$ gitk <dangling-commit-sha-goes-here> --not --all
3051------------------------------------------------
3052
3053This asks for all the history reachable from the given commit but not
3054from any branch, tag, or other reference. If you decide it's something
3055you want, you can always create a new reference to it, e.g.,
3056
3057------------------------------------------------
3058$ git branch recovered-branch <dangling-commit-sha-goes-here>
3059------------------------------------------------
3060
3061For blobs and trees, you can't do the same, but you can still examine
3062them. You can just do
3063
3064------------------------------------------------
3065$ git show <dangling-blob/tree-sha-goes-here>
3066------------------------------------------------
3067
3068to show what the contents of the blob were (or, for a tree, basically
3069what the "ls" for that directory was), and that may give you some idea
3070of what the operation was that left that dangling object.
3071
3072Usually, dangling blobs and trees aren't very interesting. They're
3073almost always the result of either being a half-way mergebase (the blob
3074will often even have the conflict markers from a merge in it, if you
3075have had conflicting merges that you fixed up by hand), or simply
3076because you interrupted a "git fetch" with ^C or something like that,
3077leaving _some_ of the new objects in the object database, but just
3078dangling and useless.
3079
3080Anyway, once you are sure that you're not interested in any dangling
3081state, you can just prune all unreachable objects:
3082
3083------------------------------------------------
3084$ git prune
3085------------------------------------------------
3086
3087and they'll be gone. But you should only run "git prune" on a quiescent
3088repository - it's kind of like doing a filesystem fsck recovery: you
3089don't want to do that while the filesystem is mounted.
3090
3091(The same is true of "git-fsck" itself, btw - but since
3092git-fsck never actually *changes* the repository, it just reports
3093on what it found, git-fsck itself is never "dangerous" to run.
3094Running it while somebody is actually changing the repository can cause
3095confusing and scary messages, but it won't actually do anything bad. In
3096contrast, running "git prune" while somebody is actively changing the
3097repository is a *BAD* idea).
b181d57f 3098
e34caace 3099[[the-index]]
1c097891
BF
3100The index
3101-----------
3102
3103The index is a binary file (generally kept in .git/index) containing a
3104sorted list of path names, each with permissions and the SHA1 of a blob
3105object; gitlink:git-ls-files[1] can show you the contents of the index:
b181d57f 3106
1c097891
BF
3107-------------------------------------------------
3108$ git ls-files --stage
3109100644 63c918c667fa005ff12ad89437f2fdc80926e21c 0 .gitignore
3110100644 5529b198e8d14decbe4ad99db3f7fb632de0439d 0 .mailmap
3111100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0 COPYING
3112100644 a37b2152bd26be2c2289e1f57a292534a51a93c7 0 Documentation/.gitignore
3113100644 fbefe9a45b00a54b58d94d06eca48b03d40a50e0 0 Documentation/Makefile
3114...
3115100644 2511aef8d89ab52be5ec6a5e46236b4b6bcd07ea 0 xdiff/xtypes.h
3116100644 2ade97b2574a9f77e7ae4002a4e07a6a38e46d07 0 xdiff/xutils.c
3117100644 d5de8292e05e7c36c4b68857c1cf9855e3d2f70a 0 xdiff/xutils.h
3118-------------------------------------------------
3119
3120Note that in older documentation you may see the index called the
3121"current directory cache" or just the "cache". It has three important
3122properties:
3123
31241. The index contains all the information necessary to generate a single
3125(uniquely determined) tree object.
3126+
3127For example, running gitlink:git-commit[1] generates this tree object
3128from the index, stores it in the object database, and uses it as the
3129tree object associated with the new commit.
3130
31312. The index enables fast comparisons between the tree object it defines
3132and the working tree.
3133+
3134It does this by storing some additional data for each entry (such as
3135the last modified time). This data is not displayed above, and is not
3136stored in the created tree object, but it can be used to determine
3137quickly which files in the working directory differ from what was
3138stored in the index, and thus save git from having to read all of the
3139data from such files to look for changes.
3140
31413. It can efficiently represent information about merge conflicts
3142between different tree objects, allowing each pathname to be
b181d57f 3143associated with sufficient information about the trees involved that
1c097891
BF
3144you can create a three-way merge between them.
3145+
3146We saw in <<conflict-resolution>> that during a merge the index can
3147store multiple versions of a single file (called "stages"). The third
3148column in the gitlink:git-ls-files[1] output above is the stage
3149number, and will take on values other than 0 for files with merge
3150conflicts.
3151
3152The index is thus a sort of temporary staging area, which is filled with
3153a tree which you are in the process of working on.
3154
3155If you blow the index away entirely, you generally haven't lost any
3156information as long as you have the name of the tree that it described.
b181d57f 3157
1c6045ff
BF
3158[[low-level-operations]]
3159Low-level git operations
3160========================
b181d57f 3161
1c6045ff
BF
3162Many of the higher-level commands were originally implemented as shell
3163scripts using a smaller core of low-level git commands. These can still
3164be useful when doing unusual things with git, or just as a way to
3165understand its inner workings.
b181d57f 3166
1bbf1c79
BF
3167[[object-manipulation]]
3168Object access and manipulation
3169------------------------------
3170
3171The gitlink:git-cat-file[1] command can show the contents of any object,
3172though the higher-level gitlink:git-show[1] is usually more useful.
3173
3174The gitlink:git-commit-tree[1] command allows constructing commits with
3175arbitrary parents and trees.
3176
3177A tree can be created with gitlink:git-write-tree[1] and its data can be
3178accessed by gitlink:git-ls-tree[1]. Two trees can be compared with
3179gitlink:git-diff-tree[1].
3180
3181A tag is created with gitlink:git-mktag[1], and the signature can be
3182verified by gitlink:git-verify-tag[1], though it is normally simpler to
3183use gitlink:git-tag[1] for both.
3184
e34caace 3185[[the-workflow]]
b181d57f
BF
3186The Workflow
3187------------
3188
1c6045ff
BF
3189High-level operations such as gitlink:git-commit[1],
3190gitlink:git-checkout[1] and git-reset[1] work by moving data between the
3191working tree, the index, and the object database. Git provides
3192low-level operations which perform each of these steps individually.
3193
b181d57f
BF
3194Generally, all "git" operations work on the index file. Some operations
3195work *purely* on the index file (showing the current state of the
1c6045ff
BF
3196index), but most operations move data between the index file and either
3197the database or the working directory. Thus there are four main
3198combinations:
b181d57f 3199
e34caace 3200[[working-directory-to-index]]
b181d57f
BF
3201working directory -> index
3202~~~~~~~~~~~~~~~~~~~~~~~~~~
3203
1c6045ff
BF
3204The gitlink:git-update-index[1] command updates the index with
3205information from the working directory. You generally update the
3206index information by just specifying the filename you want to update,
3207like so:
b181d57f
BF
3208
3209-------------------------------------------------
1c6045ff 3210$ git update-index filename
b181d57f
BF
3211-------------------------------------------------
3212
3213but to avoid common mistakes with filename globbing etc, the command
3214will not normally add totally new entries or remove old entries,
3215i.e. it will normally just update existing cache entries.
3216
3217To tell git that yes, you really do realize that certain files no
3218longer exist, or that new files should be added, you
3219should use the `--remove` and `--add` flags respectively.
3220
3221NOTE! A `--remove` flag does 'not' mean that subsequent filenames will
3222necessarily be removed: if the files still exist in your directory
3223structure, the index will be updated with their new status, not
3224removed. The only thing `--remove` means is that update-cache will be
3225considering a removed file to be a valid thing, and if the file really
3226does not exist any more, it will update the index accordingly.
3227
3228As a special case, you can also do `git-update-index --refresh`, which
3229will refresh the "stat" information of each index to match the current
3230stat information. It will 'not' update the object status itself, and
3231it will only update the fields that are used to quickly test whether
3232an object still matches its old backing store object.
3233
1c6045ff
BF
3234The previously introduced gitlink:git-add[1] is just a wrapper for
3235gitlink:git-update-index[1].
3236
e34caace 3237[[index-to-object-database]]
b181d57f
BF
3238index -> object database
3239~~~~~~~~~~~~~~~~~~~~~~~~
3240
3241You write your current index file to a "tree" object with the program
3242
3243-------------------------------------------------
1c6045ff 3244$ git write-tree
b181d57f
BF
3245-------------------------------------------------
3246
3247that doesn't come with any options - it will just write out the
3248current index into the set of tree objects that describe that state,
3249and it will return the name of the resulting top-level tree. You can
3250use that tree to re-generate the index at any time by going in the
3251other direction:
3252
e34caace 3253[[object-database-to-index]]
b181d57f
BF
3254object database -> index
3255~~~~~~~~~~~~~~~~~~~~~~~~
3256
3257You read a "tree" file from the object database, and use that to
3258populate (and overwrite - don't do this if your index contains any
3259unsaved state that you might want to restore later!) your current
3260index. Normal operation is just
3261
3262-------------------------------------------------
3263$ git-read-tree <sha1 of tree>
3264-------------------------------------------------
3265
3266and your index file will now be equivalent to the tree that you saved
3267earlier. However, that is only your 'index' file: your working
3268directory contents have not been modified.
3269
e34caace 3270[[index-to-working-directory]]
b181d57f
BF
3271index -> working directory
3272~~~~~~~~~~~~~~~~~~~~~~~~~~
3273
3274You update your working directory from the index by "checking out"
3275files. This is not a very common operation, since normally you'd just
3276keep your files updated, and rather than write to your working
3277directory, you'd tell the index files about the changes in your
3278working directory (i.e. `git-update-index`).
3279
3280However, if you decide to jump to a new version, or check out somebody
3281else's version, or just restore a previous tree, you'd populate your
3282index file with read-tree, and then you need to check out the result
3283with
3284
3285-------------------------------------------------
3286$ git-checkout-index filename
3287-------------------------------------------------
3288
3289or, if you want to check out all of the index, use `-a`.
3290
3291NOTE! git-checkout-index normally refuses to overwrite old files, so
3292if you have an old version of the tree already checked out, you will
3293need to use the "-f" flag ('before' the "-a" flag or the filename) to
3294'force' the checkout.
3295
3296
3297Finally, there are a few odds and ends which are not purely moving
3298from one representation to the other:
3299
e34caace 3300[[tying-it-all-together]]
b181d57f
BF
3301Tying it all together
3302~~~~~~~~~~~~~~~~~~~~~
3303
3304To commit a tree you have instantiated with "git-write-tree", you'd
3305create a "commit" object that refers to that tree and the history
3306behind it - most notably the "parent" commits that preceded it in
3307history.
3308
3309Normally a "commit" has one parent: the previous state of the tree
3310before a certain change was made. However, sometimes it can have two
3311or more parent commits, in which case we call it a "merge", due to the
3312fact that such a commit brings together ("merges") two or more
3313previous states represented by other commits.
3314
3315In other words, while a "tree" represents a particular directory state
3316of a working directory, a "commit" represents that state in "time",
3317and explains how we got there.
3318
3319You create a commit object by giving it the tree that describes the
3320state at the time of the commit, and a list of parents:
3321
3322-------------------------------------------------
3323$ git-commit-tree <tree> -p <parent> [-p <parent2> ..]
3324-------------------------------------------------
3325
3326and then giving the reason for the commit on stdin (either through
3327redirection from a pipe or file, or by just typing it at the tty).
3328
3329git-commit-tree will return the name of the object that represents
3330that commit, and you should save it away for later use. Normally,
3331you'd commit a new `HEAD` state, and while git doesn't care where you
3332save the note about that state, in practice we tend to just write the
3333result to the file pointed at by `.git/HEAD`, so that we can always see
3334what the last committed state was.
3335
3336Here is an ASCII art by Jon Loeliger that illustrates how
3337various pieces fit together.
3338
3339------------
3340
3341 commit-tree
3342 commit obj
3343 +----+
3344 | |
3345 | |
3346 V V
3347 +-----------+
3348 | Object DB |
3349 | Backing |
3350 | Store |
3351 +-----------+
3352 ^
3353 write-tree | |
3354 tree obj | |
3355 | | read-tree
3356 | | tree obj
3357 V
3358 +-----------+
3359 | Index |
3360 | "cache" |
3361 +-----------+
3362 update-index ^
3363 blob obj | |
3364 | |
3365 checkout-index -u | | checkout-index
3366 stat | | blob obj
3367 V
3368 +-----------+
3369 | Working |
3370 | Directory |
3371 +-----------+
3372
3373------------
3374
3375
e34caace 3376[[examining-the-data]]
b181d57f
BF
3377Examining the data
3378------------------
3379
3380You can examine the data represented in the object database and the
3381index with various helper tools. For every object, you can use
3382gitlink:git-cat-file[1] to examine details about the
3383object:
3384
3385-------------------------------------------------
3386$ git-cat-file -t <objectname>
3387-------------------------------------------------
3388
3389shows the type of the object, and once you have the type (which is
3390usually implicit in where you find the object), you can use
3391
3392-------------------------------------------------
3393$ git-cat-file blob|tree|commit|tag <objectname>
3394-------------------------------------------------
3395
3396to show its contents. NOTE! Trees have binary content, and as a result
3397there is a special helper for showing that content, called
3398`git-ls-tree`, which turns the binary content into a more easily
3399readable form.
3400
3401It's especially instructive to look at "commit" objects, since those
3402tend to be small and fairly self-explanatory. In particular, if you
3403follow the convention of having the top commit name in `.git/HEAD`,
3404you can do
3405
3406-------------------------------------------------
3407$ git-cat-file commit HEAD
3408-------------------------------------------------
3409
3410to see what the top commit was.
3411
e34caace 3412[[merging-multiple-trees]]
b181d57f 3413Merging multiple trees
d19fbc3c
BF
3414----------------------
3415
b181d57f
BF
3416Git helps you do a three-way merge, which you can expand to n-way by
3417repeating the merge procedure arbitrary times until you finally
3418"commit" the state. The normal situation is that you'd only do one
3419three-way merge (two parents), and commit it, but if you like to, you
3420can do multiple parents in one go.
3421
3422To do a three-way merge, you need the two sets of "commit" objects
3423that you want to merge, use those to find the closest common parent (a
3424third "commit" object), and then use those commit objects to find the
3425state of the directory ("tree" object) at these points.
3426
3427To get the "base" for the merge, you first look up the common parent
3428of two commits with
3429
3430-------------------------------------------------
3431$ git-merge-base <commit1> <commit2>
3432-------------------------------------------------
3433
3434which will return you the commit they are both based on. You should
3435now look up the "tree" objects of those commits, which you can easily
3436do with (for example)
3437
3438-------------------------------------------------
3439$ git-cat-file commit <commitname> | head -1
3440-------------------------------------------------
3441
3442since the tree object information is always the first line in a commit
3443object.
3444
1191ee18 3445Once you know the three trees you are going to merge (the one "original"
c64415e2 3446tree, aka the common tree, and the two "result" trees, aka the branches
1191ee18
BF
3447you want to merge), you do a "merge" read into the index. This will
3448complain if it has to throw away your old index contents, so you should
b181d57f 3449make sure that you've committed those - in fact you would normally
1191ee18
BF
3450always do a merge against your last commit (which should thus match what
3451you have in your current index anyway).
b181d57f
BF
3452
3453To do the merge, do
3454
3455-------------------------------------------------
3456$ git-read-tree -m -u <origtree> <yourtree> <targettree>
3457-------------------------------------------------
3458
3459which will do all trivial merge operations for you directly in the
3460index file, and you can just write the result out with
3461`git-write-tree`.
3462
3463
e34caace 3464[[merging-multiple-trees-2]]
b181d57f
BF
3465Merging multiple trees, continued
3466---------------------------------
3467
3468Sadly, many merges aren't trivial. If there are files that have
3469been added.moved or removed, or if both branches have modified the
3470same file, you will be left with an index tree that contains "merge
3471entries" in it. Such an index tree can 'NOT' be written out to a tree
3472object, and you will have to resolve any such merge clashes using
3473other tools before you can write out the result.
3474
3475You can examine such index state with `git-ls-files --unmerged`
3476command. An example:
3477
3478------------------------------------------------
3479$ git-read-tree -m $orig HEAD $target
3480$ git-ls-files --unmerged
3481100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1 hello.c
3482100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2 hello.c
3483100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello.c
3484------------------------------------------------
3485
3486Each line of the `git-ls-files --unmerged` output begins with
3487the blob mode bits, blob SHA1, 'stage number', and the
3488filename. The 'stage number' is git's way to say which tree it
3489came from: stage 1 corresponds to `$orig` tree, stage 2 `HEAD`
3490tree, and stage3 `$target` tree.
3491
3492Earlier we said that trivial merges are done inside
3493`git-read-tree -m`. For example, if the file did not change
3494from `$orig` to `HEAD` nor `$target`, or if the file changed
3495from `$orig` to `HEAD` and `$orig` to `$target` the same way,
3496obviously the final outcome is what is in `HEAD`. What the
3497above example shows is that file `hello.c` was changed from
3498`$orig` to `HEAD` and `$orig` to `$target` in a different way.
3499You could resolve this by running your favorite 3-way merge
c64415e2
BF
3500program, e.g. `diff3`, `merge`, or git's own merge-file, on
3501the blob objects from these three stages yourself, like this:
b181d57f
BF
3502
3503------------------------------------------------
3504$ git-cat-file blob 263414f... >hello.c~1
3505$ git-cat-file blob 06fa6a2... >hello.c~2
3506$ git-cat-file blob cc44c73... >hello.c~3
c64415e2 3507$ git merge-file hello.c~2 hello.c~1 hello.c~3
b181d57f
BF
3508------------------------------------------------
3509
3510This would leave the merge result in `hello.c~2` file, along
3511with conflict markers if there are conflicts. After verifying
3512the merge result makes sense, you can tell git what the final
3513merge result for this file is by:
3514
3515-------------------------------------------------
3516$ mv -f hello.c~2 hello.c
3517$ git-update-index hello.c
3518-------------------------------------------------
3519
3520When a path is in unmerged state, running `git-update-index` for
3521that path tells git to mark the path resolved.
3522
3523The above is the description of a git merge at the lowest level,
3524to help you understand what conceptually happens under the hood.
3525In practice, nobody, not even git itself, uses three `git-cat-file`
3526for this. There is `git-merge-index` program that extracts the
3527stages to temporary files and calls a "merge" script on it:
3528
3529-------------------------------------------------
3530$ git-merge-index git-merge-one-file hello.c
3531-------------------------------------------------
3532
207dfa07 3533and that is what higher level `git merge -s resolve` is implemented with.
b181d57f 3534
971aa71f
BF
3535[[hacking-git]]
3536Hacking git
3537===========
3538
3539This chapter covers internal details of the git implementation which