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