You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/book/01-introduction/chapter1.asc
+53-70Lines changed: 53 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,24 +21,22 @@ Many people’s version-control method of choice is to copy files into another d
21
21
This approach is very common because it is so simple, but it is also incredibly error prone.
22
22
It is easy to forget which directory you’re in and accidentally write to the wrong file or copy over files you don’t mean to.
23
23
24
-
To deal with this issue, programmers long ago developed local VCSs that had a simple database that kept all the changes to files under revision control (see <<local_version_control>>).
24
+
To deal with this issue, programmers long ago developed local VCSs that had a simple database that kept all the changes to files under revision control.
25
25
26
-
[[local_version_control]]
27
26
.Local version control.
28
27
image::images/18333fig0101-tn.png[Local version control diagram]
29
28
30
-
One of the more popular VCS tools was a system called rcs, which is still distributed with many computers today.
31
-
Even the popular Mac OS X operating system includes the rcs command when you install the Developer Tools.
32
-
This tool basically works by keeping patch sets (that is, the differences between files) from one change to another in a special format on disk; it can then re-create what any file looked like at any point in time by adding up all the patches.
29
+
One of the more popular VCS tools was a system called RCS, which is still distributed with many computers today.
30
+
Even the popular Mac OS X operating system includes the `rcs` command when you install the Developer Tools.
31
+
RCS works by keeping patch sets (that is, the differences between files) in a special format on disk; it can then re-create what any file looked like at any point in time by adding up all the patches.
33
32
34
33
==== Centralized Version Control Systems (((version control,centralized version control)))
35
34
36
35
The next major issue that people encounter is that they need to collaborate with developers on other systems.
37
36
To deal with this problem, Centralized Version Control Systems (CVCSs) were developed.
38
37
These systems, such as CVS, Subversion, and Perforce, have a single server that contains all the versioned files, and a number of clients that check out files from that central place.
39
-
For many years, this has been the standard for version control (see <<central_version_control>>).
38
+
For many years, this has been the standard for version control.
40
39
41
-
[[central_version_control]]
42
40
.Centralized version control.
43
41
image::images/18333fig0102-tn.png[Centralized version control diagram]
44
42
@@ -49,17 +47,16 @@ Administrators have fine-grained control over who can do what; and it’s far ea
49
47
However, this setup also has some serious downsides.
50
48
The most obvious is the single point of failure that the centralized server represents.
51
49
If that server goes down for an hour, then during that hour nobody can collaborate at all or save versioned changes to anything they’re working on.
52
-
If the hard disk the central database is on becomes corrupted, and proper backups haven’t been kept, you lose absolutely everything–the entire history of the project except whatever single snapshots people happen to have on their local machines.
53
-
Local VCS systems suffer from this same problem–whenever you have the entire history of the project in a single place, you risk losing everything.
50
+
If the hard disk the central database is on becomes corrupted, and proper backups haven’t been kept, you lose absolutely everything – the entire history of the project except whatever single snapshots people happen to have on their local machines.
51
+
Local VCS systems suffer from this same problem – whenever you have the entire history of the project in a single place, you risk losing everything.
54
52
55
53
==== Distributed Version Control Systems (((version control,distributed version control)))
56
54
57
55
This is where Distributed Version Control Systems (DVCSs) step in.
58
56
In a DVCS (such as Git, Mercurial, Bazaar or Darcs), clients don’t just check out the latest snapshot of the files: they fully mirror the repository.
59
57
Thus if any server dies, and these systems were collaborating via it, any of the client repositories can be copied back up to the server to restore it.
60
-
Every checkout is really a full backup of all the data (see <<dist_version_control>>).
58
+
Every checkout is really a full backup of all the data.
61
59
62
-
[[dist_version_control]]
63
60
.Distributed version control.
64
61
image::images/18333fig0103-tn.png[Distributed version control diagram]
65
62
@@ -69,9 +66,10 @@ This allows you to set up several types of workflows that aren’t possible in c
69
66
=== A Short History of Git
70
67
71
68
As with many great things in life, Git began with a bit of creative destruction and fiery controversy.
69
+
72
70
The Linux kernel is an open source software project of fairly large scope.
73
71
For most of the lifetime of the Linux kernel maintenance (1991–2002), changes to the software were passed around as patches and archived files.
74
-
In 2002, the Linux kernel project began using a proprietary DVCS system called BitKeeper.
72
+
In 2002, the Linux kernel project began using a proprietary DVCS called BitKeeper.
75
73
76
74
In 2005, the relationship between the community that developed the Linux kernel and the commercial company that developed BitKeeper broke down, and the tool’s free-of-charge status was revoked.
77
75
This prompted the Linux development community (and in particular Linus Torvalds, the creator of Linux) to develop their own tool based on some of the lessons they learned while using BitKeeper.
@@ -91,26 +89,24 @@ It’s incredibly fast, it’s very efficient with large projects, and it has an
91
89
So, what is Git in a nutshell?
92
90
This is an important section to absorb, because if you understand what Git is and the fundamentals of how it works, then using Git effectively will probably be much easier for you.
93
91
As you learn Git, try to clear your mind of the things you may know about other VCSs, such as Subversion and Perforce; doing so will help you avoid subtle confusion when using the tool.
94
-
Git stores and thinks about information much differently than these other systems, even though the user interface is fairly similar; understanding those differences will help prevent you from becoming confused while using it.
92
+
Git stores and thinks about information much differently than these other systems, even though the user interface is fairly similar, and understanding those differences will help prevent you from becoming confused while using it.
95
93
96
94
==== Snapshots, Not Differences
97
95
98
96
The major difference between Git and any other VCS (Subversion and friends included) is the way Git thinks about its data.
99
97
Conceptually, most other systems store information as a list of file-based changes.
100
-
These systems (CVS, Subversion, Perforce, Bazaar, and so on) think of the information they keep as a set of files and the changes made to each file over time, as illustrated in <<stream_of_deltas>>.
98
+
These systems (CVS, Subversion, Perforce, Bazaar, and so on) think of the information they keep as a set of files and the changes made to each file over time.
101
99
102
-
[[stream_of_deltas]]
103
-
.Other systems tend to store data as changes to a base version of each file.
104
-
image::images/18333fig0104-tn.png[Other systems tend to store data as changes to a base version of each file.]
100
+
.Storing data as changes to a base version of each file.
101
+
image::images/18333fig0104-tn.png[Storing data as changes to a base version of each file.]
105
102
106
103
Git doesn’t think of or store its data this way.
107
-
Instead, Git thinks of its data more like a set of snapshots of a mini filesystem.
104
+
Instead, Git thinks of its data more like a set of snapshots of a miniature filesystem.
108
105
Every time you commit, or save the state of your project in Git, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot.
109
-
To be efficient, if files have not changed, Git doesn’t store the file again–just a link to the previous identical file it has already stored.
110
-
Git thinks about its data more like <<stream_of_snapshots>>.
106
+
To be efficient, if files have not changed, Git doesn’t store the file again, just a link to the previous identical file it has already stored.
107
+
Git thinks about its data more like a *stream of snapshots*.
111
108
112
-
[[stream_of_snapshots]]
113
-
.Git stores data as snapshots of the project over time.
109
+
.Storing data as snapshots of the project over time.
114
110
image::images/18333fig0105-tn.png[Git stores data as snapshots of the project over time.]
115
111
116
112
This is an important distinction between Git and nearly all other VCSs.
@@ -124,7 +120,7 @@ Most operations in Git only need local files and resources to operate – genera
124
120
If you’re used to a CVCS where most operations have that network latency overhead, this aspect of Git will make you think that the gods of speed have blessed Git with unworldly powers.
125
121
Because you have the entire history of the project right there on your local disk, most operations seem almost instantaneous.
126
122
127
-
For example, to browse the history of the project, Git doesn’t need to go out to the server to get the history and display it for you–it simply reads it directly from your local database.
123
+
For example, to browse the history of the project, Git doesn’t need to go out to the server to get the history and display it for you – it simply reads it directly from your local database.
128
124
This means you see the project history almost instantly.
129
125
If you want to see the changes introduced between the current version of a file and the file a month ago, Git can look up the file a month ago and do a local difference calculation, instead of having to either ask a remote server to do it or pull an older version of the file from the remote server to do it locally.
130
126
@@ -180,7 +176,7 @@ This is the most important part of Git, and it is what is copied when you clone
180
176
The working directory is a single checkout of one version of the project.
181
177
These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify.
182
178
183
-
The staging area is a simple file, generally contained in your Git directory, that stores information about what will go into your next commit.
179
+
The staging area is a file, generally contained in your Git directory, that stores information about what will go into your next commit.
184
180
It’s sometimes referred to as the index, but it’s also common to refer to it as the staging area.
185
181
186
182
The basic Git workflow goes something like this:
@@ -196,19 +192,19 @@ In <<_git_basics_chapter>>, you’ll learn more about these states and how you c
196
192
197
193
=== The Command Line
198
194
199
-
There are a lot of different ways to use Git these days.
200
-
There are the original command line tools and there are many graphical user interfaces as well of varying capabilities.
195
+
There are a lot of different ways to use Git.
196
+
There are the original command line tools, and there are many graphical user interfaces of varying capabilities.
201
197
For this book, we will be using Git on the command line.
202
-
For one, the command line is the only place you can run *all* Git commands - most of the GUIs only implement some subset of Git functionality for simplicity.
198
+
For one, the command line is the only place you can run *all* Git commands – most of the GUIs only implement some subset of Git functionality for simplicity.
203
199
If you know how to run the command line version, you can probably also figure out how to run the GUI version, while the opposite is not neccesarily true.
204
-
Finally, while your choice of graphical client is a matter of personal taste, _all_ users will have the command-line tools installed and available.
200
+
Also, while your choice of graphical client is a matter of personal taste, _all_ users will have the command-line tools installed and available.
205
201
206
-
So, we will expect you to know how to open Terminal in Mac or Command Prompt or Powershell in Windows.
202
+
So we will expect you to know how to open Terminal in Mac or Command Prompt or Powershell in Windows.
207
203
If you don't know what we're talking about here, you may need to stop and research that quickly so that you can follow the rest of the examples and descriptions in this book.
208
204
209
205
=== Installing Git
210
206
211
-
Let’s get into using some Git.
207
+
Before you start using Git, you have to make it available on your computer.
212
208
Even if it's already installed, it's probably a good idea to update to the latest version.
213
209
You can either install it as a package or via another installer, or download the source code and compile it yourself.
214
210
@@ -223,9 +219,7 @@ Or if you’re on a Debian-based distribution like Ubuntu, try apt-get:
223
219
224
220
$ apt-get install git
225
221
226
-
For more options, there are instructions for installing on several different Unix flavors on the Git website:
227
-
228
-
http://git-scm.com/download/linux
222
+
For more options, there are instructions for installing on several different Unix flavors on the Git website, at http://git-scm.com/download/linux[].
229
223
230
224
==== Installing on Mac
231
225
@@ -235,50 +229,44 @@ On Mavericks (10.9) or above you can do this simply by trying to run 'git' from
235
229
If you don't have it installed already, it will prompt you to install it.
236
230
237
231
If you want a more up to date version, you can also install it via a binary installer.
238
-
An OSX Git installer is maintained and available for download here:
An OSX Git installer is maintained and available for download at the Git website, at http://git-scm.com/download/mac[].
241
233
242
-
[[git_osx_installer]]
243
234
.Git OS X Installer.
244
-
image::images/18333fig0107-tn.png[Git OS X installer.]
235
+
image::images/git-osx-installer.png[Git OS X installer.]
245
236
246
237
You can also install it as part of the GitHub for Mac install.
247
238
Their GUI Git tool has an option to install command line tools as well.
248
-
You can download that tool from the GitHub for Mac website.
249
-
250
-
http://mac.github.com/
239
+
You can download that tool from the GitHub for Mac website, at http://mac.github.com[].
251
240
252
241
==== Installing on Windows
253
242
254
243
There are also a few ways to install Git on Windows.
255
-
You can go to the Git website to have it automatically download the current MsysGit installer.
256
-
257
-
http://git-scm.com/download/win
258
-
259
-
If you want more information on the MsysGit project or build, you can also go directly to their homepage.
244
+
The most official build is available for download on the Git website.
245
+
Just go to http://git-scm.com/download/win[] and the download will start automatically.
246
+
Note that this is a project called Git for Windows (also called msysGit), which is separate from Git itself; for more information on it, go to http://msysgit.github.io/[].
260
247
261
-
http://msysgit.github.io
262
248
263
-
The GitHub for Windows installer will also install a command line version of Git as well as the GUI, so that is another easy option.
264
-
It also works well with Powershell and sets up solid credential caching options and sane CRLF settings, so many people find it a simpler installation option.
265
-
We'll learn more about both of those things a little later.
249
+
Another easy way to get Git installed is by installing GitHub for Windows.
250
+
The installer includes a command line version of Git as well as the GUI.
251
+
It also works well with Powershell, and sets up solid credential caching and sane CRLF settings.
252
+
We'll learn more about those things a little later, but suffice it to say they're things you want.
266
253
267
254
=== First-Time Git Setup
268
255
269
256
Now that you have Git on your system, you’ll want to do a few things to customize your Git environment.
270
-
You should have to do these things only once; they’ll stick around between upgrades.
257
+
You should have to do these things only once on any given computer; they’ll stick around between upgrades.
271
258
You can also change them at any time by running through the commands again.
272
259
273
260
Git comes with a tool called `git config` that lets you get and set configuration variables that control all aspects of how Git looks and operates.
274
261
These variables can be stored in three different places:
275
262
276
-
* `/etc/gitconfig` file: Contains values for every user on the system and all their repositories.
263
+
1. `/etc/gitconfig` file: Contains values for every user on the system and all their repositories.
277
264
If you pass the option` --system` to `git config`, it reads and writes from this file specifically.
278
-
* `~/.gitconfig` file: Specific to your user.
265
+
2. `~/.gitconfig` or `~/.config/git/config` file: Specific to your user.
279
266
You can make Git read and write to this file specifically by passing the `--global` option.
280
-
* `config` file in the Git directory (that is, `.git/config`) of whatever repository you’re currently using: Specific to that single repository.
281
-
Each level overrides values in the previous level, so values in `.git/config` trump those in `/etc/gitconfig`.
267
+
3. `config` file in the Git directory (that is, `.git/config`) of whatever repository you’re currently using: Specific to that single repository.
268
+
269
+
Each level overrides values in the previous level, so values in `.git/config` trump those in `/etc/gitconfig`.
282
270
283
271
On Windows systems, Git looks for the `.gitconfig` file in the `$HOME` directory (`C:\Users\$USER` for most people).
284
272
It also still looks for `/etc/gitconfig`, although it’s relative to the MSys root, which is wherever you decide to install Git on your Windows system when you run the installer.
@@ -298,22 +286,17 @@ For example, if you’re on a system that has yum (such as Fedora) or apt-get (s
298
286
libz-dev libssl-dev
299
287
300
288
When you have all the necessary dependencies, you can go ahead and grab the latest tagged release tarball from several places.
301
-
You can get it via the Kernel.org site.
302
-
303
-
https://www.kernel.org/pub/software/scm/git
304
-
305
-
Or the mirror on the GitHub web site.
306
-
307
-
https://github.com/git/git/releases
308
-
289
+
You can get it via the Kernel.org site, at https://www.kernel.org/pub/software/scm/git[], or the mirror on the GitHub web site, at https://github.com/git/git/releases[].
309
290
It's generally a little clearer what the latest version is on the GitHub page, but the kernel.org page also has release signatures if you want to verify your download.
310
291
311
292
Then, compile and install:
312
293
313
294
$ tar -zxf git-1.9.1.tar.gz
314
295
$ cd git-1.9.1
315
-
$ make prefix=/usr all doc info
316
-
$ sudo make prefix=/usr install install-doc install-html install-info
296
+
$ make configure
297
+
$ ./configure --prefix=/usr
298
+
$ make all doc info
299
+
$ sudo make install install-doc install-html install-info
317
300
318
301
After this is done, you can also get Git via Git itself for updates:
319
302
@@ -335,7 +318,7 @@ Many of the GUI tools will help you do this when you first run them as well.
335
318
==== Your Editor
336
319
337
320
Now that your identity is set up, you can configure the default text editor that will be used when Git needs you to type in a message.
338
-
By default, Git uses your system’s default editor, which is generally Vi or Vim.
321
+
By default, Git uses your system’s default editor, which is generally vi or Vim.
339
322
If you want to use a different text editor, such as Emacs, you can do the following:
340
323
341
324
$ git config --global core.editor emacs
@@ -345,8 +328,8 @@ If you want to use a different text editor, such as Emacs, you can do the follow
345
328
If you want to check your settings, you can use the `git config --list` command to list all the settings Git can find at that point:
346
329
347
330
$ git config --list
348
-
user.name=Scott Chacon
349
-
user.email=schacon@gmail.com
331
+
user.name=John Doe
332
+
user.email=johndoe@example.com
350
333
color.status=auto
351
334
color.branch=auto
352
335
color.interactive=auto
@@ -356,10 +339,10 @@ If you want to check your settings, you can use the `git config --list` command
356
339
You may see keys more than once, because Git reads the same key from different files (`/etc/gitconfig` and `~/.gitconfig`, for example).
357
340
In this case, Git uses the last value for each unique key it sees.
358
341
359
-
You can also check what Git thinks a specific key’s value is by typing `git config {key}`:
342
+
You can also check what Git thinks a specific key’s value is by typing `git config <key>`:
0 commit comments