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
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.
Copy file name to clipboardExpand all lines: book/02-git-basics/1-git-basics.asc
+67-72Lines changed: 67 additions & 72 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,12 +26,12 @@ At this point, nothing in your project is tracked yet.
26
26
(See <<_git_internals>> for more information about exactly what files are contained in the `.git` directory you just created.)
27
27
28
28
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`:
30
30
31
31
[source,shell]
32
32
----
33
33
$ git add *.c
34
-
$ git add README
34
+
$ git add LICENSE
35
35
$ git commit -m 'initial project version'
36
36
----
37
37
@@ -40,33 +40,33 @@ At this point, you have a Git repository with tracked files and an initial commi
40
40
41
41
==== Cloning an Existing Repository
42
42
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).
48
48
49
49
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:
51
51
52
52
[source,shell]
53
53
----
54
-
$ git clone https://github.com/schacon/grit.git
54
+
$ git clone https://github.com/libgit2/libgit2
55
55
----
56
56
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:
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`.
67
67
68
68
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.
70
70
<<_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.
71
71
72
72
=== Recording Changes to the Repository
@@ -89,7 +89,7 @@ image::images/18333fig0201-tn.png[The lifecycle of the status of your files.]
89
89
90
90
==== Checking the Status of Your Files
91
91
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)))
93
93
If you run this command directly after a clone, you should see something like this:
94
94
95
95
[source,shell]
@@ -101,16 +101,16 @@ nothing to commit, working directory clean
101
101
102
102
This means you have a clean working directory – in other words, there are no tracked and modified files.
103
103
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.
107
107
108
108
Let’s say you add a new file to your project, a simple README file.
109
109
If the file didn’t exist before, and you run `git status`, you see your untracked file like so:
110
110
111
111
[source,shell]
112
112
----
113
-
$ vim README
113
+
$ echo 'My Project' > README
114
114
$ git status
115
115
On branch master
116
116
Untracked files:
@@ -128,15 +128,15 @@ You do want to start including README, so let’s start tracking the file.
128
128
129
129
==== Tracking New Files
130
130
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)))
132
132
To begin tracking the README file, you can run this:
133
133
134
134
[source,shell]
135
135
----
136
136
$ git add README
137
137
----
138
138
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:
140
140
141
141
[source,shell]
142
142
----
@@ -150,14 +150,14 @@ Changes to be committed:
150
150
----
151
151
152
152
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.
156
156
157
157
==== Staging Modified Files
158
158
159
159
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:
161
161
162
162
[source,shell]
163
163
----
@@ -176,9 +176,9 @@ Changes not staged for commit:
176
176
177
177
----
178
178
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:
182
182
183
183
[source,shell]
184
184
----
@@ -214,6 +214,7 @@ Changes not staged for commit:
214
214
(use "git checkout -- <file>..." to discard changes in working directory)
215
215
216
216
modified: benchmarks.rb
217
+
217
218
----
218
219
219
220
What the heck?
@@ -277,12 +278,17 @@ build/ # ignore all files in the build/ directory
277
278
doc/*.txt # ignore doc/notes.txt, but not doc/server/arch.txt
278
279
----
279
280
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
+
280
286
==== Viewing Your Staged and Unstaged Changes
281
287
282
288
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.
283
289
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?
284
290
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.
286
292
287
293
Let’s say you edit and stage the `README` file again and then edit the `benchmarks.rb` file without staging it.
288
294
If you run your `git status` command, you once again see something like this:
@@ -328,24 +334,22 @@ index 3cb747f..e445e28 100644
328
334
That command compares what is in your working directory with what is in your staging area.
329
335
The result tells you the changes you’ve made that you haven’t yet staged.
330
336
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`.
333
338
This command compares your staged changes to your last commit:
334
339
335
340
[source,shell]
336
341
----
337
-
$ git diff --cached
342
+
$ git diff --staged
338
343
diff --git a/README b/README
339
344
new file mode 100644
340
345
index 0000000..03902a1
341
346
--- /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.
347
352
+
348
-
+Grit is a Ruby library for extracting information from a Git repository
349
353
----
350
354
351
355
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
414
418
Now that your staging area is set up the way you want it, you can commit your changes.
415
419
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.
416
420
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)))
419
423
420
424
[source,shell]
421
425
----
@@ -488,7 +492,7 @@ $ git commit -a -m 'added new benchmarks'
488
492
1 file changed, 5 insertions(+), 0 deletions(-)
489
493
----
490
494
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.
492
496
493
497
==== Removing Files
494
498
@@ -536,7 +540,7 @@ To do this, use the `--cached` option:
536
540
537
541
[source,shell]
538
542
----
539
-
$ git rm --cached readme.txt
543
+
$ git rm --cached README
540
544
----
541
545
542
546
You can pass files, directories, and file-glob patterns to the `git rm` command.
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
643
647
A huge number and variety of options to the `git log` command are available to show you exactly what you’re looking for.
644
648
Here, we’ll show you some of the most popular.
645
649
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.
647
651
You can also use `-2`, which limits the output to only the last two entries:
648
652
649
653
[source,shell]
@@ -731,6 +735,7 @@ Date: Sat Mar 15 10:31:28 2008 -0700
731
735
732
736
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.
733
737
It also puts a summary of the information at the end.
738
+
734
739
Another really useful option is `--pretty`.
735
740
This option changes the log output to formats other than the default.
736
741
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
751
756
[source,shell]
752
757
----
753
758
$ 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
757
762
----
758
763
759
764
<<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
786
791
We’ll cover this distinction a bit more in <<_distributed_git>>.
787
792
788
793
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:
This type of output will become more interesting as we go through branching and merging in the next chapter.
812
+
806
813
Those are only some simple output-formatting options to `git log` – there are many more.
807
814
<<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.
808
815
@@ -824,7 +831,7 @@ Those are only some simple output-formatting options to `git log` – there are
824
831
825
832
==== Limiting Log Output
826
833
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.
828
835
You’ve seen one such option already – the `-2` option, which show only the last two commits.
829
836
In fact, you can do `-<n>`, where `n` is any integer to show the last `n` commits.
830
837
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.
843
850
The `--author` option allows you to filter on a specific author, and the `--grep` option lets you search for keywords in the commit messages.
844
851
(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.)
845
852
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
+
846
860
The last really useful option to pass to `git log` as a filter is a path.
847
861
If you specify a directory or file name, you can limit the log output to commits that introduced a change to those files.
848
862
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
859
873
| `--until`, `--before` | Limit the commits to those made before the specified date.
860
874
| `--author` | Only show commits in which the author entry matches the specified string.
861
875
| `--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
862
878
|================================
863
879
864
880
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.
885
901
Be careful, because you can’t always undo some of these undos.
886
902
This is one of the few areas in Git where you may lose some work if you do it wrong.
887
903
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
-
909
904
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.
910
905
If you want to try that commit again, you can run commit with the `--amend` option:
911
906
@@ -1037,7 +1032,7 @@ If you’ve cloned your repository, you should at least see origin – that is t
0 commit comments