blob: 59ef5cef1f080d079109c50f6034ff88901c658f [file] [log] [blame]
Junio C Hamanodfccbb02008-05-26 01:16:141gittutorial(7)
2==============
3
4NAME
5----
Junio C Hamano19a0f4e2014-11-18 21:46:366gittutorial - A tutorial introduction to Git
Junio C Hamanodfccbb02008-05-26 01:16:147
8SYNOPSIS
9--------
Junio C Hamano15567bc2011-07-23 00:51:5910[verse]
Junio C Hamanodfccbb02008-05-26 01:16:1411git *
12
13DESCRIPTION
14-----------
Junio C Hamano1a4e8412005-12-27 08:17:2315
Junio C Hamano076ffcc2013-02-06 05:13:2116This tutorial explains how to import a new project into Git, make
Junio C Hamanoc2b0a492006-01-23 07:54:3617changes to it, and share changes with other developers.
Junio C Hamano1a4e8412005-12-27 08:17:2318
Junio C Hamano076ffcc2013-02-06 05:13:2119If you are instead primarily interested in using Git to fetch a project,
Junio C Hamanoed7f4f62007-05-20 09:09:0920for example, to test the latest version, you may prefer to start with
21the first two chapters of link:user-manual.html[The Git User's Manual].
22
Junio C Hamanofce7c7e2008-07-02 03:06:3823First, note that you can get documentation for a command such as
24`git log --graph` with:
Junio C Hamano1a4e8412005-12-27 08:17:2325
26------------------------------------------------
Junio C Hamanofce7c7e2008-07-02 03:06:3827$ man git-log
Junio C Hamano1a4e8412005-12-27 08:17:2328------------------------------------------------
29
Junio C Hamanof66ecee2008-11-17 18:25:4330or:
31
32------------------------------------------------
33$ git help log
34------------------------------------------------
35
36With the latter, you can use the manual viewer of your choice; see
37linkgit:git-help[1] for more information.
38
Junio C Hamano076ffcc2013-02-06 05:13:2139It is a good idea to introduce yourself to Git with your name and
Junio C Hamanoedd2b0a2007-01-15 06:12:4540public email address before doing any operation. The easiest
41way to do so is:
Junio C Hamano699660b2006-11-29 20:40:1042
43------------------------------------------------
Junio C Hamano7ad22dc2007-01-29 02:55:4844$ git config --global user.name "Your Name Comes Here"
45$ git config --global user.email you@yourdomain.example.com
Junio C Hamano699660b2006-11-29 20:40:1046------------------------------------------------
47
48
Junio C Hamanoc2b0a492006-01-23 07:54:3649Importing a new project
Junio C Hamano1a4e8412005-12-27 08:17:2350-----------------------
51
Junio C Hamanoc2b0a492006-01-23 07:54:3652Assume you have a tarball project.tar.gz with your initial work. You
Junio C Hamano076ffcc2013-02-06 05:13:2153can place it under Git revision control as follows.
Junio C Hamano1a4e8412005-12-27 08:17:2354
Junio C Hamano1a4e8412005-12-27 08:17:2355------------------------------------------------
Junio C Hamanoc2b0a492006-01-23 07:54:3656$ tar xzf project.tar.gz
57$ cd project
Junio C Hamanofc4d38c2007-01-08 06:53:3258$ git init
Junio C Hamano1a4e8412005-12-27 08:17:2359------------------------------------------------
60
Junio C Hamanoc2b0a492006-01-23 07:54:3661Git will reply
Junio C Hamano1a4e8412005-12-27 08:17:2362
Junio C Hamano1a4e8412005-12-27 08:17:2363------------------------------------------------
Junio C Hamano7d23f5e2006-12-16 07:44:0464Initialized empty Git repository in .git/
Junio C Hamano1a4e8412005-12-27 08:17:2365------------------------------------------------
Junio C Hamano1a4e8412005-12-27 08:17:2366
Junio C Hamanoc2b0a492006-01-23 07:54:3667You've now initialized the working directory--you may notice a new
Junio C Hamanoed7f4f62007-05-20 09:09:0968directory created, named ".git".
69
Junio C Hamano076ffcc2013-02-06 05:13:2170Next, tell Git to take a snapshot of the contents of all files under the
Junio C Hamano1aa40d22010-01-21 17:46:4371current directory (note the '.'), with 'git add':
Junio C Hamano1a4e8412005-12-27 08:17:2372
Junio C Hamanoc2b0a492006-01-23 07:54:3673------------------------------------------------
74$ git add .
75------------------------------------------------
76
Junio C Hamano076ffcc2013-02-06 05:13:2177This snapshot is now stored in a temporary staging area which Git calls
Junio C Hamanoed7f4f62007-05-20 09:09:0978the "index". You can permanently store the contents of the index in the
Junio C Hamano1aa40d22010-01-21 17:46:4379repository with 'git commit':
Junio C Hamanoc2b0a492006-01-23 07:54:3680
81------------------------------------------------
Junio C Hamano699660b2006-11-29 20:40:1082$ git commit
Junio C Hamanoc2b0a492006-01-23 07:54:3683------------------------------------------------
84
Junio C Hamanoed7f4f62007-05-20 09:09:0985This will prompt you for a commit message. You've now stored the first
Junio C Hamano076ffcc2013-02-06 05:13:2186version of your project in Git.
Junio C Hamanoc2b0a492006-01-23 07:54:3687
Junio C Hamano79770b62007-01-07 07:43:5888Making changes
89--------------
90
Junio C Hamanoed7f4f62007-05-20 09:09:0991Modify some files, then add their updated contents to the index:
Junio C Hamanoc2b0a492006-01-23 07:54:3692
93------------------------------------------------
Junio C Hamano79770b62007-01-07 07:43:5894$ git add file1 file2 file3
Junio C Hamanoed7f4f62007-05-20 09:09:0995------------------------------------------------
96
97You are now ready to commit. You can see what is about to be committed
Junio C Hamano1aa40d22010-01-21 17:46:4398using 'git diff' with the --cached option:
Junio C Hamanoed7f4f62007-05-20 09:09:0999
100------------------------------------------------
101$ git diff --cached
102------------------------------------------------
103
Junio C Hamano1aa40d22010-01-21 17:46:43104(Without --cached, 'git diff' will show you any changes that
Junio C Hamanoed7f4f62007-05-20 09:09:09105you've made but not yet added to the index.) You can also get a brief
Junio C Hamano1aa40d22010-01-21 17:46:43106summary of the situation with 'git status':
Junio C Hamanoed7f4f62007-05-20 09:09:09107
108------------------------------------------------
109$ git status
Junio C Hamano13220242014-11-19 23:06:27110On branch master
111Changes to be committed:
Junio C Hamano88bf5712017-09-10 08:39:23112Your branch is up to date with 'origin/master'.
Junio C Hamanoc9f11c22019-07-10 02:54:04113 (use "git restore --staged <file>..." to unstage)
Junio C Hamano13220242014-11-19 23:06:27114
115modified: file1
116modified: file2
117modified: file3
118
Junio C Hamanoed7f4f62007-05-20 09:09:09119------------------------------------------------
120
121If you need to make any further adjustments, do so now, and then add any
122newly modified content to the index. Finally, commit your changes with:
123
124------------------------------------------------
Junio C Hamanoeb692952007-01-03 22:02:12125$ git commit
Junio C Hamanoc2b0a492006-01-23 07:54:36126------------------------------------------------
127
Junio C Hamano6fb124c2008-06-13 10:04:01128This will again prompt you for a message describing the change, and then
Junio C Hamanoed7f4f62007-05-20 09:09:09129record a new version of the project.
Junio C Hamano79770b62007-01-07 07:43:58130
Junio C Hamano1aa40d22010-01-21 17:46:43131Alternatively, instead of running 'git add' beforehand, you can use
Junio C Hamano699660b2006-11-29 20:40:10132
133------------------------------------------------
134$ git commit -a
135------------------------------------------------
Junio C Hamanoc2b0a492006-01-23 07:54:36136
Junio C Hamanoed7f4f62007-05-20 09:09:09137which will automatically notice any modified (but not new) files, add
138them to the index, and commit, all in one step.
Junio C Hamano79770b62007-01-07 07:43:58139
Junio C Hamanoc2b0a492006-01-23 07:54:36140A note on commit messages: Though not required, it's a good idea to
141begin the commit message with a single short (less than 50 character)
142line summarizing the change, followed by a blank line and then a more
Junio C Hamano281fd392012-09-17 23:57:41143thorough description. The text up to the first blank line in a commit
144message is treated as the commit title, and that title is used
Junio C Hamano076ffcc2013-02-06 05:13:21145throughout Git. For example, linkgit:git-format-patch[1] turns a
Junio C Hamano281fd392012-09-17 23:57:41146commit into email, and it uses the title on the Subject line and the
147rest of the commit in the body.
Junio C Hamanoc2b0a492006-01-23 07:54:36148
Junio C Hamanoe7935c42006-12-13 21:32:17149Git tracks content not files
150----------------------------
Junio C Hamanoc2b0a492006-01-23 07:54:36151
Junio C Hamanofce7c7e2008-07-02 03:06:38152Many revision control systems provide an `add` command that tells the
153system to start tracking changes to a new file. Git's `add` command
Junio C Hamano1aa40d22010-01-21 17:46:43154does something simpler and more powerful: 'git add' is used both for new
Junio C Hamanoed7f4f62007-05-20 09:09:09155and newly modified files, and in both cases it takes a snapshot of the
156given files and stages that content in the index, ready for inclusion in
157the next commit.
Junio C Hamanoe7935c42006-12-13 21:32:17158
Junio C Hamanof614c642007-06-11 01:21:54159Viewing project history
160-----------------------
Junio C Hamanoc2b0a492006-01-23 07:54:36161
162At any point you can view the history of your changes using
163
164------------------------------------------------
Junio C Hamano6f8a7902006-05-22 01:10:13165$ git log
Junio C Hamanoc2b0a492006-01-23 07:54:36166------------------------------------------------
167
168If you also want to see complete diffs at each step, use
169
170------------------------------------------------
Junio C Hamano6f8a7902006-05-22 01:10:13171$ git log -p
Junio C Hamanoc2b0a492006-01-23 07:54:36172------------------------------------------------
173
Junio C Hamanoeb692952007-01-03 22:02:12174Often the overview of the change is useful to get a feel of
175each step
176
177------------------------------------------------
178$ git log --stat --summary
179------------------------------------------------
180
Junio C Hamanoc2b0a492006-01-23 07:54:36181Managing branches
182-----------------
183
Junio C Hamano076ffcc2013-02-06 05:13:21184A single Git repository can maintain multiple branches of
Junio C Hamanoc2b0a492006-01-23 07:54:36185development. To create a new branch named "experimental", use
186
187------------------------------------------------
188$ git branch experimental
189------------------------------------------------
190
191If you now run
192
193------------------------------------------------
194$ git branch
195------------------------------------------------
196
197you'll get a list of all existing branches:
198
199------------------------------------------------
200 experimental
201* master
202------------------------------------------------
203
204The "experimental" branch is the one you just created, and the
205"master" branch is a default branch that was created for you
206automatically. The asterisk marks the branch you are currently on;
207type
208
209------------------------------------------------
Junio C Hamanoc9f11c22019-07-10 02:54:04210$ git switch experimental
Junio C Hamanoc2b0a492006-01-23 07:54:36211------------------------------------------------
212
213to switch to the experimental branch. Now edit a file, commit the
214change, and switch back to the master branch:
215
216------------------------------------------------
217(edit file)
218$ git commit -a
Junio C Hamanoc9f11c22019-07-10 02:54:04219$ git switch master
Junio C Hamanoc2b0a492006-01-23 07:54:36220------------------------------------------------
221
222Check that the change you made is no longer visible, since it was
223made on the experimental branch and you're back on the master branch.
224
225You can make a different change on the master branch:
226
227------------------------------------------------
228(edit file)
229$ git commit -a
230------------------------------------------------
231
232at this point the two branches have diverged, with different changes
Junio C Hamano0df34342006-11-22 08:28:50233made in each. To merge the changes made in experimental into master, run
Junio C Hamanoc2b0a492006-01-23 07:54:36234
235------------------------------------------------
Junio C Hamanoedd2b0a2007-01-15 06:12:45236$ git merge experimental
Junio C Hamanoc2b0a492006-01-23 07:54:36237------------------------------------------------
238
239If the changes don't conflict, you're done. If there are conflicts,
240markers will be left in the problematic files showing the conflict;
241
242------------------------------------------------
243$ git diff
244------------------------------------------------
245
246will show this. Once you've edited the files to resolve the
247conflicts,
248
249------------------------------------------------
250$ git commit -a
251------------------------------------------------
252
253will commit the result of the merge. Finally,
254
255------------------------------------------------
256$ gitk
257------------------------------------------------
258
259will show a nice graphical representation of the resulting history.
260
Junio C Hamanoeb692952007-01-03 22:02:12261At this point you could delete the experimental branch with
262
263------------------------------------------------
264$ git branch -d experimental
265------------------------------------------------
266
267This command ensures that the changes in the experimental branch are
268already in the current branch.
269
Junio C Hamanoc2b0a492006-01-23 07:54:36270If you develop on a branch crazy-idea, then regret it, you can always
271delete the branch with
272
273-------------------------------------
274$ git branch -D crazy-idea
Junio C Hamano1a4e8412005-12-27 08:17:23275-------------------------------------
276
Junio C Hamanoc2b0a492006-01-23 07:54:36277Branches are cheap and easy, so this is a good way to try something
278out.
Junio C Hamano1a4e8412005-12-27 08:17:23279
Junio C Hamano076ffcc2013-02-06 05:13:21280Using Git for collaboration
Junio C Hamano1a4e8412005-12-27 08:17:23281---------------------------
282
Junio C Hamano076ffcc2013-02-06 05:13:21283Suppose that Alice has started a new project with a Git repository in
Junio C Hamanoc2b0a492006-01-23 07:54:36284/home/alice/project, and that Bob, who has a home directory on the
285same machine, wants to contribute.
Junio C Hamano1a4e8412005-12-27 08:17:23286
Junio C Hamanoc2b0a492006-01-23 07:54:36287Bob begins with:
Junio C Hamano1a4e8412005-12-27 08:17:23288
Junio C Hamanoc2b0a492006-01-23 07:54:36289------------------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03290bob$ git clone /home/alice/project myrepo
Junio C Hamanoc2b0a492006-01-23 07:54:36291------------------------------------------------
Junio C Hamano1a4e8412005-12-27 08:17:23292
Junio C Hamanoc2b0a492006-01-23 07:54:36293This creates a new directory "myrepo" containing a clone of Alice's
294repository. The clone is on an equal footing with the original
Junio C Hamano341071d2006-06-04 07:24:48295project, possessing its own copy of the original project's history.
Junio C Hamano1a4e8412005-12-27 08:17:23296
Junio C Hamanoc2b0a492006-01-23 07:54:36297Bob then makes some changes and commits them:
Junio C Hamano1a4e8412005-12-27 08:17:23298
Junio C Hamanoc2b0a492006-01-23 07:54:36299------------------------------------------------
300(edit files)
Junio C Hamano38ddcce2008-07-15 15:49:03301bob$ git commit -a
Junio C Hamanoc2b0a492006-01-23 07:54:36302(repeat as necessary)
303------------------------------------------------
Junio C Hamano1a4e8412005-12-27 08:17:23304
Junio C Hamanoc2b0a492006-01-23 07:54:36305When he's ready, he tells Alice to pull changes from the repository
306at /home/bob/myrepo. She does this with:
Junio C Hamano1a4e8412005-12-27 08:17:23307
Junio C Hamanoc2b0a492006-01-23 07:54:36308------------------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03309alice$ cd /home/alice/project
310alice$ git pull /home/bob/myrepo master
Junio C Hamanoc2b0a492006-01-23 07:54:36311------------------------------------------------
Junio C Hamano1a4e8412005-12-27 08:17:23312
Junio C Hamanof98fd882006-11-26 07:28:29313This merges the changes from Bob's "master" branch into Alice's
314current branch. If Alice has made her own changes in the meantime,
Junio C Hamano20d47e32009-01-26 06:36:02315then she may need to manually fix any conflicts.
Junio C Hamano1a4e8412005-12-27 08:17:23316
Junio C Hamanof98fd882006-11-26 07:28:29317The "pull" command thus performs two operations: it fetches changes
318from a remote branch, then merges them into the current branch.
Junio C Hamano1a4e8412005-12-27 08:17:23319
Junio C Hamano38ddcce2008-07-15 15:49:03320Note that in general, Alice would want her local changes committed before
321initiating this "pull". If Bob's work conflicts with what Alice did since
322their histories forked, Alice will use her working tree and the index to
323resolve conflicts, and existing local changes will interfere with the
Junio C Hamano076ffcc2013-02-06 05:13:21324conflict resolution process (Git will still perform the fetch but will
Junio C Hamano38ddcce2008-07-15 15:49:03325refuse to merge --- Alice will have to get rid of her local changes in
326some way and pull again when this happens).
327
328Alice can peek at what Bob did without merging first, using the "fetch"
329command; this allows Alice to inspect what Bob did, using a special
330symbol "FETCH_HEAD", in order to determine if he has anything worth
331pulling, like this:
332
333------------------------------------------------
334alice$ git fetch /home/bob/myrepo master
Junio C Hamanoa387df32008-08-29 08:56:58335alice$ git log -p HEAD..FETCH_HEAD
Junio C Hamano38ddcce2008-07-15 15:49:03336------------------------------------------------
337
338This operation is safe even if Alice has uncommitted local changes.
Junio C Hamano73d812c2009-07-01 02:33:06339The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable
340from the FETCH_HEAD but exclude anything that is reachable from HEAD".
Junio C Hamanoa387df32008-08-29 08:56:58341Alice already knows everything that leads to her current state (HEAD),
Junio C Hamano73d812c2009-07-01 02:33:06342and reviews what Bob has in his state (FETCH_HEAD) that she has not
343seen with this command.
Junio C Hamanoa387df32008-08-29 08:56:58344
345If Alice wants to visualize what Bob did since their histories forked
346she can issue the following command:
347
348------------------------------------------------
349$ gitk HEAD..FETCH_HEAD
350------------------------------------------------
351
352This uses the same two-dot range notation we saw earlier with 'git log'.
353
354Alice may want to view what both of them did since they forked.
355She can use three-dot form instead of the two-dot form:
356
357------------------------------------------------
358$ gitk HEAD...FETCH_HEAD
359------------------------------------------------
360
361This means "show everything that is reachable from either one, but
362exclude anything that is reachable from both of them".
363
364Please note that these range notation can be used with both gitk
365and "git log".
Junio C Hamano38ddcce2008-07-15 15:49:03366
367After inspecting what Bob did, if there is nothing urgent, Alice may
368decide to continue working without pulling from Bob. If Bob's history
369does have something Alice would immediately need, Alice may choose to
370stash her work-in-progress first, do a "pull", and then finally unstash
371her work-in-progress on top of the resulting history.
372
Junio C Hamano35bd0252007-01-17 05:40:22373When you are working in a small closely knit group, it is not
374unusual to interact with the same repository over and over
375again. By defining 'remote' repository shorthand, you can make
376it easier:
377
378------------------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03379alice$ git remote add bob /home/bob/myrepo
Junio C Hamano35bd0252007-01-17 05:40:22380------------------------------------------------
381
Junio C Hamano73d812c2009-07-01 02:33:06382With this, Alice can perform the first part of the "pull" operation
Junio C Hamano1aa40d22010-01-21 17:46:43383alone using the 'git fetch' command without merging them with her own
Junio C Hamano73d812c2009-07-01 02:33:06384branch, using:
Junio C Hamano1a4e8412005-12-27 08:17:23385
Junio C Hamanoc2b0a492006-01-23 07:54:36386-------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03387alice$ git fetch bob
Junio C Hamanoc2b0a492006-01-23 07:54:36388-------------------------------------
Junio C Hamano1a4e8412005-12-27 08:17:23389
Junio C Hamano35bd0252007-01-17 05:40:22390Unlike the longhand form, when Alice fetches from Bob using a
Junio C Hamano1aa40d22010-01-21 17:46:43391remote repository shorthand set up with 'git remote', what was
Junio C Hamano97bcb482010-11-25 03:16:07392fetched is stored in a remote-tracking branch, in this case
Junio C Hamano35bd0252007-01-17 05:40:22393`bob/master`. So after this:
Junio C Hamanoc2b0a492006-01-23 07:54:36394
395-------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03396alice$ git log -p master..bob/master
Junio C Hamanoc2b0a492006-01-23 07:54:36397-------------------------------------
398
399shows a list of all the changes that Bob made since he branched from
400Alice's master branch.
401
Junio C Hamano35bd0252007-01-17 05:40:22402After examining those changes, Alice
Junio C Hamanoedd2b0a2007-01-15 06:12:45403could merge the changes into her master branch:
Junio C Hamanoc2b0a492006-01-23 07:54:36404
405-------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03406alice$ git merge bob/master
Junio C Hamanoc2b0a492006-01-23 07:54:36407-------------------------------------
408
Junio C Hamano97bcb482010-11-25 03:16:07409This `merge` can also be done by 'pulling from her own remote-tracking
410branch', like this:
Junio C Hamanof98fd882006-11-26 07:28:29411
412-------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03413alice$ git pull . remotes/bob/master
Junio C Hamanof98fd882006-11-26 07:28:29414-------------------------------------
415
Junio C Hamano35bd0252007-01-17 05:40:22416Note that git pull always merges into the current branch,
Junio C Hamanoa6387422007-08-25 03:54:27417regardless of what else is given on the command line.
Junio C Hamanof98fd882006-11-26 07:28:29418
Junio C Hamanoc2b0a492006-01-23 07:54:36419Later, Bob can update his repo with Alice's latest changes using
420
421-------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03422bob$ git pull
Junio C Hamanoc2b0a492006-01-23 07:54:36423-------------------------------------
424
425Note that he doesn't need to give the path to Alice's repository;
Junio C Hamano076ffcc2013-02-06 05:13:21426when Bob cloned Alice's repository, Git stored the location of her
Junio C Hamanod3361ad2007-01-01 03:20:24427repository in the repository configuration, and that location is
428used for pulls:
Junio C Hamanoc2b0a492006-01-23 07:54:36429
430-------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03431bob$ git config --get remote.origin.url
Junio C Hamano330aae62007-07-06 17:01:58432/home/alice/project
Junio C Hamanoc2b0a492006-01-23 07:54:36433-------------------------------------
434
Junio C Hamano1aa40d22010-01-21 17:46:43435(The complete configuration created by 'git clone' is visible using
Junio C Hamanofce7c7e2008-07-02 03:06:38436`git config -l`, and the linkgit:git-config[1] man page
Junio C Hamanod3361ad2007-01-01 03:20:24437explains the meaning of each option.)
438
439Git also keeps a pristine copy of Alice's master branch under the
440name "origin/master":
441
442-------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03443bob$ git branch -r
Junio C Hamanod3361ad2007-01-01 03:20:24444 origin/master
445-------------------------------------
Junio C Hamanoc2b0a492006-01-23 07:54:36446
447If Bob later decides to work from a different host, he can still
448perform clones and pulls using the ssh protocol:
449
450-------------------------------------
Junio C Hamano38ddcce2008-07-15 15:49:03451bob$ git clone alice.org:/home/alice/project myrepo
Junio C Hamanoc2b0a492006-01-23 07:54:36452-------------------------------------
453
Junio C Hamanof85ef632016-02-17 22:31:06454Alternatively, Git has a native protocol, or can use http;
Junio C Hamano35738e82008-01-07 07:55:46455see linkgit:git-pull[1] for details.
Junio C Hamanoc2b0a492006-01-23 07:54:36456
457Git can also be used in a CVS-like mode, with a central repository
Junio C Hamano35738e82008-01-07 07:55:46458that various users push changes to; see linkgit:git-push[1] and
Junio C Hamanofce7c7e2008-07-02 03:06:38459linkgit:gitcvs-migration[7].
Junio C Hamanoc2b0a492006-01-23 07:54:36460
Junio C Hamano6f8a7902006-05-22 01:10:13461Exploring history
462-----------------
Junio C Hamanoc2b0a492006-01-23 07:54:36463
Junio C Hamano6f8a7902006-05-22 01:10:13464Git history is represented as a series of interrelated commits. We
Junio C Hamano1aa40d22010-01-21 17:46:43465have already seen that the 'git log' command can list those commits.
Junio C Hamano6f8a7902006-05-22 01:10:13466Note that first line of each git log entry also gives a name for the
467commit:
Junio C Hamanoc2b0a492006-01-23 07:54:36468
469-------------------------------------
Junio C Hamano6f8a7902006-05-22 01:10:13470$ git log
471commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
472Author: Junio C Hamano <junkio@cox.net>
473Date: Tue May 16 17:18:22 2006 -0700
474
475 merge-base: Clarify the comments on post processing.
Junio C Hamanoc2b0a492006-01-23 07:54:36476-------------------------------------
477
Junio C Hamano1aa40d22010-01-21 17:46:43478We can give this name to 'git show' to see the details about this
Junio C Hamano6f8a7902006-05-22 01:10:13479commit.
Junio C Hamanoc2b0a492006-01-23 07:54:36480
481-------------------------------------
Junio C Hamano6f8a7902006-05-22 01:10:13482$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
Junio C Hamanoc2b0a492006-01-23 07:54:36483-------------------------------------
484
Junio C Hamanoeb692952007-01-03 22:02:12485But there are other ways to refer to commits. You can use any initial
Junio C Hamano6f8a7902006-05-22 01:10:13486part of the name that is long enough to uniquely identify the commit:
Junio C Hamanoc2b0a492006-01-23 07:54:36487
488-------------------------------------
Junio C Hamano6f8a7902006-05-22 01:10:13489$ git show c82a22c39c # the first few characters of the name are
490# usually enough
491$ git show HEAD # the tip of the current branch
492$ git show experimental # the tip of the "experimental" branch
Junio C Hamanoc2b0a492006-01-23 07:54:36493-------------------------------------
494
Junio C Hamanoeb692952007-01-03 22:02:12495Every commit usually has one "parent" commit
496which points to the previous state of the project:
Junio C Hamanoc2b0a492006-01-23 07:54:36497
498-------------------------------------
Junio C Hamano6f8a7902006-05-22 01:10:13499$ git show HEAD^ # to see the parent of HEAD
500$ git show HEAD^^ # to see the grandparent of HEAD
501$ git show HEAD~4 # to see the great-great grandparent of HEAD
Junio C Hamanoc2b0a492006-01-23 07:54:36502-------------------------------------
503
Junio C Hamano6f8a7902006-05-22 01:10:13504Note that merge commits may have more than one parent:
Junio C Hamanoc2b0a492006-01-23 07:54:36505
506-------------------------------------
Junio C Hamano6f8a7902006-05-22 01:10:13507$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
508$ git show HEAD^2 # show the second parent of HEAD
Junio C Hamanoc2b0a492006-01-23 07:54:36509-------------------------------------
510
Junio C Hamano6f8a7902006-05-22 01:10:13511You can also give commits names of your own; after running
Junio C Hamanoc2b0a492006-01-23 07:54:36512
513-------------------------------------
Junio C Hamanofce7c7e2008-07-02 03:06:38514$ git tag v2.5 1b2e1d63ff
Junio C Hamanoc2b0a492006-01-23 07:54:36515-------------------------------------
516
Junio C Hamano6f8a7902006-05-22 01:10:13517you can refer to 1b2e1d63ff by the name "v2.5". If you intend to
518share this name with other people (for example, to identify a release
Junio C Hamanoc2b0a492006-01-23 07:54:36519version), you should create a "tag" object, and perhaps sign it; see
Junio C Hamano35738e82008-01-07 07:55:46520linkgit:git-tag[1] for details.
Junio C Hamanoc2b0a492006-01-23 07:54:36521
Junio C Hamano076ffcc2013-02-06 05:13:21522Any Git command that needs to know a commit can take any of these
Junio C Hamano6f8a7902006-05-22 01:10:13523names. For example:
Junio C Hamanoc2b0a492006-01-23 07:54:36524
525-------------------------------------
Junio C Hamano6f8a7902006-05-22 01:10:13526$ git diff v2.5 HEAD # compare the current HEAD to v2.5
527$ git branch stable v2.5 # start a new branch named "stable" based
528 # at v2.5
529$ git reset --hard HEAD^ # reset your current branch and working
Junio C Hamano33db4372006-06-07 19:51:45530 # directory to its state at HEAD^
Junio C Hamanoc2b0a492006-01-23 07:54:36531-------------------------------------
532
Junio C Hamano6f8a7902006-05-22 01:10:13533Be careful with that last command: in addition to losing any changes
534in the working directory, it will also remove all later commits from
535this branch. If this branch is the only branch containing those
Junio C Hamano1aa40d22010-01-21 17:46:43536commits, they will be lost. Also, don't use 'git reset' on a
Junio C Hamanoee1e4282007-02-04 08:32:04537publicly-visible branch that other developers pull from, as it will
538force needless merges on other developers to clean up the history.
Junio C Hamano1aa40d22010-01-21 17:46:43539If you need to undo changes that you have pushed, use 'git revert'
Junio C Hamano16cf1582007-02-05 07:21:48540instead.
Junio C Hamano6f8a7902006-05-22 01:10:13541
Junio C Hamano1aa40d22010-01-21 17:46:43542The 'git grep' command can search for strings in any version of your
Junio C Hamano6f8a7902006-05-22 01:10:13543project, so
544
545-------------------------------------
546$ git grep "hello" v2.5
547-------------------------------------
548
Junio C Hamano341071d2006-06-04 07:24:48549searches for all occurrences of "hello" in v2.5.
Junio C Hamano6f8a7902006-05-22 01:10:13550
Junio C Hamano1aa40d22010-01-21 17:46:43551If you leave out the commit name, 'git grep' will search any of the
Junio C Hamano6f8a7902006-05-22 01:10:13552files it manages in your current directory. So
553
554-------------------------------------
555$ git grep "hello"
556-------------------------------------
557
Junio C Hamano076ffcc2013-02-06 05:13:21558is a quick way to search just the files that are tracked by Git.
Junio C Hamano6f8a7902006-05-22 01:10:13559
Junio C Hamano076ffcc2013-02-06 05:13:21560Many Git commands also take sets of commits, which can be specified
Junio C Hamano1aa40d22010-01-21 17:46:43561in a number of ways. Here are some examples with 'git log':
Junio C Hamano6f8a7902006-05-22 01:10:13562
563-------------------------------------
564$ git log v2.5..v2.6 # commits between v2.5 and v2.6
565$ git log v2.5.. # commits since v2.5
566$ git log --since="2 weeks ago" # commits from the last 2 weeks
567$ git log v2.5.. Makefile # commits since v2.5 which modify
568# Makefile
569-------------------------------------
570
Junio C Hamano1aa40d22010-01-21 17:46:43571You can also give 'git log' a "range" of commits where the first is not
Junio C Hamano6f8a7902006-05-22 01:10:13572necessarily an ancestor of the second; for example, if the tips of
Junio C Hamano73d812c2009-07-01 02:33:06573the branches "stable" and "master" diverged from a common
Junio C Hamano6f8a7902006-05-22 01:10:13574commit some time ago, then
575
576-------------------------------------
Junio C Hamano73d812c2009-07-01 02:33:06577$ git log stable..master
Junio C Hamano6f8a7902006-05-22 01:10:13578-------------------------------------
579
Junio C Hamano73d812c2009-07-01 02:33:06580will list commits made in the master branch but not in the
Junio C Hamano6f8a7902006-05-22 01:10:13581stable branch, while
582
583-------------------------------------
Junio C Hamano73d812c2009-07-01 02:33:06584$ git log master..stable
Junio C Hamano6f8a7902006-05-22 01:10:13585-------------------------------------
586
587will show the list of commits made on the stable branch but not
Junio C Hamano73d812c2009-07-01 02:33:06588the master branch.
Junio C Hamano6f8a7902006-05-22 01:10:13589
Junio C Hamano1aa40d22010-01-21 17:46:43590The 'git log' command has a weakness: it must present commits in a
Junio C Hamano6f8a7902006-05-22 01:10:13591list. When the history has lines of development that diverged and
Junio C Hamano1aa40d22010-01-21 17:46:43592then merged back together, the order in which 'git log' presents
Junio C Hamano6f8a7902006-05-22 01:10:13593those commits is meaningless.
594
Junio C Hamano4fbdd442009-01-06 05:56:24595Most projects with multiple contributors (such as the Linux kernel,
Junio C Hamano076ffcc2013-02-06 05:13:21596or Git itself) have frequent merges, and 'gitk' does a better job of
Junio C Hamano6f8a7902006-05-22 01:10:13597visualizing their history. For example,
598
599-------------------------------------
600$ gitk --since="2 weeks ago" drivers/
601-------------------------------------
602
603allows you to browse any commits from the last 2 weeks of commits
Junio C Hamanobb8fb052006-05-30 07:21:12604that modified files under the "drivers" directory. (Note: you can
605adjust gitk's fonts by holding down the control key while pressing
606"-" or "+".)
Junio C Hamano6f8a7902006-05-22 01:10:13607
608Finally, most commands that take filenames will optionally allow you
609to precede any filename by a commit, to specify a particular version
Junio C Hamanobb8fb052006-05-30 07:21:12610of the file:
Junio C Hamano6f8a7902006-05-22 01:10:13611
612-------------------------------------
613$ git diff v2.5:Makefile HEAD:Makefile.in
614-------------------------------------
Junio C Hamanoc2b0a492006-01-23 07:54:36615
Junio C Hamano1aa40d22010-01-21 17:46:43616You can also use 'git show' to see any such file:
Junio C Hamanobb8fb052006-05-30 07:21:12617
618-------------------------------------
Junio C Hamanoeb692952007-01-03 22:02:12619$ git show v2.5:Makefile
Junio C Hamanobb8fb052006-05-30 07:21:12620-------------------------------------
621
Junio C Hamanoc2b0a492006-01-23 07:54:36622Next Steps
623----------
624
Junio C Hamano6f8a7902006-05-22 01:10:13625This tutorial should be enough to perform basic distributed revision
626control for your projects. However, to fully understand the depth
Junio C Hamano076ffcc2013-02-06 05:13:21627and power of Git you need to understand two simple ideas on which it
Junio C Hamano6f8a7902006-05-22 01:10:13628is based:
Junio C Hamanoc2b0a492006-01-23 07:54:36629
Junio C Hamano6f8a7902006-05-22 01:10:13630 * The object database is the rather elegant system used to
631 store the history of your project--files, directories, and
632 commits.
633
634 * The index file is a cache of the state of a directory tree,
635 used to create commits, check out working directories, and
636 hold the various trees involved in a merge.
637
Junio C Hamanofce7c7e2008-07-02 03:06:38638Part two of this tutorial explains the object
Junio C Hamano6f8a7902006-05-22 01:10:13639database, the index file, and a few other odds and ends that you'll
Junio C Hamano076ffcc2013-02-06 05:13:21640need to make the most of Git. You can find it at linkgit:gittutorial-2[7].
Junio C Hamano6f8a7902006-05-22 01:10:13641
Junio C Hamanoed7f4f62007-05-20 09:09:09642If you don't want to continue with that right away, a few other
Junio C Hamano6f8a7902006-05-22 01:10:13643digressions that may be interesting at this point are:
Junio C Hamanoc2b0a492006-01-23 07:54:36644
Junio C Hamano35738e82008-01-07 07:55:46645 * linkgit:git-format-patch[1], linkgit:git-am[1]: These convert
Junio C Hamanoc2b0a492006-01-23 07:54:36646 series of git commits into emailed patches, and vice versa,
Junio C Hamano4fbdd442009-01-06 05:56:24647 useful for projects such as the Linux kernel which rely heavily
Junio C Hamanoc2b0a492006-01-23 07:54:36648 on emailed patches.
649
Junio C Hamano35738e82008-01-07 07:55:46650 * linkgit:git-bisect[1]: When there is a regression in your
Junio C Hamanoc2b0a492006-01-23 07:54:36651 project, one way to track down the bug is by searching through
652 the history to find the exact commit that's to blame. Git bisect
653 can help you perform a binary search for that commit. It is
654 smart enough to perform a close-to-optimal search even in the
655 case of complex non-linear history with lots of merged branches.
656
Junio C Hamano804b5212009-06-07 16:05:03657 * linkgit:gitworkflows[7]: Gives an overview of recommended
658 workflows.
659
Junio C Hamano446e30b2014-10-16 21:30:32660 * linkgit:giteveryday[7]: Everyday Git with 20 Commands Or So.
Junio C Hamano6f8a7902006-05-22 01:10:13661
Junio C Hamanofce7c7e2008-07-02 03:06:38662 * linkgit:gitcvs-migration[7]: Git for CVS users.
Junio C Hamanodfccbb02008-05-26 01:16:14663
664SEE ALSO
665--------
666linkgit:gittutorial-2[7],
667linkgit:gitcvs-migration[7],
Junio C Hamano9e1793f2008-06-02 07:31:16668linkgit:gitcore-tutorial[7],
669linkgit:gitglossary[7],
Junio C Hamanof66ecee2008-11-17 18:25:43670linkgit:git-help[1],
Junio C Hamano804b5212009-06-07 16:05:03671linkgit:gitworkflows[7],
Junio C Hamano446e30b2014-10-16 21:30:32672linkgit:giteveryday[7],
Junio C Hamanodfccbb02008-05-26 01:16:14673link:user-manual.html[The Git User's Manual]
674
675GIT
676---
Junio C Hamanoe89102f2017-02-15 23:18:15677Part of the linkgit:git[1] suite