Skip to content

Commit 1dd3a6a

Browse files
committed
various small chapter 2 fixups, formatting and tweaks
Also adds the -S option to the log filter page and removes the Reset section that was outlined here. Also changes URLs to remove the .git from the ends.
1 parent 61e52ec commit 1dd3a6a

File tree

1 file changed

+67
-72
lines changed

1 file changed

+67
-72
lines changed

book/02-git-basics/1-git-basics.asc

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ At this point, nothing in your project is tracked yet.
2626
(See <<_git_internals>> for more information about exactly what files are contained in the `.git` directory you just created.)
2727

2828
If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit.
29-
You can accomplish that with a few git add commands that specify the files you want to track, followed by a commit:
29+
You can accomplish that with a few `git add` commands that specify the files you want to track, followed by a `git commit`:
3030

3131
[source,shell]
3232
----
3333
$ git add *.c
34-
$ git add README
34+
$ git add LICENSE
3535
$ git commit -m 'initial project version'
3636
----
3737

@@ -40,33 +40,33 @@ At this point, you have a Git repository with tracked files and an initial commi
4040

4141
==== Cloning an Existing Repository
4242

43-
If you want to get a copy of an existing Git repository – for example, a project you’d like to contribute to – the command you need is git clone.
44-
If you’re familiar with other VCS systems such as Subversion, you’ll notice that the command is clone and not checkout.
45-
This is an important distinction – Git receives a copy of nearly all data that the server has.
46-
Every version of every file for the history of the project is pulled down when you run `git clone`.
47-
In fact, if your server disk gets corrupted, you can use any of the clones on any client to set the server back to the state it was in when it was cloned (you may lose some server-side hooks and such, but all the versioned data would be there – see <<_git_on_the_server>> for more details).
43+
If you want to get a copy of an existing Git repository – for example, a project you’d like to contribute to – the command you need is `git clone`.
44+
If you’re familiar with other VCS systems such as Subversion, you’ll notice that the command is "clone" and not "checkout".
45+
This is an important distinction – instead of getting just a working copy, Git receives a full copy of nearly all data that the server has.
46+
Every version of every file for the history of the project is pulled down by default when you run `git clone`.
47+
In fact, if your server disk gets corrupted, you can often use nearly any of the clones on any client to set the server back to the state it was in when it was cloned (you may lose some server-side hooks and such, but all the versioned data would be there – see <<_git_on_the_server>> for more details).
4848

4949
You clone a repository with `git clone [url]`.
50-
For example, if you want to clone the Ruby Git library called Grit, you can do so like this:
50+
For example, if you want to clone the Git linkable library called libgit2, you can do so like this:
5151

5252
[source,shell]
5353
----
54-
$ git clone https://github.com/schacon/grit.git
54+
$ git clone https://github.com/libgit2/libgit2
5555
----
5656

57-
That creates a directory named ``grit'', initializes a `.git` directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version.
58-
If you go into the new `grit` directory, you’ll see the project files in there, ready to be worked on or used.
59-
If you want to clone the repository into a directory named something other than grit, you can specify that as the next command-line option:
57+
That creates a directory named ``libgit2'', initializes a `.git` directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version.
58+
If you go into the new `libgit2` directory, you’ll see the project files in there, ready to be worked on or used.
59+
If you want to clone the repository into a directory named something other than ``libgit2'', you can specify that as the next command-line option:
6060

6161
[source,shell]
6262
----
63-
$ git clone https://github.com/schacon/grit.git mygrit
63+
$ git clone https://github.com/libgit2/libgit2 mylibgit
6464
----
6565

66-
That command does the same thing as the previous one, but the target directory is called `mygrit`.
66+
That command does the same thing as the previous one, but the target directory is called `mylibgit`.
6767

6868
Git has a number of different transfer protocols you can use.
69-
The previous example uses the `git://` protocol, but you may also see `http(s)://` or `user@server:path/to/repo.git`, which uses the SSH transfer protocol.
69+
The previous example uses the `https://` protocol, but you may also see `git://` or `user@server:path/to/repo.git`, which uses the SSH transfer protocol.
7070
<<_git_on_the_server>> will introduce all of the available options the server can set up to access your Git repository and the pros and cons of each.
7171

7272
=== Recording Changes to the Repository
@@ -89,7 +89,7 @@ image::images/18333fig0201-tn.png[The lifecycle of the status of your files.]
8989

9090
==== Checking the Status of Your Files
9191

92-
The main tool you use to determine which files are in which state is the `git status` command.
92+
The main tool you use to determine which files are in which state is the `git status` command.(((git, status)))
9393
If you run this command directly after a clone, you should see something like this:
9494

9595
[source,shell]
@@ -101,16 +101,16 @@ nothing to commit, working directory clean
101101

102102
This means you have a clean working directory – in other words, there are no tracked and modified files.
103103
Git also doesn’t see any untracked files, or they would be listed here.
104-
Finally, the command tells you which branch you’re on.
105-
For now, that is always master, which is the default; you won’t worry about it here.
106-
The next chapter will go over branches and references in detail.
104+
Finally, the command tells you which branch you’re on and informs you that it has not diverged from the same branch on the server.
105+
For now, that branch is always ``master'', which is the default; you won’t worry about it here.
106+
<<_git_branching>> will go over branches and references in detail.
107107

108108
Let’s say you add a new file to your project, a simple README file.
109109
If the file didn’t exist before, and you run `git status`, you see your untracked file like so:
110110

111111
[source,shell]
112112
----
113-
$ vim README
113+
$ echo 'My Project' > README
114114
$ git status
115115
On branch master
116116
Untracked files:
@@ -128,15 +128,15 @@ You do want to start including README, so let’s start tracking the file.
128128

129129
==== Tracking New Files
130130

131-
In order to begin tracking a new file, you use the command `git add`.
131+
In order to begin tracking a new file, you use the command `git add`.(((git, add)))
132132
To begin tracking the README file, you can run this:
133133

134134
[source,shell]
135135
----
136136
$ git add README
137137
----
138138

139-
If you run your status command again, you can see that your README file is now tracked and staged:
139+
If you run your status command again, you can see that your README file is now tracked and staged to be committed:
140140

141141
[source,shell]
142142
----
@@ -150,14 +150,14 @@ Changes to be committed:
150150
----
151151

152152
You can tell that it’s staged because it’s under the ``Changes to be committed'' heading.
153-
If you commit at this point, the version of the file at the time you ran git add is what will be in the historical snapshot.
154-
You may recall that when you ran git init earlier, you then ran git add (files) – that was to begin tracking files in your directory.
155-
The git add command takes a path name for either a file or a directory; if it’s a directory, the command adds all the files in that directory recursively.
153+
If you commit at this point, the version of the file at the time you ran `git add` is what will be in the historical snapshot.
154+
You may recall that when you ran `git init` earlier, you then ran `git add (files)` – that was to begin tracking files in your directory.(((git, init)))(((git, add)))
155+
The `git add` command takes a path name for either a file or a directory; if it’s a directory, the command adds all the files in that directory recursively.
156156

157157
==== Staging Modified Files
158158

159159
Let’s change a file that was already tracked.
160-
If you change a previously tracked file called `benchmarks.rb` and then run your `git status` command again, you get something that looks like this:
160+
If you change a previously tracked file called ``benchmarks.rb'' and then run your `git status` command again, you get something that looks like this:
161161

162162
[source,shell]
163163
----
@@ -176,9 +176,9 @@ Changes not staged for commit:
176176
177177
----
178178

179-
The `benchmarks.rb` file appears under a section named ``Changed but not staged for commit'' – which means that a file that is tracked has been modified in the working directory but not yet staged.
180-
To stage it, you run the `git add` command (it’s a multipurpose command – you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved).
181-
Let’s run `git add` now to stage the benchmarks.rb file, and then run `git status` again:
179+
The ``benchmarks.rb'' file appears under a section named ``Changed but not staged for commit'' – which means that a file that is tracked has been modified in the working directory but not yet staged.
180+
To stage it, you run the `git add` command. `git add` is a multipurpose command – you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved. It may be helpful to think of it more as ``add this content to the next commit'' rather than ``add this file to the project''.
181+
Let’s run `git add` now to stage the ``benchmarks.rb'' file, and then run `git status` again:
182182

183183
[source,shell]
184184
----
@@ -214,6 +214,7 @@ Changes not staged for commit:
214214
(use "git checkout -- <file>..." to discard changes in working directory)
215215
216216
modified: benchmarks.rb
217+
217218
----
218219

219220
What the heck?
@@ -277,12 +278,17 @@ build/ # ignore all files in the build/ directory
277278
doc/*.txt # ignore doc/notes.txt, but not doc/server/arch.txt
278279
----
279280

281+
[TIP]
282+
====
283+
GitHub maintains a fairly comprehensive list of good `.gitignore` file examples for dozens or projects and languages at https://github.com/github/gitignore[] if you want a starting point for your project.
284+
====
285+
280286
==== Viewing Your Staged and Unstaged Changes
281287

282288
If the `git status` command is too vague for you – you want to know exactly what you changed, not just which files were changed – you can use the `git diff` command.
283289
We’ll cover `git diff` in more detail later, but you’ll probably use it most often to answer these two questions: What have you changed but not yet staged?
284290
And what have you staged that you are about to commit?
285-
Although `git status` answers those questions very generally, `git diff` shows you the exact lines added and removed – the patch, as it were.
291+
Although `git status` answers those questions very generally by listing the file names, `git diff` shows you the exact lines added and removed – the patch, as it were.
286292

287293
Let’s say you edit and stage the `README` file again and then edit the `benchmarks.rb` file without staging it.
288294
If you run your `git status` command, you once again see something like this:
@@ -328,24 +334,22 @@ index 3cb747f..e445e28 100644
328334
That command compares what is in your working directory with what is in your staging area.
329335
The result tells you the changes you’ve made that you haven’t yet staged.
330336

331-
If you want to see what you’ve staged that will go into your next commit, you can use `git diff --cached`.
332-
(In Git versions 1.6.1 and later, you can also use `git diff --staged`, which may be easier to remember.)
337+
If you want to see what you’ve staged that will go into your next commit, you can use `git diff --staged`.
333338
This command compares your staged changes to your last commit:
334339

335340
[source,shell]
336341
----
337-
$ git diff --cached
342+
$ git diff --staged
338343
diff --git a/README b/README
339344
new file mode 100644
340345
index 0000000..03902a1
341346
--- /dev/null
342-
+++ b/README2
343-
@@ -0,0 +1,5 @@
344-
+grit
345-
+ by Tom Preston-Werner, Chris Wanstrath
346-
+ https://github.com/mojombo/grit
347+
+++ b/README
348+
@@ -0,0 +1,4 @@
349+
+My Project
350+
+
351+
+ This is my project and it is amazing.
347352
+
348-
+Grit is a Ruby library for extracting information from a Git repository
349353
----
350354

351355
It’s important to note that `git diff` by itself doesn’t show all changes made since your last commit – only changes that are still unstaged.
@@ -414,8 +418,8 @@ index 3cb747f..e445e28 100644
414418
Now that your staging area is set up the way you want it, you can commit your changes.
415419
Remember that anything that is still unstaged – any files you have created or modified that you haven’t run `git add` on since you edited them – won’t go into this commit.
416420
They will stay as modified files on your disk.
417-
In this case, the last time you ran `git status`, you saw that everything was staged, so you’re ready to commit your changes.
418-
The simplest way to commit is to type `git commit`:
421+
In this case, the last time you ran `git status`, you saw that everything was staged, so you’re ready to commit your changes.(((git, status)))
422+
The simplest way to commit is to type `git commit`:(((git, commit)))
419423

420424
[source,shell]
421425
----
@@ -488,7 +492,7 @@ $ git commit -a -m 'added new benchmarks'
488492
1 file changed, 5 insertions(+), 0 deletions(-)
489493
----
490494

491-
Notice how you don’t have to run `git add` on the benchmarks.rb file in this case before you commit.
495+
Notice how you don’t have to run `git add` on the ``benchmarks.rb'' file in this case before you commit.
492496

493497
==== Removing Files
494498

@@ -536,7 +540,7 @@ To do this, use the `--cached` option:
536540

537541
[source,shell]
538542
----
539-
$ git rm --cached readme.txt
543+
$ git rm --cached README
540544
----
541545

542546
You can pass files, directories, and file-glob patterns to the `git rm` command.
@@ -610,7 +614,7 @@ To get the project, run
610614

611615
[source,shell]
612616
----
613-
git clone https://github.com/schacon/simplegit-progit.git
617+
git clone https://github.com/schacon/simplegit-progit
614618
----
615619

616620
When you run `git log` in this project, you should get output that looks something like this:
@@ -643,7 +647,7 @@ As you can see, this command lists each commit with its SHA-1 checksum, the auth
643647
A huge number and variety of options to the `git log` command are available to show you exactly what you’re looking for.
644648
Here, we’ll show you some of the most popular.
645649

646-
One of the more helpful options is `-p`, which shows the diff introduced in each commit.
650+
One of the more helpful options is `-p`, which shows the difference introduced in each commit.
647651
You can also use `-2`, which limits the output to only the last two entries:
648652

649653
[source,shell]
@@ -731,6 +735,7 @@ Date: Sat Mar 15 10:31:28 2008 -0700
731735

732736
As you can see, the `--stat` option prints below each commit entry a list of modified files, how many files were changed, and how many lines in those files were added and removed.
733737
It also puts a summary of the information at the end.
738+
734739
Another really useful option is `--pretty`.
735740
This option changes the log output to formats other than the default.
736741
A few prebuilt options are available for you to use.
@@ -751,9 +756,9 @@ This is especially useful when you’re generating output for machine parsing
751756
[source,shell]
752757
----
753758
$ git log --pretty=format:"%h - %an, %ar : %s"
754-
ca82a6d - Scott Chacon, 11 months ago : changed the version number
755-
085bb3b - Scott Chacon, 11 months ago : removed unnecessary test code
756-
a11bef0 - Scott Chacon, 11 months ago : first commit
759+
ca82a6d - Scott Chacon, 6 years ago : changed the version number
760+
085bb3b - Scott Chacon, 6 years ago : removed unnecessary test code
761+
a11bef0 - Scott Chacon, 6 years ago : first commit
757762
----
758763

759764
<<pretty_format>> lists some of the more useful options that format takes.
@@ -786,7 +791,7 @@ So, if you send in a patch to a project and one of the core members applies the
786791
We’ll cover this distinction a bit more in <<_distributed_git>>.
787792

788793
The oneline and format options are particularly useful with another `log` option called `--graph`.
789-
This option adds a nice little ASCII graph showing your branch and merge history, which we can see our copy of the Grit project repository:
794+
This option adds a nice little ASCII graph showing your branch and merge history:
790795

791796
[source,shell]
792797
----
@@ -803,6 +808,8 @@ $ git log --pretty=format:"%h %s" --graph
803808
* 11d191e Merge branch 'defunkt' into local
804809
----
805810

811+
This type of output will become more interesting as we go through branching and merging in the next chapter.
812+
806813
Those are only some simple output-formatting options to `git log` – there are many more.
807814
<<log_options>> lists the options we’ve covered so far, as well as some other common formatting options that may be useful, along with how they change the output of the log command.
808815

@@ -824,7 +831,7 @@ Those are only some simple output-formatting options to `git log` – there are
824831

825832
==== Limiting Log Output
826833

827-
In addition to output-formatting options, git log takes a number of useful limiting options – that is, options that let you show only a subset of commits.
834+
In addition to output-formatting options, `git log` takes a number of useful limiting options – that is, options that let you show only a subset of commits.
828835
You’ve seen one such option already – the `-2` option, which show only the last two commits.
829836
In fact, you can do `-<n>`, where `n` is any integer to show the last `n` commits.
830837
In reality, you’re unlikely to use that often, because Git by default pipes all output through a pager so you see only one page of log output at a time.
@@ -843,6 +850,13 @@ You can also filter the list to commits that match some search criteria.
843850
The `--author` option allows you to filter on a specific author, and the `--grep` option lets you search for keywords in the commit messages.
844851
(Note that if you want to specify both author and grep options, you have to add `--all-match` or the command will match commits with either.)
845852

853+
Another really helpful filter is the `-S` option which takes a string and only shows the commits that introduced a change to the code that added or removed that string. For instance, if you wanted to find the last commit that added or removed a reference to a specific function, you could call:
854+
855+
[source,shell]
856+
----
857+
$ git log --Sfunction_name
858+
----
859+
846860
The last really useful option to pass to `git log` as a filter is a path.
847861
If you specify a directory or file name, you can limit the log output to commits that introduced a change to those files.
848862
This is always the last option and is generally preceded by double dashes (`--`) to separate the paths from the options.
@@ -859,6 +873,8 @@ In <<limit_options>> we’ll list these and a few other common options for your
859873
| `--until`, `--before` | Limit the commits to those made before the specified date.
860874
| `--author` | Only show commits in which the author entry matches the specified string.
861875
| `--committer` | Only show commits in which the committer entry matches the specified string.
876+
| `--grep` | Only show commits with a commit message containing the string
877+
| `-S ` | Only show commits adding or removing code matching the string
862878
|================================
863879

864880
For example, if you want to see which commits modifying test files in the Git source code history were committed by Junio Hamano and were not merges in the month of October 2008, you can run something like this:
@@ -885,27 +901,6 @@ Here, we’ll review a few basic tools for undoing changes that you’ve made.
885901
Be careful, because you can’t always undo some of these undos.
886902
This is one of the few areas in Git where you may lose some work if you do it wrong.
887903

888-
[[_reset]]
889-
==== Reset Demystified
890-
891-
**TODO**
892-
893-
===== The Three Trees
894-
895-
===== The Workflow
896-
897-
===== The Role of Reset
898-
899-
===== Reset With a Path
900-
901-
===== A Fun Example
902-
903-
===== Check It Out
904-
905-
===== Summary
906-
907-
==== Changing Your Last Commit
908-
909904
One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message.
910905
If you want to try that commit again, you can run commit with the `--amend` option:
911906

@@ -1037,7 +1032,7 @@ If you’ve cloned your repository, you should at least see origin – that is t
10371032

10381033
[source,shell]
10391034
----
1040-
$ git clone https://github.com/schacon/ticgit.git
1035+
$ git clone https://github.com/schacon/ticgit
10411036
Cloning into 'ticgit'...
10421037
remote: Reusing existing pack: 1857, done.
10431038
remote: Total 1857 (delta 0), reused 0 (delta 0)

0 commit comments

Comments
 (0)