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: book/02-git-basics/sections/aliases.asc
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ Git doesn't automatically infer your command if you type it in partially.
9
9
If you don't want to type the entire text of each of the Git commands, you can easily set up an alias for each command using `git config`.(((git commands, config)))
10
10
Here are a couple of examples you may want to set up:
11
11
12
-
[source,shell]
12
+
[source,console]
13
13
----
14
14
$ git config --global alias.co checkout
15
15
$ git config --global alias.br branch
@@ -23,14 +23,14 @@ As you go on using Git, you'll probably use other commands frequently as well; d
23
23
This technique can also be very useful in creating commands that you think should exist.
24
24
For example, to correct the usability problem you encountered with unstaging a file, you can add your own unstage alias to Git:
25
25
26
-
[source,shell]
26
+
[source,console]
27
27
----
28
28
$ git config --global alias.unstage 'reset HEAD --'
29
29
----
30
30
31
31
This makes the following two commands equivalent:
32
32
33
-
[source,shell]
33
+
[source,console]
34
34
----
35
35
$ git unstage fileA
36
36
$ git reset HEAD fileA
@@ -39,14 +39,14 @@ $ git reset HEAD fileA
39
39
This seems a bit clearer.
40
40
It's also common to add a `last` command, like this:
41
41
42
-
[source,shell]
42
+
[source,console]
43
43
----
44
44
$ git config --global alias.last 'log -1 HEAD'
45
45
----
46
46
47
47
This way, you can see the last commit easily:
48
48
49
-
[source,shell]
49
+
[source,console]
50
50
----
51
51
$ git last
52
52
commit 66938dae3329c7aebe598c2246a8e6af90d04646
@@ -64,7 +64,7 @@ In that case, you start the command with a `!` character.
64
64
This is useful if you write your own tools that work with a Git repository.
65
65
We can demonstrate by aliasing `git visual` to run `gitk`:
Copy file name to clipboardExpand all lines: book/02-git-basics/sections/getting-a-repository.asc
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ The second clones an existing Git repository from another server.
9
9
10
10
If you're starting to track an existing project in Git, you need to go to the project's directory and type
11
11
12
-
[source,shell]
12
+
[source,console]
13
13
----
14
14
$ git init
15
15
----
@@ -21,7 +21,7 @@ At this point, nothing in your project is tracked yet.
21
21
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.
22
22
You can accomplish that with a few `git add` commands that specify the files you want to track, followed by a `git commit`:
23
23
24
-
[source,shell]
24
+
[source,console]
25
25
----
26
26
$ git add *.c
27
27
$ git add LICENSE
@@ -43,7 +43,7 @@ In fact, if your server disk gets corrupted, you can often use nearly any of the
43
43
You clone a repository with `git clone [url]`.(((git commands, clone)))
44
44
For example, if you want to clone the Git linkable library called libgit2, you can do so like this:
45
45
46
-
[source,shell]
46
+
[source,console]
47
47
----
48
48
$ git clone https://github.com/libgit2/libgit2
49
49
----
@@ -52,7 +52,7 @@ That creates a directory named ``libgit2'', initializes a `.git` directory insid
52
52
If you go into the new `libgit2` directory, you'll see the project files in there, ready to be worked on or used.
53
53
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:
Copy file name to clipboardExpand all lines: book/02-git-basics/sections/recording-changes.asc
+29-29Lines changed: 29 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ image::images/lifecycle.png[The lifecycle of the status of your files.]
20
20
The main tool you use to determine which files are in which state is the `git status` command.(((git commands, status)))
21
21
If you run this command directly after a clone, you should see something like this:
22
22
23
-
[source,shell]
23
+
[source,console]
24
24
----
25
25
$ git status
26
26
On branch master
@@ -36,7 +36,7 @@ For now, that branch is always ``master'', which is the default; you won't worry
36
36
Let's say you add a new file to your project, a simple README file.
37
37
If the file didn't exist before, and you run `git status`, you see your untracked file like so:
38
38
39
-
[source,shell]
39
+
[source,console]
40
40
----
41
41
$ echo 'My Project' > README
42
42
$ git status
@@ -60,14 +60,14 @@ You do want to start including README, so let's start tracking the file.
60
60
In order to begin tracking a new file, you use the command `git add`.(((git commands, add)))
61
61
To begin tracking the README file, you can run this:
62
62
63
-
[source,shell]
63
+
[source,console]
64
64
----
65
65
$ git add README
66
66
----
67
67
68
68
If you run your status command again, you can see that your README file is now tracked and staged to be committed:
69
69
70
-
[source,shell]
70
+
[source,console]
71
71
----
72
72
$ git status
73
73
On branch master
@@ -88,7 +88,7 @@ The `git add` command takes a path name for either a file or a directory; if it'
88
88
Let's change a file that was already tracked.
89
89
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:
90
90
91
-
[source,shell]
91
+
[source,console]
92
92
----
93
93
$ git status
94
94
On branch master
@@ -109,7 +109,7 @@ The ``benchmarks.rb'' file appears under a section named ``Changed but not stage
109
109
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''.(((git commands, add)))
110
110
Let's run `git add` now to stage the ``benchmarks.rb'' file, and then run `git status` again:
111
111
112
-
[source,shell]
112
+
[source,console]
113
113
----
114
114
$ git add benchmarks.rb
115
115
$ git status
@@ -127,7 +127,7 @@ At this point, suppose you remember one little change that you want to make in `
127
127
You open it again and make that change, and you're ready to commit.
128
128
However, let's run `git status` one more time:
129
129
130
-
[source,shell]
130
+
[source,console]
131
131
----
132
132
$ vim benchmarks.rb
133
133
$ git status
@@ -153,7 +153,7 @@ It turns out that Git stages a file exactly as it is when you run the `git add`
153
153
If you commit now, the version of `benchmarks.rb` as it was when you last ran the `git add` command is how it will go into the commit, not the version of the file as it looks in your working directory when you run `git commit`.
154
154
If you modify a file after you run `git add`, you have to run `git add` again to stage the latest version of the file:
155
155
156
-
[source,shell]
156
+
[source,console]
157
157
----
158
158
$ git add benchmarks.rb
159
159
$ git status
@@ -169,7 +169,7 @@ Changes to be committed:
169
169
170
170
While the `git status` output is pretty comprehensive, it's also quite wordy. Git also has a short status flag so you can see your changes in a more compact way. If you run `git status -s` or `git status --short` you get a far more simplified output from the command.
171
171
172
-
[source,shell]
172
+
[source,console]
173
173
----
174
174
$ git status -s
175
175
M README
@@ -189,7 +189,7 @@ These are generally automatically generated files such as log files or files pro
189
189
In such cases, you can create a file listing patterns to match them named `.gitignore`.(((ignoring files)))
190
190
Here is an example `.gitignore` file:
191
191
192
-
[source,shell]
192
+
[source,console]
193
193
----
194
194
$ cat .gitignore
195
195
*.[oa]
@@ -214,7 +214,7 @@ You can also use two asterisks to match nested directories; `a/**/z` would match
214
214
215
215
Here is another example .gitignore file:
216
216
217
-
[source,shell]
217
+
[source,console]
218
218
----
219
219
# a comment - this is ignored
220
220
*.a # no .a files
@@ -240,7 +240,7 @@ Although `git status` answers those questions very generally by listing the file
240
240
Let's say you edit and stage the `README` file again and then edit the `benchmarks.rb` file without staging it.
241
241
If you run your `git status` command, you once again see something like this:
242
242
243
-
[source,shell]
243
+
[source,console]
244
244
----
245
245
$ git status
246
246
On branch master
@@ -258,7 +258,7 @@ Changes not staged for commit:
258
258
259
259
To see what you've changed but not yet staged, type `git diff` with no other arguments:
260
260
261
-
[source,shell]
261
+
[source,console]
262
262
----
263
263
$ git diff
264
264
diff --git a/benchmarks.rb b/benchmarks.rb
@@ -284,7 +284,7 @@ The result tells you the changes you've made that you haven't yet staged.
284
284
If you want to see what you've staged that will go into your next commit, you can use `git diff --staged`.
285
285
This command compares your staged changes to your last commit:
286
286
287
-
[source,shell]
287
+
[source,console]
288
288
----
289
289
$ git diff --staged
290
290
diff --git a/README b/README
@@ -304,7 +304,7 @@ This can be confusing, because if you've staged all of your changes, `git diff`
304
304
305
305
For another example, if you stage the `benchmarks.rb` file and then edit it, you can use `git diff` to see the changes in the file that are staged and the changes that are unstaged:
306
306
307
-
[source,shell]
307
+
[source,console]
308
308
----
309
309
$ git add benchmarks.rb
310
310
$ echo '# test line' >> benchmarks.rb
@@ -324,7 +324,7 @@ Changes not staged for commit:
324
324
325
325
Now you can use `git diff` to see what is still unstaged
326
326
327
-
[source,shell]
327
+
[source,console]
328
328
----
329
329
$ git diff
330
330
diff --git a/benchmarks.rb b/benchmarks.rb
@@ -340,7 +340,7 @@ index e445e28..86b2f7c 100644
340
340
341
341
and `git diff --cached` to see what you've staged so far:
342
342
343
-
[source,shell]
343
+
[source,console]
344
344
----
345
345
$ git diff --cached
346
346
diff --git a/benchmarks.rb b/benchmarks.rb
@@ -369,7 +369,7 @@ They will stay as modified files on your disk.
369
369
In this case, let's say that the last time you ran `git status`, you saw that everything was staged, so you're ready to commit your changes.(((git commands, status)))
370
370
The simplest way to commit is to type `git commit`:(((git commands, commit)))
371
371
372
-
[source,shell]
372
+
[source,console]
373
373
----
374
374
$ git commit
375
375
----
@@ -379,7 +379,7 @@ Doing so launches your editor of choice.
379
379
380
380
The editor displays the following text (this example is a Vim screen):
381
381
382
-
[source,shell]
382
+
[source,console]
383
383
----
384
384
385
385
# Please enter the commit message for your changes. Lines starting
@@ -403,7 +403,7 @@ When you exit the editor, Git creates your commit with that commit message (with
403
403
404
404
Alternatively, you can type your commit message inline with the `commit` command by specifying it after a -m flag, like this:
405
405
406
-
[source,shell]
406
+
[source,console]
407
407
----
408
408
$ git commit -m "Story 182: Fix benchmarks for speed"
409
409
[master 463dc4f] Story 182: Fix benchmarks for speed
@@ -425,7 +425,7 @@ Although it can be amazingly useful for crafting commits exactly how you want th
425
425
If you want to skip the staging area, Git provides a simple shortcut.
426
426
Adding the `-a` option to the `git commit` command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the `git add` part:
427
427
428
-
[source,shell]
428
+
[source,console]
429
429
----
430
430
$ git status
431
431
On branch master
@@ -452,7 +452,7 @@ The `git rm` command does that, and also removes the file from your working dire
452
452
453
453
If you simply remove the file from your working directory, it shows up under the ``Changed but not updated'' (that is, _unstaged_) area of your `git status` output:
454
454
455
-
[source,shell]
455
+
[source,console]
456
456
----
457
457
$ rm grit.gemspec
458
458
$ git status
@@ -468,7 +468,7 @@ no changes added to commit (use "git add" and/or "git commit -a")
468
468
469
469
Then, if you run `git rm`, it stages the file's removal:
470
470
471
-
[source,shell]
471
+
[source,console]
472
472
----
473
473
$ git rm grit.gemspec
474
474
rm 'grit.gemspec'
@@ -489,15 +489,15 @@ In other words, you may want to keep the file on your hard drive but not have Gi
489
489
This is particularly useful if you forgot to add something to your `.gitignore` file and accidentally added it, like a large log file or a bunch of `.a` compiled files.
490
490
To do this, use the `--cached` option:
491
491
492
-
[source,shell]
492
+
[source,console]
493
493
----
494
494
$ git rm --cached README
495
495
----
496
496
497
497
You can pass files, directories, and file-glob patterns to the `git rm` command.
498
498
That means you can do things such as
499
499
500
-
[source,shell]
500
+
[source,console]
501
501
----
502
502
$ git rm log/\*.log
503
503
----
@@ -507,7 +507,7 @@ This is necessary because Git does its own filename expansion in addition to you
507
507
This command removes all files that have the `.log` extension in the `log/` directory.
508
508
Or, you can do something like this:
509
509
510
-
[source,shell]
510
+
[source,console]
511
511
----
512
512
$ git rm \*~
513
513
----
@@ -525,15 +525,15 @@ However, Git is pretty smart about figuring that out after the fact – we'll de
525
525
Thus it's a bit confusing that Git has a `mv` command.
526
526
If you want to rename a file in Git, you can run something like
527
527
528
-
[source,shell]
528
+
[source,console]
529
529
----
530
530
$ git mv file_from file_to
531
531
----
532
532
533
533
and it works fine.
534
534
In fact, if you run something like this and look at the status, you'll see that Git considers it a renamed file:
535
535
536
-
[source,shell]
536
+
[source,console]
537
537
----
538
538
$ git mv README.md README
539
539
$ git status
@@ -546,7 +546,7 @@ Changes to be committed:
546
546
547
547
However, this is equivalent to running something like this:
0 commit comments