blob: 59ef5cef1f080d079109c50f6034ff88901c658f [file] [log] [blame]
Christian Couderb27a23e2008-05-24 20:56:44 +02001gittutorial(7)
2==============
3
4NAME
5----
Thomas Ackermann022cf2b2014-11-11 20:13:36 +01006gittutorial - A tutorial introduction to Git
Christian Couderb27a23e2008-05-24 20:56:44 +02007
8SYNOPSIS
9--------
Martin von Zweigbergk7791a1d2011-07-01 22:38:26 -040010[verse]
Christian Couderb27a23e2008-05-24 20:56:44 +020011git *
12
13DESCRIPTION
14-----------
Linus Torvalds8c7fa242005-05-31 19:50:34 -070015
Thomas Ackermann2de9b712013-01-21 20:17:53 +010016This tutorial explains how to import a new project into Git, make
J. Bruce Fields927a5032006-01-22 23:57:25 -050017changes to it, and share changes with other developers.
Linus Torvalds8c7fa242005-05-31 19:50:34 -070018
Thomas Ackermann2de9b712013-01-21 20:17:53 +010019If you are instead primarily interested in using Git to fetch a project,
J. Bruce Fieldscd50aba2007-05-17 23:56:08 -040020for 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
Jonathan Nieder46e56e82008-06-30 17:17:07 -050023First, note that you can get documentation for a command such as
Jonathan Nieder483bc4f2008-06-30 13:56:34 -050024`git log --graph` with:
Linus Torvalds8c7fa242005-05-31 19:50:34 -070025
Junio C Hamano8db93072005-08-30 13:51:01 -070026------------------------------------------------
Jonathan Nieder3861cd52008-06-30 17:10:25 -050027$ man git-log
Junio C Hamano8db93072005-08-30 13:51:01 -070028------------------------------------------------
Linus Torvalds8c7fa242005-05-31 19:50:34 -070029
Christian Couder6e702c22008-11-17 16:43:04 +010030or:
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
Thomas Ackermann2de9b712013-01-21 20:17:53 +010039It is a good idea to introduce yourself to Git with your name and
Nicolas Pitrec14261e2007-01-14 22:44:18 -050040public email address before doing any operation. The easiest
41way to do so is:
Junio C Hamano66589232006-11-29 00:17:01 -080042
43------------------------------------------------
Tom Princee0d10e12007-01-28 16:16:53 -080044$ git config --global user.name "Your Name Comes Here"
45$ git config --global user.email you@yourdomain.example.com
Junio C Hamano66589232006-11-29 00:17:01 -080046------------------------------------------------
47
48
J. Bruce Fields927a5032006-01-22 23:57:25 -050049Importing a new project
Junio C Hamano2a29da72005-08-23 15:28:34 -070050-----------------------
Junio C Hamano3eb51282005-07-15 11:40:56 -070051
J. Bruce Fields927a5032006-01-22 23:57:25 -050052Assume you have a tarball project.tar.gz with your initial work. You
Thomas Ackermann2de9b712013-01-21 20:17:53 +010053can place it under Git revision control as follows.
Junio C Hamano3eb51282005-07-15 11:40:56 -070054
Junio C Hamanoc9517342005-08-24 16:46:11 -070055------------------------------------------------
Junio C Hamanodcc6e282006-01-22 22:43:59 -080056$ tar xzf project.tar.gz
J. Bruce Fields927a5032006-01-22 23:57:25 -050057$ cd project
Nicolas Pitre515377e2007-01-07 12:31:29 -050058$ git init
Junio C Hamanoc9517342005-08-24 16:46:11 -070059------------------------------------------------
60
J. Bruce Fields927a5032006-01-22 23:57:25 -050061Git will reply
Junio C Hamanoc9517342005-08-24 16:46:11 -070062
Junio C Hamanoc9517342005-08-24 16:46:11 -070063------------------------------------------------
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -050064Initialized empty Git repository in .git/
Junio C Hamanoc9517342005-08-24 16:46:11 -070065------------------------------------------------
Junio C Hamano2a29da72005-08-23 15:28:34 -070066
J. Bruce Fields927a5032006-01-22 23:57:25 -050067You've now initialized the working directory--you may notice a new
J. Bruce Fields93f9cc62007-05-18 00:51:42 -040068directory created, named ".git".
69
Thomas Ackermann2de9b712013-01-21 20:17:53 +010070Next, tell Git to take a snapshot of the contents of all files under the
Thomas Rast0b444cd2010-01-10 00:33:00 +010071current directory (note the '.'), with 'git add':
Junio C Hamano2a29da72005-08-23 15:28:34 -070072
J. Bruce Fields927a5032006-01-22 23:57:25 -050073------------------------------------------------
74$ git add .
75------------------------------------------------
76
Thomas Ackermann2de9b712013-01-21 20:17:53 +010077This snapshot is now stored in a temporary staging area which Git calls
J. Bruce Fields93f9cc62007-05-18 00:51:42 -040078the "index". You can permanently store the contents of the index in the
Thomas Rast0b444cd2010-01-10 00:33:00 +010079repository with 'git commit':
J. Bruce Fields927a5032006-01-22 23:57:25 -050080
81------------------------------------------------
Junio C Hamano66589232006-11-29 00:17:01 -080082$ git commit
J. Bruce Fields927a5032006-01-22 23:57:25 -050083------------------------------------------------
84
J. Bruce Fields93f9cc62007-05-18 00:51:42 -040085This will prompt you for a commit message. You've now stored the first
Thomas Ackermann2de9b712013-01-21 20:17:53 +010086version of your project in Git.
J. Bruce Fields927a5032006-01-22 23:57:25 -050087
J. Bruce Fields84dee6b2007-01-06 22:38:38 -050088Making changes
89--------------
90
J. Bruce Fields93f9cc62007-05-18 00:51:42 -040091Modify some files, then add their updated contents to the index:
J. Bruce Fields927a5032006-01-22 23:57:25 -050092
93------------------------------------------------
J. Bruce Fields84dee6b2007-01-06 22:38:38 -050094$ git add file1 file2 file3
J. Bruce Fields93f9cc62007-05-18 00:51:42 -040095------------------------------------------------
96
97You are now ready to commit. You can see what is about to be committed
Thomas Rast0b444cd2010-01-10 00:33:00 +010098using 'git diff' with the --cached option:
J. Bruce Fields93f9cc62007-05-18 00:51:42 -040099
100------------------------------------------------
101$ git diff --cached
102------------------------------------------------
103
Thomas Rast0b444cd2010-01-10 00:33:00 +0100104(Without --cached, 'git diff' will show you any changes that
J. Bruce Fields93f9cc62007-05-18 00:51:42 -0400105you've made but not yet added to the index.) You can also get a brief
Thomas Rast0b444cd2010-01-10 00:33:00 +0100106summary of the situation with 'git status':
J. Bruce Fields93f9cc62007-05-18 00:51:42 -0400107
108------------------------------------------------
109$ git status
Stefan Naewe89428212014-11-13 10:40:07 +0000110On branch master
111Changes to be committed:
Martin Ågren7560f542017-08-23 19:49:35 +0200112Your branch is up to date with 'origin/master'.
Nguyễn Thái Ngọc Duy80f537f2019-04-25 16:45:58 +0700113 (use "git restore --staged <file>..." to unstage)
Stefan Naewe89428212014-11-13 10:40:07 +0000114
115 modified: file1
116 modified: file2
117 modified: file3
118
J. Bruce Fields93f9cc62007-05-18 00:51:42 -0400119------------------------------------------------
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 Hamanoc1d179f2007-01-03 08:38:01 -0800125$ git commit
J. Bruce Fields927a5032006-01-22 23:57:25 -0500126------------------------------------------------
127
Fred Maranhão2feaf4e2008-06-11 19:09:48 -0400128This will again prompt you for a message describing the change, and then
J. Bruce Fields93f9cc62007-05-18 00:51:42 -0400129record a new version of the project.
J. Bruce Fields84dee6b2007-01-06 22:38:38 -0500130
Thomas Rast0b444cd2010-01-10 00:33:00 +0100131Alternatively, instead of running 'git add' beforehand, you can use
Junio C Hamano66589232006-11-29 00:17:01 -0800132
133------------------------------------------------
134$ git commit -a
135------------------------------------------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500136
J. Bruce Fields93f9cc62007-05-18 00:51:42 -0400137which will automatically notice any modified (but not new) files, add
138them to the index, and commit, all in one step.
J. Bruce Fields84dee6b2007-01-06 22:38:38 -0500139
J. Bruce Fields927a5032006-01-22 23:57:25 -0500140A 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
Jeremy White52ffe992012-09-13 17:27:09 -0500143thorough description. The text up to the first blank line in a commit
144message is treated as the commit title, and that title is used
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100145throughout Git. For example, linkgit:git-format-patch[1] turns a
Jeremy White52ffe992012-09-13 17:27:09 -0500146commit into email, and it uses the title on the Subject line and the
147rest of the commit in the body.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500148
Nicolas Pitre366bfcb2006-12-04 11:13:39 -0500149Git tracks content not files
150----------------------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500151
Jonathan Nieder483bc4f2008-06-30 13:56:34 -0500152Many revision control systems provide an `add` command that tells the
153system to start tracking changes to a new file. Git's `add` command
Thomas Rast0b444cd2010-01-10 00:33:00 +0100154does something simpler and more powerful: 'git add' is used both for new
J. Bruce Fields93f9cc62007-05-18 00:51:42 -0400155and 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.
Nicolas Pitre366bfcb2006-12-04 11:13:39 -0500158
J. Bruce Fields23c9ccb2007-06-10 16:20:34 -0400159Viewing project history
160-----------------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500161
162At any point you can view the history of your changes using
163
164------------------------------------------------
J. Bruce Fields67e6e5c2006-05-21 16:52:34 -0400165$ git log
J. Bruce Fields927a5032006-01-22 23:57:25 -0500166------------------------------------------------
167
168If you also want to see complete diffs at each step, use
169
170------------------------------------------------
J. Bruce Fields67e6e5c2006-05-21 16:52:34 -0400171$ git log -p
J. Bruce Fields927a5032006-01-22 23:57:25 -0500172------------------------------------------------
173
Junio C Hamanoc1d179f2007-01-03 08:38:01 -0800174Often the overview of the change is useful to get a feel of
175each step
176
177------------------------------------------------
178$ git log --stat --summary
179------------------------------------------------
180
J. Bruce Fields927a5032006-01-22 23:57:25 -0500181Managing branches
182-----------------
183
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100184A single Git repository can maintain multiple branches of
J. Bruce Fields927a5032006-01-22 23:57:25 -0500185development. 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------------------------------------------------
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700210$ git switch experimental
J. Bruce Fields927a5032006-01-22 23:57:25 -0500211------------------------------------------------
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
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700219$ git switch master
J. Bruce Fields927a5032006-01-22 23:57:25 -0500220------------------------------------------------
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
Paolo Ciarrocchi59427062006-11-20 21:29:41 +0100233made in each. To merge the changes made in experimental into master, run
J. Bruce Fields927a5032006-01-22 23:57:25 -0500234
235------------------------------------------------
Nicolas Pitrec14261e2007-01-14 22:44:18 -0500236$ git merge experimental
J. Bruce Fields927a5032006-01-22 23:57:25 -0500237------------------------------------------------
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
Santi Béjar9c9410e2007-01-03 13:53:27 +0100261At 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
J. Bruce Fields927a5032006-01-22 23:57:25 -0500270If 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 Hamanodc5f9232005-12-05 00:57:48 -0800275-------------------------------------
276
J. Bruce Fields927a5032006-01-22 23:57:25 -0500277Branches are cheap and easy, so this is a good way to try something
278out.
Junio C Hamanodc5f9232005-12-05 00:57:48 -0800279
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100280Using Git for collaboration
Junio C Hamano6f603002005-09-20 18:21:10 -0700281---------------------------
282
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100283Suppose that Alice has started a new project with a Git repository in
J. Bruce Fields927a5032006-01-22 23:57:25 -0500284/home/alice/project, and that Bob, who has a home directory on the
285same machine, wants to contribute.
Junio C Hamano6f603002005-09-20 18:21:10 -0700286
J. Bruce Fields927a5032006-01-22 23:57:25 -0500287Bob begins with:
Junio C Hamano6f603002005-09-20 18:21:10 -0700288
J. Bruce Fields927a5032006-01-22 23:57:25 -0500289------------------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400290bob$ git clone /home/alice/project myrepo
J. Bruce Fields927a5032006-01-22 23:57:25 -0500291------------------------------------------------
Junio C Hamano6f603002005-09-20 18:21:10 -0700292
J. Bruce Fields927a5032006-01-22 23:57:25 -0500293This creates a new directory "myrepo" containing a clone of Alice's
294repository. The clone is on an equal footing with the original
Horst H. von Brandabda1ef2006-06-03 16:27:26 -0400295project, possessing its own copy of the original project's history.
Junio C Hamano6f603002005-09-20 18:21:10 -0700296
J. Bruce Fields927a5032006-01-22 23:57:25 -0500297Bob then makes some changes and commits them:
Junio C Hamano6f603002005-09-20 18:21:10 -0700298
J. Bruce Fields927a5032006-01-22 23:57:25 -0500299------------------------------------------------
300(edit files)
Ian Katz5d5e88a2008-07-10 14:27:30 -0400301bob$ git commit -a
J. Bruce Fields927a5032006-01-22 23:57:25 -0500302(repeat as necessary)
303------------------------------------------------
Junio C Hamano6f603002005-09-20 18:21:10 -0700304
J. Bruce Fields927a5032006-01-22 23:57:25 -0500305When he's ready, he tells Alice to pull changes from the repository
306at /home/bob/myrepo. She does this with:
Junio C Hamano6f603002005-09-20 18:21:10 -0700307
J. Bruce Fields927a5032006-01-22 23:57:25 -0500308------------------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400309alice$ cd /home/alice/project
310alice$ git pull /home/bob/myrepo master
J. Bruce Fields927a5032006-01-22 23:57:25 -0500311------------------------------------------------
Junio C Hamano6f603002005-09-20 18:21:10 -0700312
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100313This merges the changes from Bob's "master" branch into Alice's
J. Bruce Fields93ee7822006-11-25 22:45:02 -0500314current branch. If Alice has made her own changes in the meantime,
Miklos Vajnac30e5672009-01-23 19:02:29 +0100315then she may need to manually fix any conflicts.
Junio C Hamano6f603002005-09-20 18:21:10 -0700316
J. Bruce Fields93ee7822006-11-25 22:45:02 -0500317The "pull" command thus performs two operations: it fetches changes
318from a remote branch, then merges them into the current branch.
Junio C Hamano6f603002005-09-20 18:21:10 -0700319
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700320Note 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
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100324conflict resolution process (Git will still perform the fetch but will
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700325refuse 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
Paolo Ciarrocchi53d15892008-08-28 14:23:52 +0200335alice$ git log -p HEAD..FETCH_HEAD
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700336------------------------------------------------
337
338This operation is safe even if Alice has uncommitted local changes.
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300339The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable
340from the FETCH_HEAD but exclude anything that is reachable from HEAD".
Paolo Ciarrocchi53d15892008-08-28 14:23:52 +0200341Alice already knows everything that leads to her current state (HEAD),
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300342and reviews what Bob has in his state (FETCH_HEAD) that she has not
343seen with this command.
Paolo Ciarrocchi53d15892008-08-28 14:23:52 +0200344
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 Hamanodc29bc82008-07-10 14:01:57 -0700366
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 Hamanoc1ff2842007-01-17 01:10:14 +0100373When 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------------------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400379alice$ git remote add bob /home/bob/myrepo
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100380------------------------------------------------
381
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300382With this, Alice can perform the first part of the "pull" operation
Thomas Rast0b444cd2010-01-10 00:33:00 +0100383alone using the 'git fetch' command without merging them with her own
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300384branch, using:
Junio C Hamano6f603002005-09-20 18:21:10 -0700385
J. Bruce Fields927a5032006-01-22 23:57:25 -0500386-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400387alice$ git fetch bob
J. Bruce Fields927a5032006-01-22 23:57:25 -0500388-------------------------------------
Junio C Hamano6f603002005-09-20 18:21:10 -0700389
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100390Unlike the longhand form, when Alice fetches from Bob using a
Thomas Rast0b444cd2010-01-10 00:33:00 +0100391remote repository shorthand set up with 'git remote', what was
Matthieu Moy0e615b22010-11-02 16:31:20 +0100392fetched is stored in a remote-tracking branch, in this case
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100393`bob/master`. So after this:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500394
395-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400396alice$ git log -p master..bob/master
J. Bruce Fields927a5032006-01-22 23:57:25 -0500397-------------------------------------
398
399shows a list of all the changes that Bob made since he branched from
400Alice's master branch.
401
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100402After examining those changes, Alice
Nicolas Pitrec14261e2007-01-14 22:44:18 -0500403could merge the changes into her master branch:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500404
405-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400406alice$ git merge bob/master
J. Bruce Fields927a5032006-01-22 23:57:25 -0500407-------------------------------------
408
Matthieu Moy60109d02010-11-02 16:31:21 +0100409This `merge` can also be done by 'pulling from her own remote-tracking
410branch', like this:
J. Bruce Fields93ee7822006-11-25 22:45:02 -0500411
412-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400413alice$ git pull . remotes/bob/master
J. Bruce Fields93ee7822006-11-25 22:45:02 -0500414-------------------------------------
415
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100416Note that git pull always merges into the current branch,
Brian Hetro02783072007-08-23 20:44:13 -0400417regardless of what else is given on the command line.
J. Bruce Fields93ee7822006-11-25 22:45:02 -0500418
J. Bruce Fields927a5032006-01-22 23:57:25 -0500419Later, Bob can update his repo with Alice's latest changes using
420
421-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400422bob$ git pull
J. Bruce Fields927a5032006-01-22 23:57:25 -0500423-------------------------------------
424
425Note that he doesn't need to give the path to Alice's repository;
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100426when Bob cloned Alice's repository, Git stored the location of her
J. Bruce Fieldsd66409f2006-12-31 18:47:38 -0500427repository in the repository configuration, and that location is
428used for pulls:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500429
430-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400431bob$ git config --get remote.origin.url
Alecs King8960b5a2007-07-06 00:21:16 +0800432/home/alice/project
J. Bruce Fields927a5032006-01-22 23:57:25 -0500433-------------------------------------
434
Thomas Rast0b444cd2010-01-10 00:33:00 +0100435(The complete configuration created by 'git clone' is visible using
Jonathan Nieder483bc4f2008-06-30 13:56:34 -0500436`git config -l`, and the linkgit:git-config[1] man page
J. Bruce Fieldsd66409f2006-12-31 18:47:38 -0500437explains the meaning of each option.)
438
439Git also keeps a pristine copy of Alice's master branch under the
440name "origin/master":
441
442-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400443bob$ git branch -r
J. Bruce Fieldsd66409f2006-12-31 18:47:38 -0500444 origin/master
445-------------------------------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500446
447If Bob later decides to work from a different host, he can still
448perform clones and pulls using the ssh protocol:
449
450-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400451bob$ git clone alice.org:/home/alice/project myrepo
J. Bruce Fields927a5032006-01-22 23:57:25 -0500452-------------------------------------
453
Jeff King0d0bac62016-01-30 02:21:26 -0500454Alternatively, Git has a native protocol, or can use http;
Dan McGee5162e692007-12-29 00:20:38 -0600455see linkgit:git-pull[1] for details.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500456
457Git can also be used in a CVS-like mode, with a central repository
Dan McGee5162e692007-12-29 00:20:38 -0600458that various users push changes to; see linkgit:git-push[1] and
Jonathan Nieder6998e4d2008-06-30 17:01:21 -0500459linkgit:gitcvs-migration[7].
J. Bruce Fields927a5032006-01-22 23:57:25 -0500460
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400461Exploring history
462-----------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500463
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400464Git history is represented as a series of interrelated commits. We
Thomas Rast0b444cd2010-01-10 00:33:00 +0100465have already seen that the 'git log' command can list those commits.
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400466Note that first line of each git log entry also gives a name for the
467commit:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500468
469-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400470$ 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.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500476-------------------------------------
477
Thomas Rast0b444cd2010-01-10 00:33:00 +0100478We can give this name to 'git show' to see the details about this
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400479commit.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500480
481-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400482$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
J. Bruce Fields927a5032006-01-22 23:57:25 -0500483-------------------------------------
484
Junio C Hamanoc1d179f2007-01-03 08:38:01 -0800485But there are other ways to refer to commits. You can use any initial
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400486part of the name that is long enough to uniquely identify the commit:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500487
488-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400489$ 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
J. Bruce Fields927a5032006-01-22 23:57:25 -0500493-------------------------------------
494
Santi Béjar9c9410e2007-01-03 13:53:27 +0100495Every commit usually has one "parent" commit
496which points to the previous state of the project:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500497
498-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400499$ 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
J. Bruce Fields927a5032006-01-22 23:57:25 -0500502-------------------------------------
503
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400504Note that merge commits may have more than one parent:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500505
506-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400507$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
508$ git show HEAD^2 # show the second parent of HEAD
J. Bruce Fields927a5032006-01-22 23:57:25 -0500509-------------------------------------
510
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400511You can also give commits names of your own; after running
J. Bruce Fields927a5032006-01-22 23:57:25 -0500512
513-------------------------------------
Jonathan Niederb1889c32008-06-30 01:09:04 -0500514$ git tag v2.5 1b2e1d63ff
J. Bruce Fields927a5032006-01-22 23:57:25 -0500515-------------------------------------
516
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400517you 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
J. Bruce Fields927a5032006-01-22 23:57:25 -0500519version), you should create a "tag" object, and perhaps sign it; see
Dan McGee5162e692007-12-29 00:20:38 -0600520linkgit:git-tag[1] for details.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500521
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100522Any Git command that needs to know a commit can take any of these
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400523names. For example:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500524
525-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400526$ 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
Francis Daly37425062006-06-07 13:56:45 +0100530 # directory to its state at HEAD^
J. Bruce Fields927a5032006-01-22 23:57:25 -0500531-------------------------------------
532
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400533Be 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
Thomas Rast0b444cd2010-01-10 00:33:00 +0100536commits, they will be lost. Also, don't use 'git reset' on a
Junio C Hamanoa9d18362007-02-02 22:40:49 -0800537publicly-visible branch that other developers pull from, as it will
538force needless merges on other developers to clean up the history.
Thomas Rast0b444cd2010-01-10 00:33:00 +0100539If you need to undo changes that you have pushed, use 'git revert'
Robin Rosenberg6e2e1cf2007-02-04 17:16:39 +0100540instead.
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400541
Thomas Rast0b444cd2010-01-10 00:33:00 +0100542The 'git grep' command can search for strings in any version of your
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400543project, so
544
545-------------------------------------
546$ git grep "hello" v2.5
547-------------------------------------
548
Horst H. von Brandabda1ef2006-06-03 16:27:26 -0400549searches for all occurrences of "hello" in v2.5.
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400550
Thomas Rast0b444cd2010-01-10 00:33:00 +0100551If you leave out the commit name, 'git grep' will search any of the
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400552files it manages in your current directory. So
553
554-------------------------------------
555$ git grep "hello"
556-------------------------------------
557
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100558is a quick way to search just the files that are tracked by Git.
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400559
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100560Many Git commands also take sets of commits, which can be specified
Thomas Rast0b444cd2010-01-10 00:33:00 +0100561in a number of ways. Here are some examples with 'git log':
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400562
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
Thomas Rast0b444cd2010-01-10 00:33:00 +0100571You can also give 'git log' a "range" of commits where the first is not
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400572necessarily an ancestor of the second; for example, if the tips of
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300573the branches "stable" and "master" diverged from a common
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400574commit some time ago, then
575
576-------------------------------------
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300577$ git log stable..master
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400578-------------------------------------
579
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300580will list commits made in the master branch but not in the
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400581stable branch, while
582
583-------------------------------------
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300584$ git log master..stable
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400585-------------------------------------
586
587will show the list of commits made on the stable branch but not
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300588the master branch.
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400589
Thomas Rast0b444cd2010-01-10 00:33:00 +0100590The 'git log' command has a weakness: it must present commits in a
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400591list. When the history has lines of development that diverged and
Thomas Rast0b444cd2010-01-10 00:33:00 +0100592then merged back together, the order in which 'git log' presents
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400593those commits is meaningless.
594
Henrik Austadc7719fb2009-01-05 16:25:36 +0100595Most projects with multiple contributors (such as the Linux kernel,
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100596or Git itself) have frequent merges, and 'gitk' does a better job of
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400597visualizing 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
J. Bruce Fields2be1bc42006-05-29 19:31:32 -0400604that modified files under the "drivers" directory. (Note: you can
605adjust gitk's fonts by holding down the control key while pressing
606"-" or "+".)
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400607
608Finally, most commands that take filenames will optionally allow you
609to precede any filename by a commit, to specify a particular version
J. Bruce Fields38573862006-05-29 19:31:33 -0400610of the file:
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400611
612-------------------------------------
613$ git diff v2.5:Makefile HEAD:Makefile.in
614-------------------------------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500615
Thomas Rast0b444cd2010-01-10 00:33:00 +0100616You can also use 'git show' to see any such file:
J. Bruce Fields38573862006-05-29 19:31:33 -0400617
618-------------------------------------
Santi Béjar9c9410e2007-01-03 13:53:27 +0100619$ git show v2.5:Makefile
J. Bruce Fields38573862006-05-29 19:31:33 -0400620-------------------------------------
621
J. Bruce Fields927a5032006-01-22 23:57:25 -0500622Next Steps
623----------
624
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400625This tutorial should be enough to perform basic distributed revision
626control for your projects. However, to fully understand the depth
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100627and power of Git you need to understand two simple ideas on which it
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400628is based:
629
630 * 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
Jonathan Nieder6998e4d2008-06-30 17:01:21 -0500638Part two of this tutorial explains the object
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400639database, the index file, and a few other odds and ends that you'll
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100640need to make the most of Git. You can find it at linkgit:gittutorial-2[7].
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400641
J. Bruce Fieldscd50aba2007-05-17 23:56:08 -0400642If you don't want to continue with that right away, a few other
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400643digressions that may be interesting at this point are:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500644
Dan McGee5162e692007-12-29 00:20:38 -0600645 * linkgit:git-format-patch[1], linkgit:git-am[1]: These convert
J. Bruce Fields927a5032006-01-22 23:57:25 -0500646 series of git commits into emailed patches, and vice versa,
Henrik Austadc7719fb2009-01-05 16:25:36 +0100647 useful for projects such as the Linux kernel which rely heavily
J. Bruce Fields927a5032006-01-22 23:57:25 -0500648 on emailed patches.
649
Dan McGee5162e692007-12-29 00:20:38 -0600650 * linkgit:git-bisect[1]: When there is a regression in your
J. Bruce Fields927a5032006-01-22 23:57:25 -0500651 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
Thomas Rast801a0112009-06-06 15:11:07 +0200657 * linkgit:gitworkflows[7]: Gives an overview of recommended
658 workflows.
659
Philip Oakley673151a2014-10-10 22:25:37 +0100660 * linkgit:giteveryday[7]: Everyday Git with 20 Commands Or So.
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400661
Jonathan Nieder6998e4d2008-06-30 17:01:21 -0500662 * linkgit:gitcvs-migration[7]: Git for CVS users.
Christian Couderb27a23e2008-05-24 20:56:44 +0200663
664SEE ALSO
665--------
666linkgit:gittutorial-2[7],
667linkgit:gitcvs-migration[7],
Christian Couder497c8332008-05-29 19:21:46 +0200668linkgit:gitcore-tutorial[7],
669linkgit:gitglossary[7],
Christian Couder6e702c22008-11-17 16:43:04 +0100670linkgit:git-help[1],
Thomas Rast801a0112009-06-06 15:11:07 +0200671linkgit:gitworkflows[7],
Philip Oakley673151a2014-10-10 22:25:37 +0100672linkgit:giteveryday[7],
Christian Couderb27a23e2008-05-24 20:56:44 +0200673link:user-manual.html[The Git User's Manual]
674
675GIT
676---
Stefan Beller941b9c52017-02-08 17:29:30 -0800677Part of the linkgit:git[1] suite