blob: 4759408788070baba26b94e91dde74a9e6c6dda5 [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
Martin Ågrenad353d72023-04-15 19:29:11 +020052Assume 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
Martin Ågrenad353d72023-04-15 19:29:11 +020068directory created, named `.git`.
J. Bruce Fields93f9cc62007-05-18 00:51:42 -040069
Thomas Ackermann2de9b712013-01-21 20:17:53 +010070Next, tell Git to take a snapshot of the contents of all files under the
Martin Ågrenad353d72023-04-15 19:29:11 +020071current 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
Martin Ågrenad353d72023-04-15 19:29:11 +020079repository 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
Martin Ågrenad353d72023-04-15 19:29:11 +020098using `git diff` with the `--cached` option:
J. Bruce Fields93f9cc62007-05-18 00:51:42 -040099
100------------------------------------------------
101$ git diff --cached
102------------------------------------------------
103
Martin Ågrenad353d72023-04-15 19:29:11 +0200104(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
Martin Ågrenad353d72023-04-15 19:29:11 +0200106summary 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:
Nguyễn Thái Ngọc Duy80f537f2019-04-25 16:45:58 +0700112 (use "git restore --staged <file>..." to unstage)
Stefan Naewe89428212014-11-13 10:40:07 +0000113
114 modified: file1
115 modified: file2
116 modified: file3
117
J. Bruce Fields93f9cc62007-05-18 00:51:42 -0400118------------------------------------------------
119
120If you need to make any further adjustments, do so now, and then add any
121newly modified content to the index. Finally, commit your changes with:
122
123------------------------------------------------
Junio C Hamanoc1d179f2007-01-03 08:38:01 -0800124$ git commit
J. Bruce Fields927a5032006-01-22 23:57:25 -0500125------------------------------------------------
126
Fred Maranhão2feaf4e2008-06-11 19:09:48 -0400127This will again prompt you for a message describing the change, and then
J. Bruce Fields93f9cc62007-05-18 00:51:42 -0400128record a new version of the project.
J. Bruce Fields84dee6b2007-01-06 22:38:38 -0500129
Martin Ågrenad353d72023-04-15 19:29:11 +0200130Alternatively, instead of running `git add` beforehand, you can use
Junio C Hamano66589232006-11-29 00:17:01 -0800131
132------------------------------------------------
133$ git commit -a
134------------------------------------------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500135
J. Bruce Fields93f9cc62007-05-18 00:51:42 -0400136which will automatically notice any modified (but not new) files, add
137them to the index, and commit, all in one step.
J. Bruce Fields84dee6b2007-01-06 22:38:38 -0500138
J. Bruce Fields927a5032006-01-22 23:57:25 -0500139A note on commit messages: Though not required, it's a good idea to
谢致邦 (XIE Zhibang)1627e6b2023-10-08 15:19:26 +0000140begin the commit message with a single short (no more than 50
141characters) line summarizing the change, followed by a blank line and
142then a more thorough description. The text up to the first blank line in
143a commit message is treated as the commit title, and that title is used
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100144throughout Git. For example, linkgit:git-format-patch[1] turns a
Jeremy White52ffe992012-09-13 17:27:09 -0500145commit into email, and it uses the title on the Subject line and the
146rest of the commit in the body.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500147
Nicolas Pitre366bfcb2006-12-04 11:13:39 -0500148Git tracks content not files
149----------------------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500150
Jonathan Nieder483bc4f2008-06-30 13:56:34 -0500151Many revision control systems provide an `add` command that tells the
152system to start tracking changes to a new file. Git's `add` command
Martin Ågrenad353d72023-04-15 19:29:11 +0200153does something simpler and more powerful: `git add` is used both for new
J. Bruce Fields93f9cc62007-05-18 00:51:42 -0400154and newly modified files, and in both cases it takes a snapshot of the
155given files and stages that content in the index, ready for inclusion in
156the next commit.
Nicolas Pitre366bfcb2006-12-04 11:13:39 -0500157
J. Bruce Fields23c9ccb2007-06-10 16:20:34 -0400158Viewing project history
159-----------------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500160
161At any point you can view the history of your changes using
162
163------------------------------------------------
J. Bruce Fields67e6e5c2006-05-21 16:52:34 -0400164$ git log
J. Bruce Fields927a5032006-01-22 23:57:25 -0500165------------------------------------------------
166
167If you also want to see complete diffs at each step, use
168
169------------------------------------------------
J. Bruce Fields67e6e5c2006-05-21 16:52:34 -0400170$ git log -p
J. Bruce Fields927a5032006-01-22 23:57:25 -0500171------------------------------------------------
172
Junio C Hamanoc1d179f2007-01-03 08:38:01 -0800173Often the overview of the change is useful to get a feel of
174each step
175
176------------------------------------------------
177$ git log --stat --summary
178------------------------------------------------
179
J. Bruce Fields927a5032006-01-22 23:57:25 -0500180Managing branches
181-----------------
182
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100183A single Git repository can maintain multiple branches of
Martin Ågrenad353d72023-04-15 19:29:11 +0200184development. To create a new branch named `experimental`, use
J. Bruce Fields927a5032006-01-22 23:57:25 -0500185
186------------------------------------------------
187$ git branch experimental
188------------------------------------------------
189
190If you now run
191
192------------------------------------------------
193$ git branch
194------------------------------------------------
195
196you'll get a list of all existing branches:
197
198------------------------------------------------
199 experimental
200* master
201------------------------------------------------
202
Martin Ågrenad353d72023-04-15 19:29:11 +0200203The `experimental` branch is the one you just created, and the
204`master` branch is a default branch that was created for you
J. Bruce Fields927a5032006-01-22 23:57:25 -0500205automatically. The asterisk marks the branch you are currently on;
206type
207
208------------------------------------------------
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700209$ git switch experimental
J. Bruce Fields927a5032006-01-22 23:57:25 -0500210------------------------------------------------
211
Martin Ågrenad353d72023-04-15 19:29:11 +0200212to switch to the `experimental` branch. Now edit a file, commit the
213change, and switch back to the `master` branch:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500214
215------------------------------------------------
216(edit file)
217$ git commit -a
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700218$ git switch master
J. Bruce Fields927a5032006-01-22 23:57:25 -0500219------------------------------------------------
220
221Check that the change you made is no longer visible, since it was
Martin Ågrenad353d72023-04-15 19:29:11 +0200222made on the `experimental` branch and you're back on the `master` branch.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500223
Martin Ågrenad353d72023-04-15 19:29:11 +0200224You can make a different change on the `master` branch:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500225
226------------------------------------------------
227(edit file)
228$ git commit -a
229------------------------------------------------
230
231at this point the two branches have diverged, with different changes
Martin Ågrenad353d72023-04-15 19:29:11 +0200232made in each. To merge the changes made in `experimental` into `master`, run
J. Bruce Fields927a5032006-01-22 23:57:25 -0500233
234------------------------------------------------
Nicolas Pitrec14261e2007-01-14 22:44:18 -0500235$ git merge experimental
J. Bruce Fields927a5032006-01-22 23:57:25 -0500236------------------------------------------------
237
238If the changes don't conflict, you're done. If there are conflicts,
239markers will be left in the problematic files showing the conflict;
240
241------------------------------------------------
242$ git diff
243------------------------------------------------
244
245will show this. Once you've edited the files to resolve the
246conflicts,
247
248------------------------------------------------
249$ git commit -a
250------------------------------------------------
251
252will commit the result of the merge. Finally,
253
254------------------------------------------------
255$ gitk
256------------------------------------------------
257
258will show a nice graphical representation of the resulting history.
259
Martin Ågrenad353d72023-04-15 19:29:11 +0200260At this point you could delete the `experimental` branch with
Santi Béjar9c9410e2007-01-03 13:53:27 +0100261
262------------------------------------------------
263$ git branch -d experimental
264------------------------------------------------
265
Martin Ågrenad353d72023-04-15 19:29:11 +0200266This command ensures that the changes in the `experimental` branch are
Santi Béjar9c9410e2007-01-03 13:53:27 +0100267already in the current branch.
268
Martin Ågrenad353d72023-04-15 19:29:11 +0200269If you develop on a branch `crazy-idea`, then regret it, you can always
J. Bruce Fields927a5032006-01-22 23:57:25 -0500270delete the branch with
271
272-------------------------------------
273$ git branch -D crazy-idea
Junio C Hamanodc5f9232005-12-05 00:57:48 -0800274-------------------------------------
275
J. Bruce Fields927a5032006-01-22 23:57:25 -0500276Branches are cheap and easy, so this is a good way to try something
277out.
Junio C Hamanodc5f9232005-12-05 00:57:48 -0800278
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100279Using Git for collaboration
Junio C Hamano6f603002005-09-20 18:21:10 -0700280---------------------------
281
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100282Suppose that Alice has started a new project with a Git repository in
Martin Ågrenad353d72023-04-15 19:29:11 +0200283`/home/alice/project`, and that Bob, who has a home directory on the
J. Bruce Fields927a5032006-01-22 23:57:25 -0500284same machine, wants to contribute.
Junio C Hamano6f603002005-09-20 18:21:10 -0700285
J. Bruce Fields927a5032006-01-22 23:57:25 -0500286Bob begins with:
Junio C Hamano6f603002005-09-20 18:21:10 -0700287
J. Bruce Fields927a5032006-01-22 23:57:25 -0500288------------------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400289bob$ git clone /home/alice/project myrepo
J. Bruce Fields927a5032006-01-22 23:57:25 -0500290------------------------------------------------
Junio C Hamano6f603002005-09-20 18:21:10 -0700291
Martin Ågrenad353d72023-04-15 19:29:11 +0200292This creates a new directory `myrepo` containing a clone of Alice's
J. Bruce Fields927a5032006-01-22 23:57:25 -0500293repository. The clone is on an equal footing with the original
Horst H. von Brandabda1ef2006-06-03 16:27:26 -0400294project, possessing its own copy of the original project's history.
Junio C Hamano6f603002005-09-20 18:21:10 -0700295
J. Bruce Fields927a5032006-01-22 23:57:25 -0500296Bob then makes some changes and commits them:
Junio C Hamano6f603002005-09-20 18:21:10 -0700297
J. Bruce Fields927a5032006-01-22 23:57:25 -0500298------------------------------------------------
299(edit files)
Ian Katz5d5e88a2008-07-10 14:27:30 -0400300bob$ git commit -a
J. Bruce Fields927a5032006-01-22 23:57:25 -0500301(repeat as necessary)
302------------------------------------------------
Junio C Hamano6f603002005-09-20 18:21:10 -0700303
J. Bruce Fields927a5032006-01-22 23:57:25 -0500304When he's ready, he tells Alice to pull changes from the repository
Martin Ågrenad353d72023-04-15 19:29:11 +0200305at `/home/bob/myrepo`. She does this with:
Junio C Hamano6f603002005-09-20 18:21:10 -0700306
J. Bruce Fields927a5032006-01-22 23:57:25 -0500307------------------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400308alice$ cd /home/alice/project
309alice$ git pull /home/bob/myrepo master
J. Bruce Fields927a5032006-01-22 23:57:25 -0500310------------------------------------------------
Junio C Hamano6f603002005-09-20 18:21:10 -0700311
Martin Ågrenad353d72023-04-15 19:29:11 +0200312This merges the changes from Bob's `master` branch into Alice's
J. Bruce Fields93ee7822006-11-25 22:45:02 -0500313current branch. If Alice has made her own changes in the meantime,
Miklos Vajnac30e5672009-01-23 19:02:29 +0100314then she may need to manually fix any conflicts.
Junio C Hamano6f603002005-09-20 18:21:10 -0700315
Martin Ågrenad353d72023-04-15 19:29:11 +0200316The `pull` command thus performs two operations: it fetches changes
J. Bruce Fields93ee7822006-11-25 22:45:02 -0500317from a remote branch, then merges them into the current branch.
Junio C Hamano6f603002005-09-20 18:21:10 -0700318
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700319Note that in general, Alice would want her local changes committed before
Martin Ågrenad353d72023-04-15 19:29:11 +0200320initiating this `pull`. If Bob's work conflicts with what Alice did since
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700321their histories forked, Alice will use her working tree and the index to
322resolve conflicts, and existing local changes will interfere with the
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100323conflict resolution process (Git will still perform the fetch but will
Andrei Rybakf0b92242021-07-29 13:02:52 +0200324refuse to merge -- Alice will have to get rid of her local changes in
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700325some way and pull again when this happens).
326
Martin Ågrenad353d72023-04-15 19:29:11 +0200327Alice can peek at what Bob did without merging first, using the `fetch`
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700328command; this allows Alice to inspect what Bob did, using a special
Martin Ågrenad353d72023-04-15 19:29:11 +0200329symbol `FETCH_HEAD`, in order to determine if he has anything worth
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700330pulling, like this:
331
332------------------------------------------------
333alice$ git fetch /home/bob/myrepo master
Paolo Ciarrocchi53d15892008-08-28 14:23:52 +0200334alice$ git log -p HEAD..FETCH_HEAD
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700335------------------------------------------------
336
337This operation is safe even if Alice has uncommitted local changes.
Martin Ågrenad353d72023-04-15 19:29:11 +0200338The range notation `HEAD..FETCH_HEAD` means "show everything that is reachable
339from the `FETCH_HEAD` but exclude anything that is reachable from `HEAD`".
340Alice already knows everything that leads to her current state (`HEAD`),
341and reviews what Bob has in his state (`FETCH_HEAD`) that she has not
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300342seen with this command.
Paolo Ciarrocchi53d15892008-08-28 14:23:52 +0200343
344If Alice wants to visualize what Bob did since their histories forked
345she can issue the following command:
346
347------------------------------------------------
348$ gitk HEAD..FETCH_HEAD
349------------------------------------------------
350
Martin Ågrenad353d72023-04-15 19:29:11 +0200351This uses the same two-dot range notation we saw earlier with `git log`.
Paolo Ciarrocchi53d15892008-08-28 14:23:52 +0200352
353Alice may want to view what both of them did since they forked.
354She can use three-dot form instead of the two-dot form:
355
356------------------------------------------------
357$ gitk HEAD...FETCH_HEAD
358------------------------------------------------
359
360This means "show everything that is reachable from either one, but
361exclude anything that is reachable from both of them".
362
Martin Ågrenad353d72023-04-15 19:29:11 +0200363Please note that these range notation can be used with both `gitk`
364and `git log`.
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700365
366After inspecting what Bob did, if there is nothing urgent, Alice may
367decide to continue working without pulling from Bob. If Bob's history
368does have something Alice would immediately need, Alice may choose to
Martin Ågrenad353d72023-04-15 19:29:11 +0200369stash her work-in-progress first, do a `pull`, and then finally unstash
Junio C Hamanodc29bc82008-07-10 14:01:57 -0700370her work-in-progress on top of the resulting history.
371
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100372When you are working in a small closely knit group, it is not
373unusual to interact with the same repository over and over
374again. By defining 'remote' repository shorthand, you can make
375it easier:
376
377------------------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400378alice$ git remote add bob /home/bob/myrepo
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100379------------------------------------------------
380
Martin Ågrenad353d72023-04-15 19:29:11 +0200381With this, Alice can perform the first part of the `pull` operation
382alone using the `git fetch` command without merging them with her own
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300383branch, using:
Junio C Hamano6f603002005-09-20 18:21:10 -0700384
J. Bruce Fields927a5032006-01-22 23:57:25 -0500385-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400386alice$ git fetch bob
J. Bruce Fields927a5032006-01-22 23:57:25 -0500387-------------------------------------
Junio C Hamano6f603002005-09-20 18:21:10 -0700388
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100389Unlike the longhand form, when Alice fetches from Bob using a
Martin Ågrenad353d72023-04-15 19:29:11 +0200390remote repository shorthand set up with `git remote`, what was
Matthieu Moy0e615b22010-11-02 16:31:20 +0100391fetched is stored in a remote-tracking branch, in this case
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100392`bob/master`. So after this:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500393
394-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400395alice$ git log -p master..bob/master
J. Bruce Fields927a5032006-01-22 23:57:25 -0500396-------------------------------------
397
398shows a list of all the changes that Bob made since he branched from
Martin Ågrenad353d72023-04-15 19:29:11 +0200399Alice's `master` branch.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500400
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100401After examining those changes, Alice
Martin Ågrenad353d72023-04-15 19:29:11 +0200402could merge the changes into her `master` branch:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500403
404-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400405alice$ git merge bob/master
J. Bruce Fields927a5032006-01-22 23:57:25 -0500406-------------------------------------
407
Matthieu Moy60109d02010-11-02 16:31:21 +0100408This `merge` can also be done by 'pulling from her own remote-tracking
409branch', like this:
J. Bruce Fields93ee7822006-11-25 22:45:02 -0500410
411-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400412alice$ git pull . remotes/bob/master
J. Bruce Fields93ee7822006-11-25 22:45:02 -0500413-------------------------------------
414
Junio C Hamanoc1ff2842007-01-17 01:10:14 +0100415Note that git pull always merges into the current branch,
Brian Hetro02783072007-08-23 20:44:13 -0400416regardless of what else is given on the command line.
J. Bruce Fields93ee7822006-11-25 22:45:02 -0500417
J. Bruce Fields927a5032006-01-22 23:57:25 -0500418Later, Bob can update his repo with Alice's latest changes using
419
420-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400421bob$ git pull
J. Bruce Fields927a5032006-01-22 23:57:25 -0500422-------------------------------------
423
424Note that he doesn't need to give the path to Alice's repository;
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100425when Bob cloned Alice's repository, Git stored the location of her
J. Bruce Fieldsd66409f2006-12-31 18:47:38 -0500426repository in the repository configuration, and that location is
427used for pulls:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500428
429-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400430bob$ git config --get remote.origin.url
Alecs King8960b5a2007-07-06 00:21:16 +0800431/home/alice/project
J. Bruce Fields927a5032006-01-22 23:57:25 -0500432-------------------------------------
433
Martin Ågrenad353d72023-04-15 19:29:11 +0200434(The complete configuration created by `git clone` is visible using
Jonathan Nieder483bc4f2008-06-30 13:56:34 -0500435`git config -l`, and the linkgit:git-config[1] man page
J. Bruce Fieldsd66409f2006-12-31 18:47:38 -0500436explains the meaning of each option.)
437
Martin Ågrenad353d72023-04-15 19:29:11 +0200438Git also keeps a pristine copy of Alice's `master` branch under the
439name `origin/master`:
J. Bruce Fieldsd66409f2006-12-31 18:47:38 -0500440
441-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400442bob$ git branch -r
J. Bruce Fieldsd66409f2006-12-31 18:47:38 -0500443 origin/master
444-------------------------------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500445
446If Bob later decides to work from a different host, he can still
447perform clones and pulls using the ssh protocol:
448
449-------------------------------------
Ian Katz5d5e88a2008-07-10 14:27:30 -0400450bob$ git clone alice.org:/home/alice/project myrepo
J. Bruce Fields927a5032006-01-22 23:57:25 -0500451-------------------------------------
452
Jeff King0d0bac62016-01-30 02:21:26 -0500453Alternatively, Git has a native protocol, or can use http;
Dan McGee5162e692007-12-29 00:20:38 -0600454see linkgit:git-pull[1] for details.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500455
456Git can also be used in a CVS-like mode, with a central repository
Dan McGee5162e692007-12-29 00:20:38 -0600457that various users push changes to; see linkgit:git-push[1] and
Jonathan Nieder6998e4d2008-06-30 17:01:21 -0500458linkgit:gitcvs-migration[7].
J. Bruce Fields927a5032006-01-22 23:57:25 -0500459
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400460Exploring history
461-----------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500462
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400463Git history is represented as a series of interrelated commits. We
Martin Ågrenad353d72023-04-15 19:29:11 +0200464have already seen that the `git log` command can list those commits.
465Note that first line of each `git log` entry also gives a name for the
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400466commit:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500467
468-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400469$ git log
470commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
471Author: Junio C Hamano <junkio@cox.net>
472Date: Tue May 16 17:18:22 2006 -0700
473
474 merge-base: Clarify the comments on post processing.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500475-------------------------------------
476
Martin Ågrenad353d72023-04-15 19:29:11 +0200477We can give this name to `git show` to see the details about this
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400478commit.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500479
480-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400481$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
J. Bruce Fields927a5032006-01-22 23:57:25 -0500482-------------------------------------
483
Junio C Hamanoc1d179f2007-01-03 08:38:01 -0800484But there are other ways to refer to commits. You can use any initial
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400485part of the name that is long enough to uniquely identify the commit:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500486
487-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400488$ git show c82a22c39c # the first few characters of the name are
489 # usually enough
490$ git show HEAD # the tip of the current branch
491$ git show experimental # the tip of the "experimental" branch
J. Bruce Fields927a5032006-01-22 23:57:25 -0500492-------------------------------------
493
Santi Béjar9c9410e2007-01-03 13:53:27 +0100494Every commit usually has one "parent" commit
495which points to the previous state of the project:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500496
497-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400498$ git show HEAD^ # to see the parent of HEAD
499$ git show HEAD^^ # to see the grandparent of HEAD
500$ git show HEAD~4 # to see the great-great grandparent of HEAD
J. Bruce Fields927a5032006-01-22 23:57:25 -0500501-------------------------------------
502
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400503Note that merge commits may have more than one parent:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500504
505-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400506$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
507$ git show HEAD^2 # show the second parent of HEAD
J. Bruce Fields927a5032006-01-22 23:57:25 -0500508-------------------------------------
509
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400510You can also give commits names of your own; after running
J. Bruce Fields927a5032006-01-22 23:57:25 -0500511
512-------------------------------------
Jonathan Niederb1889c32008-06-30 01:09:04 -0500513$ git tag v2.5 1b2e1d63ff
J. Bruce Fields927a5032006-01-22 23:57:25 -0500514-------------------------------------
515
Martin Ågrenad353d72023-04-15 19:29:11 +0200516you can refer to `1b2e1d63ff` by the name `v2.5`. If you intend to
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400517share this name with other people (for example, to identify a release
J. Bruce Fields927a5032006-01-22 23:57:25 -0500518version), you should create a "tag" object, and perhaps sign it; see
Dan McGee5162e692007-12-29 00:20:38 -0600519linkgit:git-tag[1] for details.
J. Bruce Fields927a5032006-01-22 23:57:25 -0500520
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100521Any Git command that needs to know a commit can take any of these
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400522names. For example:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500523
524-------------------------------------
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400525$ git diff v2.5 HEAD # compare the current HEAD to v2.5
526$ git branch stable v2.5 # start a new branch named "stable" based
527 # at v2.5
528$ git reset --hard HEAD^ # reset your current branch and working
Francis Daly37425062006-06-07 13:56:45 +0100529 # directory to its state at HEAD^
J. Bruce Fields927a5032006-01-22 23:57:25 -0500530-------------------------------------
531
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400532Be careful with that last command: in addition to losing any changes
533in the working directory, it will also remove all later commits from
534this branch. If this branch is the only branch containing those
Martin Ågrenad353d72023-04-15 19:29:11 +0200535commits, they will be lost. Also, don't use `git reset` on a
Junio C Hamanoa9d18362007-02-02 22:40:49 -0800536publicly-visible branch that other developers pull from, as it will
537force needless merges on other developers to clean up the history.
Martin Ågrenad353d72023-04-15 19:29:11 +0200538If you need to undo changes that you have pushed, use `git revert`
Robin Rosenberg6e2e1cf2007-02-04 17:16:39 +0100539instead.
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400540
Martin Ågrenad353d72023-04-15 19:29:11 +0200541The `git grep` command can search for strings in any version of your
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400542project, so
543
544-------------------------------------
545$ git grep "hello" v2.5
546-------------------------------------
547
Martin Ågrenad353d72023-04-15 19:29:11 +0200548searches for all occurrences of "hello" in `v2.5`.
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400549
Martin Ågrenad353d72023-04-15 19:29:11 +0200550If you leave out the commit name, `git grep` will search any of the
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400551files it manages in your current directory. So
552
553-------------------------------------
554$ git grep "hello"
555-------------------------------------
556
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100557is a quick way to search just the files that are tracked by Git.
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400558
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100559Many Git commands also take sets of commits, which can be specified
Martin Ågrenad353d72023-04-15 19:29:11 +0200560in a number of ways. Here are some examples with `git log`:
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400561
562-------------------------------------
563$ git log v2.5..v2.6 # commits between v2.5 and v2.6
564$ git log v2.5.. # commits since v2.5
565$ git log --since="2 weeks ago" # commits from the last 2 weeks
566$ git log v2.5.. Makefile # commits since v2.5 which modify
567 # Makefile
568-------------------------------------
569
Martin Ågrenad353d72023-04-15 19:29:11 +0200570You can also give `git log` a "range" of commits where the first is not
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400571necessarily an ancestor of the second; for example, if the tips of
Martin Ågrenad353d72023-04-15 19:29:11 +0200572the branches `stable` and `master` diverged from a common
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400573commit some time ago, then
574
575-------------------------------------
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300576$ git log stable..master
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400577-------------------------------------
578
Martin Ågrenad353d72023-04-15 19:29:11 +0200579will list commits made in the `master` branch but not in the
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400580stable branch, while
581
582-------------------------------------
Thadeu Lima de Souza Cascardo21d777f2009-06-29 12:13:58 -0300583$ git log master..stable
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400584-------------------------------------
585
586will show the list of commits made on the stable branch but not
Martin Ågrenad353d72023-04-15 19:29:11 +0200587the `master` branch.
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400588
Martin Ågrenad353d72023-04-15 19:29:11 +0200589The `git log` command has a weakness: it must present commits in a
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400590list. When the history has lines of development that diverged and
Martin Ågrenad353d72023-04-15 19:29:11 +0200591then merged back together, the order in which `git log` presents
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400592those commits is meaningless.
593
Henrik Austadc7719fb2009-01-05 16:25:36 +0100594Most projects with multiple contributors (such as the Linux kernel,
Martin Ågrenad353d72023-04-15 19:29:11 +0200595or Git itself) have frequent merges, and `gitk` does a better job of
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400596visualizing their history. For example,
597
598-------------------------------------
599$ gitk --since="2 weeks ago" drivers/
600-------------------------------------
601
602allows you to browse any commits from the last 2 weeks of commits
Martin Ågrenad353d72023-04-15 19:29:11 +0200603that modified files under the `drivers` directory. (Note: you can
J. Bruce Fields2be1bc42006-05-29 19:31:32 -0400604adjust gitk's fonts by holding down the control key while pressing
605"-" or "+".)
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400606
607Finally, most commands that take filenames will optionally allow you
608to precede any filename by a commit, to specify a particular version
J. Bruce Fields38573862006-05-29 19:31:33 -0400609of the file:
J. Bruce Fieldsf1fe3842006-05-21 16:54:05 -0400610
611-------------------------------------
612$ git diff v2.5:Makefile HEAD:Makefile.in
613-------------------------------------
J. Bruce Fields927a5032006-01-22 23:57:25 -0500614
Martin Ågrenad353d72023-04-15 19:29:11 +0200615You can also use `git show` to see any such file:
J. Bruce Fields38573862006-05-29 19:31:33 -0400616
617-------------------------------------
Santi Béjar9c9410e2007-01-03 13:53:27 +0100618$ git show v2.5:Makefile
J. Bruce Fields38573862006-05-29 19:31:33 -0400619-------------------------------------
620
J. Bruce Fields927a5032006-01-22 23:57:25 -0500621Next Steps
622----------
623
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400624This tutorial should be enough to perform basic distributed revision
625control for your projects. However, to fully understand the depth
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100626and power of Git you need to understand two simple ideas on which it
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400627is based:
628
629 * The object database is the rather elegant system used to
630 store the history of your project--files, directories, and
631 commits.
632
633 * The index file is a cache of the state of a directory tree,
634 used to create commits, check out working directories, and
635 hold the various trees involved in a merge.
636
Jonathan Nieder6998e4d2008-06-30 17:01:21 -0500637Part two of this tutorial explains the object
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400638database, the index file, and a few other odds and ends that you'll
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100639need to make the most of Git. You can find it at linkgit:gittutorial-2[7].
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400640
J. Bruce Fieldscd50aba2007-05-17 23:56:08 -0400641If you don't want to continue with that right away, a few other
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400642digressions that may be interesting at this point are:
J. Bruce Fields927a5032006-01-22 23:57:25 -0500643
Dan McGee5162e692007-12-29 00:20:38 -0600644 * linkgit:git-format-patch[1], linkgit:git-am[1]: These convert
J. Bruce Fields927a5032006-01-22 23:57:25 -0500645 series of git commits into emailed patches, and vice versa,
Henrik Austadc7719fb2009-01-05 16:25:36 +0100646 useful for projects such as the Linux kernel which rely heavily
J. Bruce Fields927a5032006-01-22 23:57:25 -0500647 on emailed patches.
648
Dan McGee5162e692007-12-29 00:20:38 -0600649 * linkgit:git-bisect[1]: When there is a regression in your
J. Bruce Fields927a5032006-01-22 23:57:25 -0500650 project, one way to track down the bug is by searching through
Martin Ågrenad353d72023-04-15 19:29:11 +0200651 the history to find the exact commit that's to blame. `git bisect`
J. Bruce Fields927a5032006-01-22 23:57:25 -0500652 can help you perform a binary search for that commit. It is
653 smart enough to perform a close-to-optimal search even in the
654 case of complex non-linear history with lots of merged branches.
655
Thomas Rast801a0112009-06-06 15:11:07 +0200656 * linkgit:gitworkflows[7]: Gives an overview of recommended
657 workflows.
658
Philip Oakley673151a2014-10-10 22:25:37 +0100659 * linkgit:giteveryday[7]: Everyday Git with 20 Commands Or So.
J. Bruce Fieldse31952d2006-05-21 19:49:34 -0400660
Jonathan Nieder6998e4d2008-06-30 17:01:21 -0500661 * linkgit:gitcvs-migration[7]: Git for CVS users.
Christian Couderb27a23e2008-05-24 20:56:44 +0200662
663SEE ALSO
664--------
665linkgit:gittutorial-2[7],
666linkgit:gitcvs-migration[7],
Christian Couder497c8332008-05-29 19:21:46 +0200667linkgit:gitcore-tutorial[7],
668linkgit:gitglossary[7],
Christian Couder6e702c22008-11-17 16:43:04 +0100669linkgit:git-help[1],
Thomas Rast801a0112009-06-06 15:11:07 +0200670linkgit:gitworkflows[7],
Philip Oakley673151a2014-10-10 22:25:37 +0100671linkgit:giteveryday[7],
Christian Couderb27a23e2008-05-24 20:56:44 +0200672link:user-manual.html[The Git User's Manual]
673
674GIT
675---
Stefan Beller941b9c52017-02-08 17:29:30 -0800676Part of the linkgit:git[1] suite