]>
Commit | Line | Data |
---|---|---|
b27a23e3 CC |
1 | gittutorial(7) |
2 | ============== | |
3 | ||
4 | NAME | |
5 | ---- | |
6 | gittutorial - A tutorial introduction to git (for version 1.5.1 or newer) | |
7 | ||
8 | SYNOPSIS | |
9 | -------- | |
10 | git * | |
11 | ||
12 | DESCRIPTION | |
13 | ----------- | |
8c7fa247 | 14 | |
927a503c BF |
15 | This tutorial explains how to import a new project into git, make |
16 | changes to it, and share changes with other developers. | |
8c7fa247 | 17 | |
cd50aba9 BF |
18 | If you are instead primarily interested in using git to fetch a project, |
19 | for example, to test the latest version, you may prefer to start with | |
20 | the first two chapters of link:user-manual.html[The Git User's Manual]. | |
21 | ||
46e56e81 | 22 | First, note that you can get documentation for a command such as |
483bc4f0 | 23 | `git log --graph` with: |
8c7fa247 | 24 | |
927a503c | 25 | ------------------------------------------------ |
3861cd55 | 26 | $ man git-log |
927a503c | 27 | ------------------------------------------------ |
8c7fa247 | 28 | |
6e702c24 CC |
29 | or: |
30 | ||
31 | ------------------------------------------------ | |
32 | $ git help log | |
33 | ------------------------------------------------ | |
34 | ||
35 | With the latter, you can use the manual viewer of your choice; see | |
36 | linkgit:git-help[1] for more information. | |
37 | ||
c14261ea NP |
38 | It is a good idea to introduce yourself to git with your name and |
39 | public email address before doing any operation. The easiest | |
40 | way to do so is: | |
66589230 JH |
41 | |
42 | ------------------------------------------------ | |
e0d10e1c TP |
43 | $ git config --global user.name "Your Name Comes Here" |
44 | $ git config --global user.email you@yourdomain.example.com | |
66589230 JH |
45 | ------------------------------------------------ |
46 | ||
47 | ||
927a503c BF |
48 | Importing a new project |
49 | ----------------------- | |
8c7fa247 | 50 | |
927a503c BF |
51 | Assume you have a tarball project.tar.gz with your initial work. You |
52 | can place it under git revision control as follows. | |
8c7fa247 | 53 | |
8db9307c | 54 | ------------------------------------------------ |
dcc6e28f | 55 | $ tar xzf project.tar.gz |
927a503c | 56 | $ cd project |
515377ea | 57 | $ git init |
8db9307c | 58 | ------------------------------------------------ |
8c7fa247 | 59 | |
927a503c | 60 | Git will reply |
8c7fa247 | 61 | |
927a503c | 62 | ------------------------------------------------ |
ef0a89a6 | 63 | Initialized empty Git repository in .git/ |
927a503c | 64 | ------------------------------------------------ |
8c7fa247 | 65 | |
927a503c | 66 | You've now initialized the working directory--you may notice a new |
93f9cc67 BF |
67 | directory created, named ".git". |
68 | ||
69 | Next, tell git to take a snapshot of the contents of all files under the | |
0b444cdb | 70 | current directory (note the '.'), with 'git add': |
8c7fa247 | 71 | |
8db9307c | 72 | ------------------------------------------------ |
927a503c | 73 | $ git add . |
8db9307c | 74 | ------------------------------------------------ |
8c7fa247 | 75 | |
93f9cc67 BF |
76 | This snapshot is now stored in a temporary staging area which git calls |
77 | the "index". You can permanently store the contents of the index in the | |
0b444cdb | 78 | repository with 'git commit': |
8c7fa247 | 79 | |
927a503c | 80 | ------------------------------------------------ |
66589230 | 81 | $ git commit |
927a503c | 82 | ------------------------------------------------ |
8c7fa247 | 83 | |
93f9cc67 BF |
84 | This will prompt you for a commit message. You've now stored the first |
85 | version of your project in git. | |
8c7fa247 | 86 | |
84dee6bb BF |
87 | Making changes |
88 | -------------- | |
89 | ||
93f9cc67 | 90 | Modify some files, then add their updated contents to the index: |
8c7fa247 | 91 | |
8db9307c | 92 | ------------------------------------------------ |
93f9cc67 | 93 | $ git add file1 file2 file3 |
8db9307c | 94 | ------------------------------------------------ |
8c7fa247 | 95 | |
93f9cc67 | 96 | You are now ready to commit. You can see what is about to be committed |
0b444cdb | 97 | using 'git diff' with the --cached option: |
93f9cc67 BF |
98 | |
99 | ------------------------------------------------ | |
100 | $ git diff --cached | |
101 | ------------------------------------------------ | |
102 | ||
0b444cdb | 103 | (Without --cached, 'git diff' will show you any changes that |
93f9cc67 | 104 | you've made but not yet added to the index.) You can also get a brief |
0b444cdb | 105 | summary of the situation with 'git status': |
93f9cc67 BF |
106 | |
107 | ------------------------------------------------ | |
108 | $ git status | |
109 | # On branch master | |
110 | # Changes to be committed: | |
111 | # (use "git reset HEAD <file>..." to unstage) | |
112 | # | |
113 | # modified: file1 | |
114 | # modified: file2 | |
115 | # modified: file3 | |
116 | # | |
117 | ------------------------------------------------ | |
118 | ||
119 | If you need to make any further adjustments, do so now, and then add any | |
120 | newly modified content to the index. Finally, commit your changes with: | |
8c7fa247 | 121 | |
927a503c | 122 | ------------------------------------------------ |
c1d179f8 | 123 | $ git commit |
927a503c | 124 | ------------------------------------------------ |
f2416c27 | 125 | |
2feaf4e9 | 126 | This will again prompt you for a message describing the change, and then |
93f9cc67 | 127 | record a new version of the project. |
84dee6bb | 128 | |
0b444cdb | 129 | Alternatively, instead of running 'git add' beforehand, you can use |
66589230 JH |
130 | |
131 | ------------------------------------------------ | |
132 | $ git commit -a | |
133 | ------------------------------------------------ | |
8c7fa247 | 134 | |
93f9cc67 BF |
135 | which will automatically notice any modified (but not new) files, add |
136 | them to the index, and commit, all in one step. | |
84dee6bb | 137 | |
927a503c BF |
138 | A note on commit messages: Though not required, it's a good idea to |
139 | begin the commit message with a single short (less than 50 character) | |
140 | line summarizing the change, followed by a blank line and then a more | |
141 | thorough description. Tools that turn commits into email, for | |
c1d179f8 | 142 | example, use the first line on the Subject: line and the rest of the |
927a503c | 143 | commit in the body. |
8c7fa247 | 144 | |
366bfcb6 NP |
145 | Git tracks content not files |
146 | ---------------------------- | |
147 | ||
483bc4f0 JN |
148 | Many revision control systems provide an `add` command that tells the |
149 | system to start tracking changes to a new file. Git's `add` command | |
0b444cdb | 150 | does something simpler and more powerful: 'git add' is used both for new |
93f9cc67 BF |
151 | and newly modified files, and in both cases it takes a snapshot of the |
152 | given files and stages that content in the index, ready for inclusion in | |
153 | the next commit. | |
8c7fa247 | 154 | |
23c9ccb2 BF |
155 | Viewing project history |
156 | ----------------------- | |
8c7fa247 | 157 | |
927a503c | 158 | At any point you can view the history of your changes using |
8c7fa247 | 159 | |
927a503c | 160 | ------------------------------------------------ |
67e6e5c4 | 161 | $ git log |
927a503c | 162 | ------------------------------------------------ |
8c7fa247 | 163 | |
927a503c | 164 | If you also want to see complete diffs at each step, use |
8c7fa247 | 165 | |
927a503c | 166 | ------------------------------------------------ |
67e6e5c4 | 167 | $ git log -p |
927a503c | 168 | ------------------------------------------------ |
8c7fa247 | 169 | |
c1d179f8 JH |
170 | Often the overview of the change is useful to get a feel of |
171 | each step | |
172 | ||
173 | ------------------------------------------------ | |
174 | $ git log --stat --summary | |
175 | ------------------------------------------------ | |
176 | ||
927a503c BF |
177 | Managing branches |
178 | ----------------- | |
2a29da7c | 179 | |
927a503c BF |
180 | A single git repository can maintain multiple branches of |
181 | development. To create a new branch named "experimental", use | |
8c7fa247 | 182 | |
927a503c BF |
183 | ------------------------------------------------ |
184 | $ git branch experimental | |
185 | ------------------------------------------------ | |
8c7fa247 | 186 | |
927a503c | 187 | If you now run |
8c7fa247 | 188 | |
927a503c BF |
189 | ------------------------------------------------ |
190 | $ git branch | |
191 | ------------------------------------------------ | |
8c7fa247 | 192 | |
927a503c | 193 | you'll get a list of all existing branches: |
8c7fa247 | 194 | |
8db9307c | 195 | ------------------------------------------------ |
927a503c BF |
196 | experimental |
197 | * master | |
8db9307c | 198 | ------------------------------------------------ |
8c7fa247 | 199 | |
927a503c BF |
200 | The "experimental" branch is the one you just created, and the |
201 | "master" branch is a default branch that was created for you | |
202 | automatically. The asterisk marks the branch you are currently on; | |
203 | type | |
8c7fa247 | 204 | |
927a503c BF |
205 | ------------------------------------------------ |
206 | $ git checkout experimental | |
207 | ------------------------------------------------ | |
8c7fa247 | 208 | |
927a503c BF |
209 | to switch to the experimental branch. Now edit a file, commit the |
210 | change, and switch back to the master branch: | |
8c7fa247 | 211 | |
927a503c BF |
212 | ------------------------------------------------ |
213 | (edit file) | |
214 | $ git commit -a | |
215 | $ git checkout master | |
216 | ------------------------------------------------ | |
8c7fa247 | 217 | |
927a503c BF |
218 | Check that the change you made is no longer visible, since it was |
219 | made on the experimental branch and you're back on the master branch. | |
8c7fa247 | 220 | |
927a503c | 221 | You can make a different change on the master branch: |
8c7fa247 | 222 | |
927a503c BF |
223 | ------------------------------------------------ |
224 | (edit file) | |
225 | $ git commit -a | |
226 | ------------------------------------------------ | |
8c7fa247 | 227 | |
927a503c | 228 | at this point the two branches have diverged, with different changes |
59427063 | 229 | made in each. To merge the changes made in experimental into master, run |
ed616049 | 230 | |
927a503c | 231 | ------------------------------------------------ |
c14261ea | 232 | $ git merge experimental |
927a503c BF |
233 | ------------------------------------------------ |
234 | ||
235 | If the changes don't conflict, you're done. If there are conflicts, | |
236 | markers will be left in the problematic files showing the conflict; | |
8c7fa247 | 237 | |
8db9307c | 238 | ------------------------------------------------ |
927a503c | 239 | $ git diff |
8db9307c | 240 | ------------------------------------------------ |
8c7fa247 | 241 | |
927a503c BF |
242 | will show this. Once you've edited the files to resolve the |
243 | conflicts, | |
8c7fa247 | 244 | |
8db9307c | 245 | ------------------------------------------------ |
927a503c | 246 | $ git commit -a |
8db9307c | 247 | ------------------------------------------------ |
8c7fa247 | 248 | |
927a503c | 249 | will commit the result of the merge. Finally, |
8c7fa247 | 250 | |
8db9307c | 251 | ------------------------------------------------ |
927a503c | 252 | $ gitk |
8db9307c | 253 | ------------------------------------------------ |
8c7fa247 | 254 | |
927a503c | 255 | will show a nice graphical representation of the resulting history. |
8c7fa247 | 256 | |
9c9410e1 SB |
257 | At this point you could delete the experimental branch with |
258 | ||
259 | ------------------------------------------------ | |
260 | $ git branch -d experimental | |
261 | ------------------------------------------------ | |
262 | ||
263 | This command ensures that the changes in the experimental branch are | |
264 | already in the current branch. | |
265 | ||
927a503c BF |
266 | If you develop on a branch crazy-idea, then regret it, you can always |
267 | delete the branch with | |
8c7fa247 | 268 | |
927a503c BF |
269 | ------------------------------------- |
270 | $ git branch -D crazy-idea | |
271 | ------------------------------------- | |
8c7fa247 | 272 | |
927a503c BF |
273 | Branches are cheap and easy, so this is a good way to try something |
274 | out. | |
8c7fa247 | 275 | |
927a503c BF |
276 | Using git for collaboration |
277 | --------------------------- | |
3eb5128a | 278 | |
927a503c BF |
279 | Suppose that Alice has started a new project with a git repository in |
280 | /home/alice/project, and that Bob, who has a home directory on the | |
281 | same machine, wants to contribute. | |
3eb5128a | 282 | |
927a503c | 283 | Bob begins with: |
3eb5128a | 284 | |
8db9307c | 285 | ------------------------------------------------ |
5d5e88af | 286 | bob$ git clone /home/alice/project myrepo |
8db9307c | 287 | ------------------------------------------------ |
3eb5128a | 288 | |
927a503c BF |
289 | This creates a new directory "myrepo" containing a clone of Alice's |
290 | repository. The clone is on an equal footing with the original | |
abda1ef5 | 291 | project, possessing its own copy of the original project's history. |
927a503c BF |
292 | |
293 | Bob then makes some changes and commits them: | |
ed616049 | 294 | |
927a503c BF |
295 | ------------------------------------------------ |
296 | (edit files) | |
5d5e88af | 297 | bob$ git commit -a |
927a503c BF |
298 | (repeat as necessary) |
299 | ------------------------------------------------ | |
ed616049 | 300 | |
927a503c BF |
301 | When he's ready, he tells Alice to pull changes from the repository |
302 | at /home/bob/myrepo. She does this with: | |
ed616049 | 303 | |
927a503c | 304 | ------------------------------------------------ |
5d5e88af IK |
305 | alice$ cd /home/alice/project |
306 | alice$ git pull /home/bob/myrepo master | |
927a503c | 307 | ------------------------------------------------ |
ed616049 | 308 | |
c1ff284a | 309 | This merges the changes from Bob's "master" branch into Alice's |
93ee7823 | 310 | current branch. If Alice has made her own changes in the meantime, |
c30e5673 | 311 | then she may need to manually fix any conflicts. |
2ae6c706 | 312 | |
93ee7823 BF |
313 | The "pull" command thus performs two operations: it fetches changes |
314 | from a remote branch, then merges them into the current branch. | |
2ae6c706 | 315 | |
dc29bc8b JH |
316 | Note that in general, Alice would want her local changes committed before |
317 | initiating this "pull". If Bob's work conflicts with what Alice did since | |
318 | their histories forked, Alice will use her working tree and the index to | |
319 | resolve conflicts, and existing local changes will interfere with the | |
320 | conflict resolution process (git will still perform the fetch but will | |
321 | refuse to merge --- Alice will have to get rid of her local changes in | |
322 | some way and pull again when this happens). | |
323 | ||
324 | Alice can peek at what Bob did without merging first, using the "fetch" | |
325 | command; this allows Alice to inspect what Bob did, using a special | |
326 | symbol "FETCH_HEAD", in order to determine if he has anything worth | |
327 | pulling, like this: | |
328 | ||
329 | ------------------------------------------------ | |
330 | alice$ git fetch /home/bob/myrepo master | |
53d1589f | 331 | alice$ git log -p HEAD..FETCH_HEAD |
dc29bc8b JH |
332 | ------------------------------------------------ |
333 | ||
334 | This operation is safe even if Alice has uncommitted local changes. | |
21d777f2 TLSC |
335 | The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable |
336 | from the FETCH_HEAD but exclude anything that is reachable from HEAD". | |
53d1589f | 337 | Alice already knows everything that leads to her current state (HEAD), |
21d777f2 TLSC |
338 | and reviews what Bob has in his state (FETCH_HEAD) that she has not |
339 | seen with this command. | |
53d1589f PC |
340 | |
341 | If Alice wants to visualize what Bob did since their histories forked | |
342 | she can issue the following command: | |
343 | ||
344 | ------------------------------------------------ | |
345 | $ gitk HEAD..FETCH_HEAD | |
346 | ------------------------------------------------ | |
347 | ||
348 | This uses the same two-dot range notation we saw earlier with 'git log'. | |
349 | ||
350 | Alice may want to view what both of them did since they forked. | |
351 | She can use three-dot form instead of the two-dot form: | |
352 | ||
353 | ------------------------------------------------ | |
354 | $ gitk HEAD...FETCH_HEAD | |
355 | ------------------------------------------------ | |
356 | ||
357 | This means "show everything that is reachable from either one, but | |
358 | exclude anything that is reachable from both of them". | |
359 | ||
360 | Please note that these range notation can be used with both gitk | |
361 | and "git log". | |
dc29bc8b JH |
362 | |
363 | After inspecting what Bob did, if there is nothing urgent, Alice may | |
364 | decide to continue working without pulling from Bob. If Bob's history | |
365 | does have something Alice would immediately need, Alice may choose to | |
366 | stash her work-in-progress first, do a "pull", and then finally unstash | |
367 | her work-in-progress on top of the resulting history. | |
368 | ||
c1ff284a JH |
369 | When you are working in a small closely knit group, it is not |
370 | unusual to interact with the same repository over and over | |
371 | again. By defining 'remote' repository shorthand, you can make | |
372 | it easier: | |
373 | ||
374 | ------------------------------------------------ | |
5d5e88af | 375 | alice$ git remote add bob /home/bob/myrepo |
c1ff284a JH |
376 | ------------------------------------------------ |
377 | ||
21d777f2 | 378 | With this, Alice can perform the first part of the "pull" operation |
0b444cdb | 379 | alone using the 'git fetch' command without merging them with her own |
21d777f2 | 380 | branch, using: |
2a29da7c | 381 | |
927a503c | 382 | ------------------------------------- |
5d5e88af | 383 | alice$ git fetch bob |
927a503c | 384 | ------------------------------------- |
2a29da7c | 385 | |
c1ff284a | 386 | Unlike the longhand form, when Alice fetches from Bob using a |
0b444cdb | 387 | remote repository shorthand set up with 'git remote', what was |
c1ff284a JH |
388 | fetched is stored in a remote tracking branch, in this case |
389 | `bob/master`. So after this: | |
a7333f9e | 390 | |
927a503c | 391 | ------------------------------------- |
5d5e88af | 392 | alice$ git log -p master..bob/master |
927a503c | 393 | ------------------------------------- |
a7333f9e | 394 | |
927a503c BF |
395 | shows a list of all the changes that Bob made since he branched from |
396 | Alice's master branch. | |
a7333f9e | 397 | |
c1ff284a | 398 | After examining those changes, Alice |
c14261ea | 399 | could merge the changes into her master branch: |
ed616049 | 400 | |
927a503c | 401 | ------------------------------------- |
5d5e88af | 402 | alice$ git merge bob/master |
927a503c | 403 | ------------------------------------- |
ed616049 | 404 | |
c1ff284a JH |
405 | This `merge` can also be done by 'pulling from her own remote |
406 | tracking branch', like this: | |
93ee7823 BF |
407 | |
408 | ------------------------------------- | |
5d5e88af | 409 | alice$ git pull . remotes/bob/master |
93ee7823 BF |
410 | ------------------------------------- |
411 | ||
c1ff284a | 412 | Note that git pull always merges into the current branch, |
02783075 | 413 | regardless of what else is given on the command line. |
93ee7823 | 414 | |
927a503c | 415 | Later, Bob can update his repo with Alice's latest changes using |
ed616049 | 416 | |
927a503c | 417 | ------------------------------------- |
5d5e88af | 418 | bob$ git pull |
927a503c | 419 | ------------------------------------- |
ed616049 | 420 | |
927a503c BF |
421 | Note that he doesn't need to give the path to Alice's repository; |
422 | when Bob cloned Alice's repository, git stored the location of her | |
d66409f0 BF |
423 | repository in the repository configuration, and that location is |
424 | used for pulls: | |
2a29da7c | 425 | |
927a503c | 426 | ------------------------------------- |
5d5e88af | 427 | bob$ git config --get remote.origin.url |
8960b5a7 | 428 | /home/alice/project |
927a503c | 429 | ------------------------------------- |
2a29da7c | 430 | |
0b444cdb | 431 | (The complete configuration created by 'git clone' is visible using |
483bc4f0 | 432 | `git config -l`, and the linkgit:git-config[1] man page |
d66409f0 BF |
433 | explains the meaning of each option.) |
434 | ||
435 | Git also keeps a pristine copy of Alice's master branch under the | |
436 | name "origin/master": | |
437 | ||
438 | ------------------------------------- | |
5d5e88af | 439 | bob$ git branch -r |
d66409f0 BF |
440 | origin/master |
441 | ------------------------------------- | |
2a29da7c | 442 | |
927a503c BF |
443 | If Bob later decides to work from a different host, he can still |
444 | perform clones and pulls using the ssh protocol: | |
2a29da7c | 445 | |
927a503c | 446 | ------------------------------------- |
5d5e88af | 447 | bob$ git clone alice.org:/home/alice/project myrepo |
927a503c | 448 | ------------------------------------- |
2a29da7c | 449 | |
927a503c | 450 | Alternatively, git has a native protocol, or can use rsync or http; |
5162e697 | 451 | see linkgit:git-pull[1] for details. |
0c04094b | 452 | |
927a503c | 453 | Git can also be used in a CVS-like mode, with a central repository |
5162e697 | 454 | that various users push changes to; see linkgit:git-push[1] and |
6998e4db | 455 | linkgit:gitcvs-migration[7]. |
0c04094b | 456 | |
f1fe3846 BF |
457 | Exploring history |
458 | ----------------- | |
0c04094b | 459 | |
f1fe3846 | 460 | Git history is represented as a series of interrelated commits. We |
0b444cdb | 461 | have already seen that the 'git log' command can list those commits. |
f1fe3846 BF |
462 | Note that first line of each git log entry also gives a name for the |
463 | commit: | |
c9517341 | 464 | |
927a503c | 465 | ------------------------------------- |
f1fe3846 BF |
466 | $ git log |
467 | commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7 | |
468 | Author: Junio C Hamano <junkio@cox.net> | |
469 | Date: Tue May 16 17:18:22 2006 -0700 | |
470 | ||
471 | merge-base: Clarify the comments on post processing. | |
927a503c | 472 | ------------------------------------- |
0c04094b | 473 | |
0b444cdb | 474 | We can give this name to 'git show' to see the details about this |
f1fe3846 | 475 | commit. |
0c04094b | 476 | |
927a503c | 477 | ------------------------------------- |
f1fe3846 | 478 | $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7 |
927a503c | 479 | ------------------------------------- |
0c04094b | 480 | |
c1d179f8 | 481 | But there are other ways to refer to commits. You can use any initial |
f1fe3846 | 482 | part of the name that is long enough to uniquely identify the commit: |
0c04094b | 483 | |
f1fe3846 BF |
484 | ------------------------------------- |
485 | $ git show c82a22c39c # the first few characters of the name are | |
486 | # usually enough | |
487 | $ git show HEAD # the tip of the current branch | |
488 | $ git show experimental # the tip of the "experimental" branch | |
489 | ------------------------------------- | |
490 | ||
9c9410e1 SB |
491 | Every commit usually has one "parent" commit |
492 | which points to the previous state of the project: | |
0c04094b | 493 | |
927a503c | 494 | ------------------------------------- |
f1fe3846 BF |
495 | $ git show HEAD^ # to see the parent of HEAD |
496 | $ git show HEAD^^ # to see the grandparent of HEAD | |
497 | $ git show HEAD~4 # to see the great-great grandparent of HEAD | |
927a503c | 498 | ------------------------------------- |
e7c1ca42 | 499 | |
f1fe3846 BF |
500 | Note that merge commits may have more than one parent: |
501 | ||
502 | ------------------------------------- | |
503 | $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^) | |
504 | $ git show HEAD^2 # show the second parent of HEAD | |
505 | ------------------------------------- | |
44760f1d | 506 | |
f1fe3846 | 507 | You can also give commits names of your own; after running |
3eb5128a | 508 | |
927a503c | 509 | ------------------------------------- |
b1889c36 | 510 | $ git tag v2.5 1b2e1d63ff |
927a503c | 511 | ------------------------------------- |
3eb5128a | 512 | |
f1fe3846 BF |
513 | you can refer to 1b2e1d63ff by the name "v2.5". If you intend to |
514 | share this name with other people (for example, to identify a release | |
515 | version), you should create a "tag" object, and perhaps sign it; see | |
5162e697 | 516 | linkgit:git-tag[1] for details. |
f1fe3846 BF |
517 | |
518 | Any git command that needs to know a commit can take any of these | |
519 | names. For example: | |
3eb5128a | 520 | |
927a503c | 521 | ------------------------------------- |
f1fe3846 BF |
522 | $ git diff v2.5 HEAD # compare the current HEAD to v2.5 |
523 | $ git branch stable v2.5 # start a new branch named "stable" based | |
524 | # at v2.5 | |
525 | $ git reset --hard HEAD^ # reset your current branch and working | |
37425065 | 526 | # directory to its state at HEAD^ |
927a503c | 527 | ------------------------------------- |
c9517341 | 528 | |
f1fe3846 BF |
529 | Be careful with that last command: in addition to losing any changes |
530 | in the working directory, it will also remove all later commits from | |
531 | this branch. If this branch is the only branch containing those | |
0b444cdb | 532 | commits, they will be lost. Also, don't use 'git reset' on a |
a9d1836b JH |
533 | publicly-visible branch that other developers pull from, as it will |
534 | force needless merges on other developers to clean up the history. | |
0b444cdb | 535 | If you need to undo changes that you have pushed, use 'git revert' |
6e2e1cfb | 536 | instead. |
c9517341 | 537 | |
0b444cdb | 538 | The 'git grep' command can search for strings in any version of your |
f1fe3846 | 539 | project, so |
c9517341 | 540 | |
927a503c | 541 | ------------------------------------- |
f1fe3846 | 542 | $ git grep "hello" v2.5 |
927a503c | 543 | ------------------------------------- |
c9517341 | 544 | |
abda1ef5 | 545 | searches for all occurrences of "hello" in v2.5. |
2a29da7c | 546 | |
0b444cdb | 547 | If you leave out the commit name, 'git grep' will search any of the |
f1fe3846 | 548 | files it manages in your current directory. So |
2a29da7c | 549 | |
927a503c | 550 | ------------------------------------- |
f1fe3846 | 551 | $ git grep "hello" |
dc5f9239 JH |
552 | ------------------------------------- |
553 | ||
f1fe3846 | 554 | is a quick way to search just the files that are tracked by git. |
927a503c | 555 | |
f1fe3846 | 556 | Many git commands also take sets of commits, which can be specified |
0b444cdb | 557 | in a number of ways. Here are some examples with 'git log': |
927a503c BF |
558 | |
559 | ------------------------------------- | |
f1fe3846 BF |
560 | $ git log v2.5..v2.6 # commits between v2.5 and v2.6 |
561 | $ git log v2.5.. # commits since v2.5 | |
562 | $ git log --since="2 weeks ago" # commits from the last 2 weeks | |
563 | $ git log v2.5.. Makefile # commits since v2.5 which modify | |
564 | # Makefile | |
927a503c | 565 | ------------------------------------- |
6f60300b | 566 | |
0b444cdb | 567 | You can also give 'git log' a "range" of commits where the first is not |
f1fe3846 | 568 | necessarily an ancestor of the second; for example, if the tips of |
21d777f2 | 569 | the branches "stable" and "master" diverged from a common |
f1fe3846 BF |
570 | commit some time ago, then |
571 | ||
572 | ------------------------------------- | |
21d777f2 | 573 | $ git log stable..master |
f1fe3846 BF |
574 | ------------------------------------- |
575 | ||
21d777f2 | 576 | will list commits made in the master branch but not in the |
f1fe3846 BF |
577 | stable branch, while |
578 | ||
579 | ------------------------------------- | |
21d777f2 | 580 | $ git log master..stable |
f1fe3846 BF |
581 | ------------------------------------- |
582 | ||
583 | will show the list of commits made on the stable branch but not | |
21d777f2 | 584 | the master branch. |
f1fe3846 | 585 | |
0b444cdb | 586 | The 'git log' command has a weakness: it must present commits in a |
f1fe3846 | 587 | list. When the history has lines of development that diverged and |
0b444cdb | 588 | then merged back together, the order in which 'git log' presents |
f1fe3846 BF |
589 | those commits is meaningless. |
590 | ||
c7719fbe | 591 | Most projects with multiple contributors (such as the Linux kernel, |
42d36bb8 | 592 | or git itself) have frequent merges, and 'gitk' does a better job of |
f1fe3846 BF |
593 | visualizing their history. For example, |
594 | ||
595 | ------------------------------------- | |
596 | $ gitk --since="2 weeks ago" drivers/ | |
597 | ------------------------------------- | |
598 | ||
599 | allows you to browse any commits from the last 2 weeks of commits | |
2be1bc48 BF |
600 | that modified files under the "drivers" directory. (Note: you can |
601 | adjust gitk's fonts by holding down the control key while pressing | |
602 | "-" or "+".) | |
f1fe3846 BF |
603 | |
604 | Finally, most commands that take filenames will optionally allow you | |
605 | to precede any filename by a commit, to specify a particular version | |
38573864 | 606 | of the file: |
f1fe3846 BF |
607 | |
608 | ------------------------------------- | |
609 | $ git diff v2.5:Makefile HEAD:Makefile.in | |
610 | ------------------------------------- | |
927a503c | 611 | |
0b444cdb | 612 | You can also use 'git show' to see any such file: |
38573864 BF |
613 | |
614 | ------------------------------------- | |
9c9410e1 | 615 | $ git show v2.5:Makefile |
38573864 BF |
616 | ------------------------------------- |
617 | ||
927a503c BF |
618 | Next Steps |
619 | ---------- | |
620 | ||
e31952da BF |
621 | This tutorial should be enough to perform basic distributed revision |
622 | control for your projects. However, to fully understand the depth | |
623 | and power of git you need to understand two simple ideas on which it | |
624 | is based: | |
625 | ||
626 | * The object database is the rather elegant system used to | |
627 | store the history of your project--files, directories, and | |
628 | commits. | |
629 | ||
630 | * The index file is a cache of the state of a directory tree, | |
631 | used to create commits, check out working directories, and | |
632 | hold the various trees involved in a merge. | |
633 | ||
6998e4db | 634 | Part two of this tutorial explains the object |
e31952da | 635 | database, the index file, and a few other odds and ends that you'll |
6998e4db | 636 | need to make the most of git. You can find it at linkgit:gittutorial-2[7]. |
e31952da | 637 | |
cd50aba9 | 638 | If you don't want to continue with that right away, a few other |
e31952da | 639 | digressions that may be interesting at this point are: |
927a503c | 640 | |
5162e697 | 641 | * linkgit:git-format-patch[1], linkgit:git-am[1]: These convert |
927a503c | 642 | series of git commits into emailed patches, and vice versa, |
c7719fbe | 643 | useful for projects such as the Linux kernel which rely heavily |
927a503c BF |
644 | on emailed patches. |
645 | ||
5162e697 | 646 | * linkgit:git-bisect[1]: When there is a regression in your |
927a503c BF |
647 | project, one way to track down the bug is by searching through |
648 | the history to find the exact commit that's to blame. Git bisect | |
649 | can help you perform a binary search for that commit. It is | |
650 | smart enough to perform a close-to-optimal search even in the | |
651 | case of complex non-linear history with lots of merged branches. | |
652 | ||
801a011d TR |
653 | * linkgit:gitworkflows[7]: Gives an overview of recommended |
654 | workflows. | |
655 | ||
abda1ef5 | 656 | * link:everyday.html[Everyday GIT with 20 Commands Or So] |
e31952da | 657 | |
6998e4db | 658 | * linkgit:gitcvs-migration[7]: Git for CVS users. |
b27a23e3 CC |
659 | |
660 | SEE ALSO | |
661 | -------- | |
662 | linkgit:gittutorial-2[7], | |
663 | linkgit:gitcvs-migration[7], | |
497c8331 CC |
664 | linkgit:gitcore-tutorial[7], |
665 | linkgit:gitglossary[7], | |
6e702c24 | 666 | linkgit:git-help[1], |
801a011d | 667 | linkgit:gitworkflows[7], |
b27a23e3 CC |
668 | link:everyday.html[Everyday git], |
669 | link:user-manual.html[The Git User's Manual] | |
670 | ||
671 | GIT | |
672 | --- | |
9e1f0a85 | 673 | Part of the linkgit:git[1] suite. |