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