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