forked from seankross/the-unix-workbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-and-github.html
1476 lines (1415 loc) · 96.3 KB
/
git-and-github.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Chapter 6 Git and GitHub | The Unix Workbench</title>
<meta name="description" content="Chapter 6 Git and GitHub | The Unix Workbench">
<meta name="generator" content="bookdown and GitBook 2.6.7">
<meta property="og:title" content="Chapter 6 Git and GitHub | The Unix Workbench" />
<meta property="og:type" content="book" />
<meta property="og:url" content="https://github.com/seankross/the-unix-workbench" />
<meta property="og:image" content="https://github.com/seankross/the-unix-workbenchimages/cover.png" />
<meta name="github-repo" content="seankross/the-unix-workbench" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 6 Git and GitHub | The Unix Workbench" />
<meta name="twitter:image" content="https://github.com/seankross/the-unix-workbenchimages/cover.png" />
<meta name="author" content="Sean Kross">
<meta name="date" content="2019-01-22">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="prev" href="bash-programming.html">
<link rel="next" href="nephology.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-47412721-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-47412721-2');
</script>
<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(data-line-number);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./">The Unix Workbench</a></li>
<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Title Page</a></li>
<li class="chapter" data-level="" data-path="dedication.html"><a href="dedication.html"><i class="fa fa-check"></i>Dedication</a></li>
<li class="chapter" data-level="" data-path="acknowledgements.html"><a href="acknowledgements.html"><i class="fa fa-check"></i>Acknowledgements</a></li>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html"><i class="fa fa-check"></i>Introduction</a></li>
<li class="chapter" data-level="1" data-path="what-is-unix.html"><a href="what-is-unix.html"><i class="fa fa-check"></i><b>1</b> What is Unix?</a></li>
<li class="chapter" data-level="2" data-path="getting-unix.html"><a href="getting-unix.html"><i class="fa fa-check"></i><b>2</b> Getting Unix</a><ul>
<li class="chapter" data-level="2.1" data-path="getting-unix.html"><a href="getting-unix.html#mac-ubuntu-users"><i class="fa fa-check"></i><b>2.1</b> Mac & Ubuntu Users</a></li>
<li class="chapter" data-level="2.2" data-path="getting-unix.html"><a href="getting-unix.html#windows"><i class="fa fa-check"></i><b>2.2</b> Windows</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="command-line-basics.html"><a href="command-line-basics.html"><i class="fa fa-check"></i><b>3</b> Command Line Basics</a><ul>
<li class="chapter" data-level="3.1" data-path="command-line-basics.html"><a href="command-line-basics.html#hello-terminal"><i class="fa fa-check"></i><b>3.1</b> Hello Terminal!</a><ul>
<li class="chapter" data-level="3.1.1" data-path="command-line-basics.html"><a href="command-line-basics.html#summary"><i class="fa fa-check"></i><b>3.1.1</b> Summary</a></li>
<li class="chapter" data-level="3.1.2" data-path="command-line-basics.html"><a href="command-line-basics.html#exercises"><i class="fa fa-check"></i><b>3.1.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="command-line-basics.html"><a href="command-line-basics.html#navigating-the-command-line"><i class="fa fa-check"></i><b>3.2</b> Navigating the Command Line</a><ul>
<li class="chapter" data-level="3.2.1" data-path="command-line-basics.html"><a href="command-line-basics.html#summary-1"><i class="fa fa-check"></i><b>3.2.1</b> Summary</a></li>
<li class="chapter" data-level="3.2.2" data-path="command-line-basics.html"><a href="command-line-basics.html#exercises-1"><i class="fa fa-check"></i><b>3.2.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="command-line-basics.html"><a href="command-line-basics.html#creation-and-inspection"><i class="fa fa-check"></i><b>3.3</b> Creation and Inspection</a><ul>
<li class="chapter" data-level="3.3.1" data-path="command-line-basics.html"><a href="command-line-basics.html#summary-2"><i class="fa fa-check"></i><b>3.3.1</b> Summary</a></li>
<li class="chapter" data-level="3.3.2" data-path="command-line-basics.html"><a href="command-line-basics.html#exercises-2"><i class="fa fa-check"></i><b>3.3.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="3.4" data-path="command-line-basics.html"><a href="command-line-basics.html#migration-and-destruction"><i class="fa fa-check"></i><b>3.4</b> Migration and Destruction</a><ul>
<li class="chapter" data-level="3.4.1" data-path="command-line-basics.html"><a href="command-line-basics.html#summary-3"><i class="fa fa-check"></i><b>3.4.1</b> Summary</a></li>
<li class="chapter" data-level="3.4.2" data-path="command-line-basics.html"><a href="command-line-basics.html#exercises-3"><i class="fa fa-check"></i><b>3.4.2</b> Exercises</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="working-with-unix.html"><a href="working-with-unix.html"><i class="fa fa-check"></i><b>4</b> Working with Unix</a><ul>
<li class="chapter" data-level="4.1" data-path="working-with-unix.html"><a href="working-with-unix.html#self-help"><i class="fa fa-check"></i><b>4.1</b> Self-Help</a><ul>
<li class="chapter" data-level="4.1.1" data-path="working-with-unix.html"><a href="working-with-unix.html#summary-4"><i class="fa fa-check"></i><b>4.1.1</b> Summary</a></li>
<li class="chapter" data-level="4.1.2" data-path="working-with-unix.html"><a href="working-with-unix.html#exercises-4"><i class="fa fa-check"></i><b>4.1.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="working-with-unix.html"><a href="working-with-unix.html#get-wild"><i class="fa fa-check"></i><b>4.2</b> Get Wild</a><ul>
<li class="chapter" data-level="4.2.1" data-path="working-with-unix.html"><a href="working-with-unix.html#summary-5"><i class="fa fa-check"></i><b>4.2.1</b> Summary</a></li>
<li class="chapter" data-level="4.2.2" data-path="working-with-unix.html"><a href="working-with-unix.html#exercises-5"><i class="fa fa-check"></i><b>4.2.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="working-with-unix.html"><a href="working-with-unix.html#search"><i class="fa fa-check"></i><b>4.3</b> Search</a><ul>
<li class="chapter" data-level="4.3.1" data-path="working-with-unix.html"><a href="working-with-unix.html#regular-expressions"><i class="fa fa-check"></i><b>4.3.1</b> Regular Expressions</a></li>
<li class="chapter" data-level="4.3.2" data-path="working-with-unix.html"><a href="working-with-unix.html#metacharacters"><i class="fa fa-check"></i><b>4.3.2</b> Metacharacters</a></li>
<li class="chapter" data-level="4.3.3" data-path="working-with-unix.html"><a href="working-with-unix.html#character-sets"><i class="fa fa-check"></i><b>4.3.3</b> Character Sets</a></li>
<li class="chapter" data-level="4.3.4" data-path="working-with-unix.html"><a href="working-with-unix.html#escaping-anchors-odds-and-ends"><i class="fa fa-check"></i><b>4.3.4</b> Escaping, Anchors, Odds, and Ends</a></li>
<li class="chapter" data-level="4.3.5" data-path="working-with-unix.html"><a href="working-with-unix.html#find"><i class="fa fa-check"></i><b>4.3.5</b> <code>find</code></a></li>
<li class="chapter" data-level="4.3.6" data-path="working-with-unix.html"><a href="working-with-unix.html#summary-6"><i class="fa fa-check"></i><b>4.3.6</b> Summary</a></li>
<li class="chapter" data-level="4.3.7" data-path="working-with-unix.html"><a href="working-with-unix.html#exercises-6"><i class="fa fa-check"></i><b>4.3.7</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="working-with-unix.html"><a href="working-with-unix.html#configure"><i class="fa fa-check"></i><b>4.4</b> Configure</a><ul>
<li class="chapter" data-level="4.4.1" data-path="working-with-unix.html"><a href="working-with-unix.html#history"><i class="fa fa-check"></i><b>4.4.1</b> History</a></li>
<li class="chapter" data-level="4.4.2" data-path="working-with-unix.html"><a href="working-with-unix.html#customizing-bash"><i class="fa fa-check"></i><b>4.4.2</b> Customizing Bash</a></li>
<li class="chapter" data-level="4.4.3" data-path="working-with-unix.html"><a href="working-with-unix.html#summary-7"><i class="fa fa-check"></i><b>4.4.3</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="4.5" data-path="working-with-unix.html"><a href="working-with-unix.html#differentiate"><i class="fa fa-check"></i><b>4.5</b> Differentiate</a><ul>
<li class="chapter" data-level="4.5.1" data-path="working-with-unix.html"><a href="working-with-unix.html#summary-8"><i class="fa fa-check"></i><b>4.5.1</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="4.6" data-path="working-with-unix.html"><a href="working-with-unix.html#pipes"><i class="fa fa-check"></i><b>4.6</b> Pipes</a><ul>
<li class="chapter" data-level="4.6.1" data-path="working-with-unix.html"><a href="working-with-unix.html#summary-9"><i class="fa fa-check"></i><b>4.6.1</b> Summary</a></li>
<li class="chapter" data-level="4.6.2" data-path="working-with-unix.html"><a href="working-with-unix.html#exercises-7"><i class="fa fa-check"></i><b>4.6.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="4.7" data-path="working-with-unix.html"><a href="working-with-unix.html#make"><i class="fa fa-check"></i><b>4.7</b> Make</a><ul>
<li class="chapter" data-level="4.7.1" data-path="working-with-unix.html"><a href="working-with-unix.html#summary-10"><i class="fa fa-check"></i><b>4.7.1</b> Summary</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="bash-programming.html"><a href="bash-programming.html"><i class="fa fa-check"></i><b>5</b> Bash Programming</a><ul>
<li class="chapter" data-level="5.1" data-path="bash-programming.html"><a href="bash-programming.html#math"><i class="fa fa-check"></i><b>5.1</b> Math</a><ul>
<li class="chapter" data-level="5.1.1" data-path="bash-programming.html"><a href="bash-programming.html#summary-11"><i class="fa fa-check"></i><b>5.1.1</b> Summary</a></li>
<li class="chapter" data-level="5.1.2" data-path="bash-programming.html"><a href="bash-programming.html#exercises-8"><i class="fa fa-check"></i><b>5.1.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="bash-programming.html"><a href="bash-programming.html#variables"><i class="fa fa-check"></i><b>5.2</b> Variables</a><ul>
<li class="chapter" data-level="5.2.1" data-path="bash-programming.html"><a href="bash-programming.html#summary-12"><i class="fa fa-check"></i><b>5.2.1</b> Summary</a></li>
<li class="chapter" data-level="5.2.2" data-path="bash-programming.html"><a href="bash-programming.html#exercises-9"><i class="fa fa-check"></i><b>5.2.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="bash-programming.html"><a href="bash-programming.html#user-input"><i class="fa fa-check"></i><b>5.3</b> User Input</a><ul>
<li class="chapter" data-level="5.3.1" data-path="bash-programming.html"><a href="bash-programming.html#summary-13"><i class="fa fa-check"></i><b>5.3.1</b> Summary</a></li>
<li class="chapter" data-level="5.3.2" data-path="bash-programming.html"><a href="bash-programming.html#exercises-10"><i class="fa fa-check"></i><b>5.3.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="bash-programming.html"><a href="bash-programming.html#logic-and-ifelse"><i class="fa fa-check"></i><b>5.4</b> Logic and If/Else</a><ul>
<li class="chapter" data-level="5.4.1" data-path="bash-programming.html"><a href="bash-programming.html#conditional-execution"><i class="fa fa-check"></i><b>5.4.1</b> Conditional Execution</a></li>
<li class="chapter" data-level="5.4.2" data-path="bash-programming.html"><a href="bash-programming.html#conditional-expressions"><i class="fa fa-check"></i><b>5.4.2</b> Conditional Expressions</a></li>
<li class="chapter" data-level="5.4.3" data-path="bash-programming.html"><a href="bash-programming.html#if-and-else"><i class="fa fa-check"></i><b>5.4.3</b> If and Else</a></li>
<li class="chapter" data-level="5.4.4" data-path="bash-programming.html"><a href="bash-programming.html#summary-14"><i class="fa fa-check"></i><b>5.4.4</b> Summary</a></li>
<li class="chapter" data-level="5.4.5" data-path="bash-programming.html"><a href="bash-programming.html#exercises-11"><i class="fa fa-check"></i><b>5.4.5</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="5.5" data-path="bash-programming.html"><a href="bash-programming.html#arrays"><i class="fa fa-check"></i><b>5.5</b> Arrays</a><ul>
<li class="chapter" data-level="5.5.1" data-path="bash-programming.html"><a href="bash-programming.html#summary-15"><i class="fa fa-check"></i><b>5.5.1</b> Summary</a></li>
<li class="chapter" data-level="5.5.2" data-path="bash-programming.html"><a href="bash-programming.html#exercises-12"><i class="fa fa-check"></i><b>5.5.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="5.6" data-path="bash-programming.html"><a href="bash-programming.html#braces"><i class="fa fa-check"></i><b>5.6</b> Braces</a><ul>
<li class="chapter" data-level="5.6.1" data-path="bash-programming.html"><a href="bash-programming.html#summary-16"><i class="fa fa-check"></i><b>5.6.1</b> Summary</a></li>
<li class="chapter" data-level="5.6.2" data-path="bash-programming.html"><a href="bash-programming.html#exercises-13"><i class="fa fa-check"></i><b>5.6.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="5.7" data-path="bash-programming.html"><a href="bash-programming.html#loops"><i class="fa fa-check"></i><b>5.7</b> Loops</a><ul>
<li class="chapter" data-level="5.7.1" data-path="bash-programming.html"><a href="bash-programming.html#for"><i class="fa fa-check"></i><b>5.7.1</b> <code>for</code></a></li>
<li class="chapter" data-level="5.7.2" data-path="bash-programming.html"><a href="bash-programming.html#while"><i class="fa fa-check"></i><b>5.7.2</b> <code>while</code></a></li>
<li class="chapter" data-level="5.7.3" data-path="bash-programming.html"><a href="bash-programming.html#nesting"><i class="fa fa-check"></i><b>5.7.3</b> Nesting</a></li>
<li class="chapter" data-level="5.7.4" data-path="bash-programming.html"><a href="bash-programming.html#summary-17"><i class="fa fa-check"></i><b>5.7.4</b> Summary</a></li>
<li class="chapter" data-level="5.7.5" data-path="bash-programming.html"><a href="bash-programming.html#exercises-14"><i class="fa fa-check"></i><b>5.7.5</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="5.8" data-path="bash-programming.html"><a href="bash-programming.html#functions"><i class="fa fa-check"></i><b>5.8</b> Functions</a><ul>
<li class="chapter" data-level="5.8.1" data-path="bash-programming.html"><a href="bash-programming.html#writing-functions"><i class="fa fa-check"></i><b>5.8.1</b> Writing Functions</a></li>
<li class="chapter" data-level="5.8.2" data-path="bash-programming.html"><a href="bash-programming.html#getting-values-from-functions"><i class="fa fa-check"></i><b>5.8.2</b> Getting Values from Functions</a></li>
<li class="chapter" data-level="5.8.3" data-path="bash-programming.html"><a href="bash-programming.html#summary-18"><i class="fa fa-check"></i><b>5.8.3</b> Summary</a></li>
<li class="chapter" data-level="5.8.4" data-path="bash-programming.html"><a href="bash-programming.html#exercises-15"><i class="fa fa-check"></i><b>5.8.4</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="5.9" data-path="bash-programming.html"><a href="bash-programming.html#writing-programs"><i class="fa fa-check"></i><b>5.9</b> Writing Programs</a><ul>
<li class="chapter" data-level="5.9.1" data-path="bash-programming.html"><a href="bash-programming.html#the-unix-philosophy"><i class="fa fa-check"></i><b>5.9.1</b> The Unix Philosophy</a></li>
<li class="chapter" data-level="5.9.2" data-path="bash-programming.html"><a href="bash-programming.html#making-programs-executable"><i class="fa fa-check"></i><b>5.9.2</b> Making Programs Executable</a></li>
<li class="chapter" data-level="5.9.3" data-path="bash-programming.html"><a href="bash-programming.html#environmental-variables"><i class="fa fa-check"></i><b>5.9.3</b> Environmental Variables</a></li>
<li class="chapter" data-level="5.9.4" data-path="bash-programming.html"><a href="bash-programming.html#summary-19"><i class="fa fa-check"></i><b>5.9.4</b> Summary</a></li>
<li class="chapter" data-level="5.9.5" data-path="bash-programming.html"><a href="bash-programming.html#exercises-16"><i class="fa fa-check"></i><b>5.9.5</b> Exercises</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="git-and-github.html"><a href="git-and-github.html"><i class="fa fa-check"></i><b>6</b> Git and GitHub</a><ul>
<li class="chapter" data-level="6.1" data-path="git-and-github.html"><a href="git-and-github.html#what-are-git-and-github"><i class="fa fa-check"></i><b>6.1</b> What are Git and GitHub?</a></li>
<li class="chapter" data-level="6.2" data-path="git-and-github.html"><a href="git-and-github.html#setting-up-git-and-github"><i class="fa fa-check"></i><b>6.2</b> Setting Up Git and GitHub</a></li>
<li class="chapter" data-level="6.3" data-path="git-and-github.html"><a href="git-and-github.html#getting-started-with-git"><i class="fa fa-check"></i><b>6.3</b> Getting Started with Git</a><ul>
<li class="chapter" data-level="6.3.1" data-path="git-and-github.html"><a href="git-and-github.html#summary-20"><i class="fa fa-check"></i><b>6.3.1</b> Summary</a></li>
<li class="chapter" data-level="6.3.2" data-path="git-and-github.html"><a href="git-and-github.html#exercises-17"><i class="fa fa-check"></i><b>6.3.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="6.4" data-path="git-and-github.html"><a href="git-and-github.html#important-git-features"><i class="fa fa-check"></i><b>6.4</b> Important Git Features</a><ul>
<li class="chapter" data-level="6.4.1" data-path="git-and-github.html"><a href="git-and-github.html#getting-help-logs-and-diffs"><i class="fa fa-check"></i><b>6.4.1</b> Getting Help, Logs, and Diffs</a></li>
<li class="chapter" data-level="6.4.2" data-path="git-and-github.html"><a href="git-and-github.html#ignoring-files"><i class="fa fa-check"></i><b>6.4.2</b> Ignoring Files</a></li>
<li class="chapter" data-level="6.4.3" data-path="git-and-github.html"><a href="git-and-github.html#summary-21"><i class="fa fa-check"></i><b>6.4.3</b> Summary</a></li>
<li class="chapter" data-level="6.4.4" data-path="git-and-github.html"><a href="git-and-github.html#exercises-18"><i class="fa fa-check"></i><b>6.4.4</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="6.5" data-path="git-and-github.html"><a href="git-and-github.html#branching"><i class="fa fa-check"></i><b>6.5</b> Branching</a><ul>
<li class="chapter" data-level="6.5.1" data-path="git-and-github.html"><a href="git-and-github.html#summary-22"><i class="fa fa-check"></i><b>6.5.1</b> Summary</a></li>
<li class="chapter" data-level="6.5.2" data-path="git-and-github.html"><a href="git-and-github.html#exercises-19"><i class="fa fa-check"></i><b>6.5.2</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="6.6" data-path="git-and-github.html"><a href="git-and-github.html#github"><i class="fa fa-check"></i><b>6.6</b> GitHub</a><ul>
<li class="chapter" data-level="6.6.1" data-path="git-and-github.html"><a href="git-and-github.html#markdown"><i class="fa fa-check"></i><b>6.6.1</b> Markdown</a></li>
<li class="chapter" data-level="6.6.2" data-path="git-and-github.html"><a href="git-and-github.html#pull-requests"><i class="fa fa-check"></i><b>6.6.2</b> Pull Requests</a></li>
<li class="chapter" data-level="6.6.3" data-path="git-and-github.html"><a href="git-and-github.html#pages"><i class="fa fa-check"></i><b>6.6.3</b> Pages</a></li>
<li class="chapter" data-level="6.6.4" data-path="git-and-github.html"><a href="git-and-github.html#forking"><i class="fa fa-check"></i><b>6.6.4</b> Forking</a></li>
<li class="chapter" data-level="6.6.5" data-path="git-and-github.html"><a href="git-and-github.html#summary-23"><i class="fa fa-check"></i><b>6.6.5</b> Summary</a></li>
<li class="chapter" data-level="6.6.6" data-path="git-and-github.html"><a href="git-and-github.html#exercises-20"><i class="fa fa-check"></i><b>6.6.6</b> Exercises</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="7" data-path="nephology.html"><a href="nephology.html"><i class="fa fa-check"></i><b>7</b> Nephology</a><ul>
<li class="chapter" data-level="7.1" data-path="nephology.html"><a href="nephology.html#introduction-to-cloud-computing"><i class="fa fa-check"></i><b>7.1</b> Introduction to Cloud Computing</a></li>
<li class="chapter" data-level="7.2" data-path="nephology.html"><a href="nephology.html#setting-up-digitalocean"><i class="fa fa-check"></i><b>7.2</b> Setting Up DigitalOcean</a></li>
<li class="chapter" data-level="7.3" data-path="nephology.html"><a href="nephology.html#connecting-to-the-cloud"><i class="fa fa-check"></i><b>7.3</b> Connecting to the Cloud</a><ul>
<li class="chapter" data-level="7.3.1" data-path="nephology.html"><a href="nephology.html#summary-24"><i class="fa fa-check"></i><b>7.3.1</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="nephology.html"><a href="nephology.html#cloud-computing-basics"><i class="fa fa-check"></i><b>7.4</b> Cloud Computing Basics</a><ul>
<li class="chapter" data-level="7.4.1" data-path="nephology.html"><a href="nephology.html#moving-files-in-and-out-of-the-cloud"><i class="fa fa-check"></i><b>7.4.1</b> Moving Files In and Out of the Cloud</a></li>
<li class="chapter" data-level="7.4.2" data-path="nephology.html"><a href="nephology.html#talking-to-other-servers"><i class="fa fa-check"></i><b>7.4.2</b> Talking to Other Servers</a></li>
<li class="chapter" data-level="7.4.3" data-path="nephology.html"><a href="nephology.html#automating-tasks"><i class="fa fa-check"></i><b>7.4.3</b> Automating Tasks</a></li>
<li class="chapter" data-level="7.4.4" data-path="nephology.html"><a href="nephology.html#summary-25"><i class="fa fa-check"></i><b>7.4.4</b> Summary</a></li>
<li class="chapter" data-level="7.4.5" data-path="nephology.html"><a href="nephology.html#exercises-21"><i class="fa fa-check"></i><b>7.4.5</b> Exercises</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="nephology.html"><a href="nephology.html#shutting-down-a-server"><i class="fa fa-check"></i><b>7.5</b> Shutting Down a Server</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="start-building.html"><a href="start-building.html"><i class="fa fa-check"></i><b>8</b> Start Building</a><ul>
<li class="chapter" data-level="8.1" data-path="start-building.html"><a href="start-building.html#next-steps"><i class="fa fa-check"></i><b>8.1</b> Next Steps</a></li>
<li class="chapter" data-level="8.2" data-path="start-building.html"><a href="start-building.html#giving-feedback"><i class="fa fa-check"></i><b>8.2</b> Giving Feedback</a></li>
<li class="chapter" data-level="8.3" data-path="start-building.html"><a href="start-building.html#using-this-book"><i class="fa fa-check"></i><b>8.3</b> Using this Book</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">The Unix Workbench</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="git-and-github" class="section level1">
<h1><span class="header-section-number">Chapter 6</span> Git and GitHub</h1>
<blockquote>
<p>Proof rather than argument. - Japanese proverb</p>
</blockquote>
<div id="what-are-git-and-github" class="section level2">
<h2><span class="header-section-number">6.1</span> What are Git and GitHub?</h2>
<p>Git is a command line program which allows you to track versions of any code or
plain text documents that you create. Like the “track changes” feature of
a word processor Git keeps track of who made particular changes, the time and
date of those changes, and where the changes were made. If a critical file gets
deleted by accident, or if you make a breaking change to your code and you want
to try to figure out where the breaking change was made, you can use Git to
restore the deleted file or find the new bug in your program. Git organizes
groups of files that you’re tracking into a <strong>repository</strong>, which is just a
directory where all of the changes to files in that directory are tracked. Git
can also help you collaborate with others when you’re writing software. As
<a href="https://twitter.com/kwbroman">Karl Broman</a> says
(paraphrasing <a href="https://twitter.com/mtholder">Mark Holder</a>):
“Your closest collaborator is you six months ago, but you don’t reply to
emails.”</p>
<p>GitHub is a website that provides remote Git repositories. A remote repository
is just a Git repository that you’re able to access over an internet connection.
GitHub allows you to create public remote repositories for free, and anyone can
see your code in these public repositories. If you want to keep your code private
then you can pay GitHub for private remote repositories.</p>
<p>If you’re working
on code together with a friend GitHub can help you sync changes to code files
between you and your friend. There’s also a social and community aspect to
GitHub, since you can watch other programmers develop their projects. GitHub
also makes it easy to jump in and help somebody with their project. GitHub
offers many other useful features which we will discuss at length.</p>
</div>
<div id="setting-up-git-and-github" class="section level2">
<h2><span class="header-section-number">6.2</span> Setting Up Git and GitHub</h2>
<p>Before setting up Git, go to <a href="https://github.com/">GitHub</a> and create a free
account. Take note of which email address you use and which username you choose.</p>
<p>To see if you have Git installed open up your terminal and enter the following:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> --version</code></pre>
<pre><code>## git version 2.14.3 (Apple Git-98)</code></pre>
<p>If you don’t get a response back telling you the version of Git that you have
installed then you need to install Git. You can find instructions for installing
Git on your operating system
<a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git">here</a>.</p>
<p>Open up your shell once you have git installed and run <code>git --version</code> again to
make sure that installation succeeded (you may need to restart your shell or
your computer). After Git is installed we need to set up two environmental
variables, but we only need to do this once. The first variable we need to set
up with Git is your GitHub user name, and the second variable is the email
address that you used to create your GitHub account:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> config --global user.name <span class="st">"myUserName"</span>
<span class="fu">git</span> config --global user.email [email protected]</code></pre>
</div>
<div id="getting-started-with-git" class="section level2">
<h2><span class="header-section-number">6.3</span> Getting Started with Git</h2>
<p>Let’s create our first Git repository. First we need to create a directory:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="bu">cd</span>
<span class="fu">mkdir</span> my-first-repo
<span class="bu">cd</span> my-first-repo</code></pre>
<p>“Repo” in this case is just shorthand for “repository.” To start tracking files
with Git in a directory enter <code>git init</code> into the command line:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> init</code></pre>
<pre><code>## Initialized empty Git repository in /Users/sean/my-first-repo/.git/</code></pre>
<p>You’ve just created your first repository! Now let’s create a file and start
tracking it.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="bu">echo</span> <span class="st">"Welcome to My First Repo"</span> <span class="op">></span> readme.txt</code></pre>
<p>Now that we’ve created a file in this Git repository, let’s use <code>git status</code> to
see what’s going on in this repository. We’ll be using <code>git status</code> continuously
throughout this chapter in order to get information about the status of this Git
repository.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
##
## Initial commit
##
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## readme.txt
##
## nothing added to commit but untracked files present (use "git add" to track)</code></pre>
<p>As you can see <code>readme.txt</code> is listed as an untracked file. In order to let Git
know that you want to track this file we need to use <code>git add</code> with the name of
the file that we want to track. Let’s start tracking <code>readme.txt</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> add readme.txt</code></pre>
<p>Git now knows to track any changes to <code>readme.txt</code>. Let’s see how the status of
the repository has changed:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
##
## Initial commit
##
## Changes to be committed:
## (use "git rm --cached <file>..." to unstage)
##
## new file: readme.txt
##</code></pre>
<p>Git is now tracking <code>readme.txt</code>, or in Git-specific language <code>readme.txt</code> is
now <strong>staged</strong>. Between the parentheses in the message above you can see that
<code>git status</code> is giving us a tip about how to unstage (or un-track) this file,
which we could do with <code>git rm --cached readme.txt</code>. Let’s unstage this file
just to see what happens:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> rm --cached readme.txt</code></pre>
<pre><code>## rm 'readme.txt'</code></pre>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
##
## Initial commit
##
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## readme.txt
##</code></pre>
<p>Our repository is right back to the way it started with <code>readme.txt</code> as an
unstaged file. Let’s start tracking <code>readme.txt</code> again so we can move on to
cooler Git features.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> add readme.txt</code></pre>
<p>Now that Git is tracking <code>readme.txt</code> we need to create a milestone to indicate
the changes that we made to <code>readme.txt</code>. In this case, the changes that we made
were creating the file in the first place! This milestone is called a <strong>commit</strong>
in Git. A commit logs the content of all of the currently staged files. Right
now we only have <code>readme.txt</code> staged so let’s commit the creation of this file.
When making a Git commit, we need to write a commit message which is specified
after the <code>-m</code> flag. The message should briefly describe what changes you’ve
made since the last commit.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> commit -m <span class="st">"added readme.txt"</span></code></pre>
<pre><code>## [master (root-commit) 73e53ca] added readme.txt
## 1 file changed, 1 insertion(+)
## create mode 100644 readme.txt</code></pre>
<p>The message above confirms that the commit succeeded and it summarizes the
changes that took place since the last commit. As you can see in the message we
only changed one file, and we only changed one line in that file. Let’s run
<code>git status</code> again to see the state of our repository after we’ve made the first
commit:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## nothing to commit, working tree clean</code></pre>
<p>All of the changes to the files in this repository have been committed! Let’s
add a few more files to this repository and commit them.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">touch</span> file1.txt
<span class="fu">touch</span> fil2.txt
<span class="fu">ls</span></code></pre>
<pre><code>## file1.txt
## fil2.txt
## readme.txt</code></pre>
<p>While we’re at it let’s also add a new line of text to <code>readme.txt</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="bu">echo</span> <span class="st">"Learning Git is going well so far."</span> <span class="op">>></span> readme.txt</code></pre>
<p>Now that we’ve added two more files and we’ve made changes to one file let’s
take a look at the state of this repository.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## Changes not staged for commit:
## (use "git add <file>..." to update what will be committed)
## (use "git checkout -- <file>..." to discard changes in working directory)
##
## modified: readme.txt
##
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## fil2.txt
## file1.txt
##
## no changes added to commit (use "git add" and/or "git commit -a")</code></pre>
<p>We can see that Git has detected that one file has been modified, and that there
are two files in this directory that it is not tracking. Now we need to tell
Git to track the changes to these files. We could tell Git to track changes to
each file using <code>git add</code>, or since all of the files in this repository are
<code>.txt</code> files we could use a wildcard and enter <code>git add *.txt</code> into the
console. However if we want to track all of the changes to all of the files in
our directory we should use the command <code>git add -A</code>.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> add -A
<span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## Changes to be committed:
## (use "git reset HEAD <file>..." to unstage)
##
## new file: fil2.txt
## new file: file1.txt
## modified: readme.txt
##</code></pre>
<p>Now the changes to all of the files in this repository are being tracked.
Finally let’s commit these changes:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> commit -m <span class="st">"added two files"</span></code></pre>
<pre><code>## [master 53a1983] added two files
## 3 files changed, 1 insertion(+)
## create mode 100644 fil2.txt
## create mode 100644 file1.txt</code></pre>
<p>Darn it, now looking at this commit summary I realize that I have a typo in one
of the names of my files! Thankfully we can undo the most recent commit with
the command <code>git reset --soft HEAD~</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> reset --soft HEAD~
<span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## Changes to be committed:
## (use "git reset HEAD <file>..." to unstage)
##
## new file: fil2.txt
## new file: file1.txt
## modified: readme.txt
##</code></pre>
<p>This repo is now in that exact same state it was right before we made the
commit. Now we can rename <code>fil2.txt</code> to <code>file2.txt</code>, then let’s look at the
status of the repository again.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">mv</span> fil2.txt file2.txt
<span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## Changes to be committed:
## (use "git reset HEAD <file>..." to unstage)
##
## new file: fil2.txt
## new file: file1.txt
## modified: readme.txt
##
## Changes not staged for commit:
## (use "git add/rm <file>..." to update what will be committed)
## (use "git checkout -- <file>..." to discard changes in working directory)
##
## deleted: fil2.txt
##
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## file2.txt
##</code></pre>
<p>We previously told Git to track <code>fil2.txt</code>, and we can see that Git acknowledges
that the file has been deleted. We can bring Git up to speed with what files it
should be tracking with <code>git add -A</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> add -A
<span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## Changes to be committed:
## (use "git reset HEAD <file>..." to unstage)
##
## new file: file1.txt
## new file: file2.txt
## modified: readme.txt
##</code></pre>
<p>Finally we got the file names right! Now let’s make the correct commit:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> commit -m <span class="st">"added two files"</span></code></pre>
<pre><code>## [master 12bb9f5] added two files
## 3 files changed, 1 insertion(+)
## create mode 100644 file1.txt
## create mode 100644 file2.txt</code></pre>
<p>That looks much better.</p>
<div id="summary-20" class="section level3">
<h3><span class="header-section-number">6.3.1</span> Summary</h3>
<ul>
<li>Git tracks changes to plain text files (code files and text documents).</li>
<li>A directory where changes to files are tracked by Git is called a Git
repository.</li>
<li>Change your working directory, then run <code>git init</code> to start a repository.</li>
<li>You can track changes to a file using <code>git add [names of files]</code>.</li>
<li>You can create a milestone about the state of your files using <code>git commit -m "message about changes since the last commit"</code>.</li>
<li>To examine the state of files in your repository use <code>git status</code>.</li>
</ul>
</div>
<div id="exercises-17" class="section level3">
<h3><span class="header-section-number">6.3.2</span> Exercises</h3>
<ol style="list-style-type: decimal">
<li>Start a repository in a new directory.</li>
<li>Create a new file in your new Git repository. Make sure Git is tracking the
file and then create a new commit.</li>
<li>Make changes to the file, and then commit these changes.</li>
<li>Add two new files to your repository, but only commit one of them. What is
the status of your repository after the commit?</li>
<li>Undo the last commit, add the untracked file, and redo the commit.</li>
</ol>
</div>
</div>
<div id="important-git-features" class="section level2">
<h2><span class="header-section-number">6.4</span> Important Git Features</h2>
<div id="getting-help-logs-and-diffs" class="section level3">
<h3><span class="header-section-number">6.4.1</span> Getting Help, Logs, and Diffs</h3>
<p>Git commands have their own <code>man</code> pages. You can access them with
<code>git help [name of command]</code>. For example here’s the start of the help page
for <code>git status</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> help status</code></pre>
<pre><code>GIT-STATUS(1) Git Manual GIT-STATUS(1)
NAME
git-status - Show the working tree status
SYNOPSIS
git status [<options>...] [--] [<pathspec>...]
DESCRIPTION
Displays paths that have differences between the index file and the current HEAD commit,
paths that have differences between the working tree and the index file, and paths in the
working tree that are not tracked by Git (and are not ignored by gitignore(5)). The first
are what you would commit by running git commit; the second and third are what you could
commit by running git add before running git commit.</code></pre>
<p>Just like any other help page that uses <code>less</code>, you can return to the prompt
with the <code>Q</code> key.</p>
<p>If you want to see a list of your Git commits, enter <code>git log</code> into the console:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> log</code></pre>
<pre><code>## commit 12bb9f53b10c9b720dac8441e8624370e4e071b6
## Author: seankross <[email protected]>
## Date: Fri Apr 21 15:23:59 2017 -0400
##
## added two files
##
## commit 73e53cae75301ce9b2802107b1956447241bb17a
## Author: seankross <[email protected]>
## Date: Thu Apr 20 14:15:26 2017 -0400
##
## added readme.txt</code></pre>
<p>If you’ve made many commits to a repository you might need to press the <code>Q</code> key
in order to get back to the prompt. Each commit has its time, date, and commit
message recorded, along with a SHA-1 hash that uniquely identifies the commit.</p>
<p>Git can also help show the differences between unstaged changes to your files
compared to the last commit. Let’s add a new line of text to <code>readme.txt</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="bu">echo</span> <span class="st">"The third line."</span> <span class="op">>></span> readme.txt
<span class="fu">git</span> diff readme.txt</code></pre>
<pre><code>## diff --git a/readme.txt b/readme.txt
## index b965f6a..a3db358 100644
## --- a/readme.txt
## +++ b/readme.txt
## @@ -1,2 +1,3 @@
## Welcome to My First Repo
## Learning Git is going well so far.
## +I added a line.</code></pre>
<p>As you can see a plus sign shows up next to the added line. Now let’s open up
this file in a text editor so we can delete the second line.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">nano</span> readme.txt
<span class="co"># Delete the second line</span>
<span class="fu">git</span> diff readme.txt</code></pre>
<pre><code>## diff --git a/readme.txt b/readme.txt
## index b965f6a..e173fdf 100644
## --- a/readme.txt
## +++ b/readme.txt
## @@ -1,2 +1,2 @@
## Welcome to My First Repo
## -Learning Git is going well so far.
## +I added a line.</code></pre>
<p>A minus sign appears next to the line we deleted. Let’s take a look at the
status of our directory at this point.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## Changes not staged for commit:
## (use "git add <file>..." to update what will be committed)
## (use "git checkout -- <file>..." to discard changes in working directory)
##
## modified: readme.txt
##
## no changes added to commit (use "git add" and/or "git commit -a")</code></pre>
<p>If you read the results from <code>git status</code> carefully you can see that we can take
this repository in one of two directions at this point. We can either <code>git add</code>
the files we’ve made changes to in order to track those changes, or we can
use <code>git checkout</code> in order to remove all of the changes we’ve made to a file
to restore its content to what was present in the last commit. Let’s remove
our changes to see how this works.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">cat</span> readme.txt</code></pre>
<pre><code>## Welcome to My First Repo
## I added a line.</code></pre>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> checkout readme.txt
<span class="fu">cat</span> readme.txt</code></pre>
<pre><code>## Welcome to My First Repo
## Learning Git is going well so far.</code></pre>
<p>And as you can see the changes we made to <code>readme.txt</code> have been undone.</p>
</div>
<div id="ignoring-files" class="section level3">
<h3><span class="header-section-number">6.4.2</span> Ignoring Files</h3>
<p>Sometimes you might have files that you never want Git to track, for example
binary files that are generated as by-products of running code (PDFs or images),
or secrets like passwords or API keys. A file in your Git repository called
<code>.gitignore</code> can list names of files and sub-folders, or simple regular
expressions (whatever you can use with <code>ls</code>) in order to specify files which
should never be tracked. Each line of a <code>.gitignore</code> file should specify a file
or group of files that should not be tracked by Git. Let’s make a <code>.gitignore</code>
file to make sure that we never track image files in this repository:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">touch</span> toby.jpg
<span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## toby.jpg
##
## nothing added to commit but untracked files present (use "git add" to track)</code></pre>
<p>Now that we’ve added an image to our repository, let’s add a <code>.gitignore</code> file
to make sure Git doesn’t track these kinds of files.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="bu">echo</span> <span class="st">"*.jpg"</span> <span class="op">></span> .gitignore
<span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## .gitignore
##
## nothing added to commit but untracked files present (use "git add" to track)</code></pre>
<p>Now we can see that Git has detected the new <code>.gitignore</code> file, but it doesn’t
see <code>toby.jpg</code>. Let’s add and commit our <code>.gitignore</code> file:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> add -A
<span class="fu">git</span> commit -m <span class="st">"added gitignore"</span></code></pre>
<pre><code>## [master adef548] added gitignore
## 1 file changed, 1 insertion(+)
## create mode 100644 .gitignore</code></pre>
<p>Now if we add another <code>.jpg</code> file, Git will not see the file:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">touch</span> bernie.jpg
<span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## nothing to commit, working tree clean</code></pre>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">ls</span></code></pre>
<pre><code>## bernie.jpg
## toby.jpg
## file1.txt
## file2.txt
## readme.txt</code></pre>
</div>
<div id="summary-21" class="section level3">
<h3><span class="header-section-number">6.4.3</span> Summary</h3>
<ul>
<li><code>git help</code> allows you to read the <code>man</code> pages for specific Git commands.</li>
<li><code>git log</code> will show you your commit history.</li>
<li><code>git diff</code> displays what has changed between the last commit and your current
untracked changes.</li>
<li>You can specify a <code>.gitignore</code> file in order to tell Git not to track certain
files.</li>
</ul>
</div>
<div id="exercises-18" class="section level3">
<h3><span class="header-section-number">6.4.4</span> Exercises</h3>
<ol style="list-style-type: decimal">
<li>Look at the help pages for <code>git log</code> and <code>git diff</code>.</li>
<li>Add to the <code>.gitignore</code> you already started to include a specific file name,
then add that file to your repository.</li>
<li>Create a file that contains the Git log for this repository. Use <code>grep</code> to
see which day of the week most of the commits occurred on.</li>
</ol>
</div>
</div>
<div id="branching" class="section level2">
<h2><span class="header-section-number">6.5</span> Branching</h2>
<p>Branching is one of the most powerful features that Git offers. Creating
different Git branches allows you to work on a particular feature or set of
files independently from other “copies” of a repository. That way you and a
friend can work on different parts of the same file on different branches, and
then Git can help you elegantly merge your branches and changes together.</p>
<p>You can list all of the available branches with the command <code>git branch</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> branch</code></pre>
<pre><code>## * master</code></pre>
<p>The star (<code>*</code>) indicates which branch you’re currently on. The default branch
that is created is always called <em>master</em>. Usually people use this branch as the
working version of the software that they are writing, while they develop new and
potentially unstable features on other branches.</p>
<p>To add a branch we’ll also use the <code>git branch</code> command, followed the name of
the branch we want to create:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> branch my-new-feature</code></pre>
<p>Now let’s enter <code>git branch</code> again to confirm that we’ve created the branch:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> branch</code></pre>
<pre><code>## * master
## my-new-feature</code></pre>
<p>We can make <code>my-new-feature</code> the current branch using <code>git checkout</code> with the
name of the branch:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> checkout my-new-feature</code></pre>
<pre><code>## Switched to branch 'my-new-feature'</code></pre>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> branch</code></pre>
<pre><code>## master
## * my-new-feature</code></pre>
<p>If we look at <code>git status</code> we can also see that it will tell us which branch
we’re on:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> status</code></pre>
<pre><code>On branch my-new-feature
nothing to commit, working tree clean</code></pre>
<p>We can switch back to the <code>master</code> branch using <code>git checkout</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> checkout master</code></pre>
<pre><code>## Switched to branch 'master'</code></pre>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> branch</code></pre>
<pre><code>## * master
## my-new-feature</code></pre>
<p>Now we can delete a branch by using the <code>-d</code> flag with <code>git branch</code> and the name
of the branch we want to delete:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> branch -d my-new-feature</code></pre>
<pre><code>## Deleted branch my-new-feature (was adef548).</code></pre>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> branch</code></pre>
<pre><code>## * master</code></pre>
<p>Let’s create a new branch for adding a section to the <code>readme.txt</code> in our
repository. We can create a new branch and switch to that branch at the same
time using the command <code>git checkout -b</code> and the name of the new branch we want
to create:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> checkout -b update-readme</code></pre>
<pre><code>## Switched to a new branch 'update-readme'</code></pre>
<p>Now that we’ve created and switched to a new branch, let’s make some changes to
a file. As you might be expecting right now we’ll add a new line to
<code>readme.txt</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="bu">echo</span> <span class="st">"I added this line in the update-readme branch."</span> <span class="op">>></span> readme.txt
<span class="fu">cat</span> readme.txt</code></pre>
<pre><code>## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.</code></pre>
<p>Now that we’ve added a new line let’s commit these changes:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> add -A
<span class="fu">git</span> commit -m <span class="st">"added a third line to readme.txt"</span></code></pre>
<pre><code>## [update-readme 6e378a9] added a third line to readme.txt
## 1 file changed, 1 insertion(+)</code></pre>
<p>Now that we’ve made a commit on the <code>update-readme</code> branch, let’s switch back to the
<code>master</code> branch, and then we’ll take a look at <code>readme.txt</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> checkout master</code></pre>
<pre><code>## Switched to branch 'master'</code></pre>
<p>Now that we’re on the <code>master</code> branch let’s quickly glance at <code>readme.txt</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">cat</span> readme.txt</code></pre>
<pre><code>## Welcome to My First Repo
## Learning Git is going well so far.</code></pre>
<p>The third line that we added is gone! Don’t fret, the line that we added isn’t
gone forever. We committed the change to this file while we were on the
<code>update-readme</code> branch, so the updated file is safely in that branch. Let’s
switch back to that branch just to make sure:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> checkout update-readme
<span class="fu">cat</span> readme.txt</code></pre>
<pre><code>## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.</code></pre>
<p>And the third line is back! Let’s add and commit yet another line while we’re
on this branch:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="bu">echo</span> <span class="st">"It's sunny outside today."</span> <span class="op">>></span> readme.txt
<span class="fu">git</span> add -A
<span class="fu">git</span> commit -m <span class="st">"added weather info"</span></code></pre>
<pre><code>## [update-readme d7946e9] added weather info
## 1 file changed, 1 insertion(+)</code></pre>
<p>This is a small example of how to use Git branching, but you can see how you
can make incremental edits to plain text (usually code files) without
affecting the <code>master</code> branch (the tested and working copy of your software)
and without affecting any other branches. You can imagine how
this system could be used for multiple people to work on the same codebase at
the same time, or how you could develop and test multiple software features
without them interfering with each other. Now that we’ve made a couple of
changes to <code>readme.txt</code>, let’s combine those changes with what we have in the
<code>master</code> branch. This is made possible by a Git <strong>merge</strong>. Merging allows you
to elegantly combine the changes that have been made between two branches. Let’s
merge the changes we made in the <code>update-readme</code> branch with the <code>master</code>
branch. Git incorporates other branches into the current branch by default.
When you’re merging, the current branch is also called the <strong>base</strong> branch.
Let’s switch to the <code>master</code> branch so we can merge in the changes from the
<code>update-readme</code> branch:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> checkout master</code></pre>
<pre><code>## Switched to branch 'master'</code></pre>
<p>To merge in the changes from another branch we need to use <code>git merge</code> and the
name of the branch:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> merge update-readme</code></pre>
<pre><code>## Updating adef548..d7946e9
## Fast-forward
## readme.txt | 2 ++
## 1 file changed, 2 insertions(+)</code></pre>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">cat</span> readme.txt</code></pre>
<pre><code>## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
## It's sunny outside today.</code></pre>
<p>It looks like you’ve merged your first branch in Git! Branching is part of what
makes Git so powerful since it enables parallel developments on the same code
base. But what if there are two commits in two separate branches that make
different edits to the same line of text? When this occurs it is called a
<strong>conflict</strong>. Let’s create a conflict so we can learn how they can be resolved.</p>
<p>First we’ll switch to the <code>update-readme</code> branch. Use <code>nano</code> to edit the last
line of <code>readme.txt</code>, then commit your changes:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> checkout update-readme
<span class="fu">nano</span> readme.txt
<span class="fu">cat</span> readme.txt</code></pre>
<pre><code>## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
## It's cloudy outside today.</code></pre>
<p>Notice that we changed “sunny” to “cloudy” in the last line.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> add -A
<span class="fu">git</span> commit -m <span class="st">"changed sunny to cloudy"</span></code></pre>
<p>Now that our changes are committed on the <code>update-readme</code> branch, let’s switch
back to <code>master</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> checkout master</code></pre>
<p>Let’s change the same line of code using <code>nano</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">nano</span> readme.txt
<span class="fu">cat</span> readme.txt</code></pre>
<pre><code>## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
## It's windy outside today.</code></pre>
<p>Now let’s commit these changes:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> add -A
<span class="fu">git</span> commit -m <span class="st">"changed sunny to windy"</span></code></pre>
<p>We’ve now created two commits that directly conflict with each other. On the
<code>update-readme</code> branch the last line says <code>It's cloudy outside today.</code>, while
on the <code>master</code> branch the last line says <code>It's windy outside today.</code>. Let’s
see what happens when we try to merge <code>update-readme</code> into <code>master</code>.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> merge update-readme</code></pre>
<pre><code>## Auto-merging readme.txt
## CONFLICT (content): Merge conflict in readme.txt
## Automatic merge failed; fix conflicts and then commit the result.</code></pre>
<p>Uh-oh, there’s a conflict! Let’s check the status of the repo right now:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> status</code></pre>
<pre><code>## On branch master
## You have unmerged paths.
## (fix conflicts and run "git commit")
## (use "git merge --abort" to abort the merge)
##
## Unmerged paths:
## (use "git add <file>..." to mark resolution)
##
## both modified: readme.txt
##
## no changes added to commit (use "git add" and/or "git commit -a")</code></pre>
<p>If you’re getting used to reading the result of <code>git status</code>, you can see that
it often offers suggestions about what steps you should take next. Git is
indicating that both versions of readme.txt have modified the same text. Let’s
take a look at <code>readme.txt</code> to see what’s going on there:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">cat</span> readme.txt</code></pre>
<pre><code>## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
## <<<<<<< HEAD
## It's windy outside today.
## =======
## It's cloudy outside today.
## >>>>>>> update-readme</code></pre>
<p>The first three lines of this file look normal, then things get interesting!
The line between <code><<<<<<< HEAD</code> and <code>=======</code> shows the version of the
conflicted line on the current branch. In Git terminology the <code>HEAD</code>
represents the most recent commit on the branch which is currently checked out
(which is <code>master</code> in this case). The line between <code>=======</code> and
<code>>>>>>>> update-readme</code> shows the version of the line on the <code>update-readme</code>
branch. In order to resolve this conflict, all we need to do is open <code>readme.txt</code>
with <code>nano</code> so we can delete the lines we want to get rid of. In this case let’s
keep the “cloudy” version.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">nano</span> readme.txt
<span class="fu">cat</span> readme.txt</code></pre>
<pre><code>## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
## It's cloudy outside today.</code></pre>
<p>Now we can commit the resolution of this conflict.</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> add -A
<span class="fu">git</span> commit -m <span class="st">"resolved conflict"</span></code></pre>
<p>You’re now familiar with this basics of Git! If you want to go into further
depth with your study of Git I highly recommend the free and open source book
<a href="https://git-scm.com/book/">Pro Git</a>.</p>
<div id="summary-22" class="section level3">
<h3><span class="header-section-number">6.5.1</span> Summary</h3>
<ul>
<li>Git branching allows you and others to work on the same code base together.</li>
<li>You can create a branch with the command <code>git branch [name of branch]</code>.</li>
<li>To switch to a branch use <code>git checkout [name of branch]</code>.</li>
<li>You can combine a branch with your current branch by using <code>git merge</code>.</li>
</ul>
</div>
<div id="exercises-19" class="section level3">
<h3><span class="header-section-number">6.5.2</span> Exercises</h3>
<ol style="list-style-type: decimal">
<li>Start a new branch.</li>
<li>Switch to that branch and add commits to it. Switch to an older branch and
then merge the new branch into your current branch.</li>
<li>Purposefully create and resolve a merge conflict.</li>
</ol>
</div>
</div>
<div id="github" class="section level2">
<h2><span class="header-section-number">6.6</span> GitHub</h2>
<p>Now that you know the basics of using Git, let’s talk about how you can share
your work and start collaborating online using GitHub. As an added bonus, by
the end of this chapter you will have created your very own website!
To get started go to <a href="https://github.com/">GitHub</a> and sign in with the
credentials we set up at the beginning of the chapter. After you sign in you
should see a plus-sign near the top-right corner of your web browser. Click the
plus-sign and a little menu should appear, then click “New repository.” You
should now see a screen that looks like this:</p>
<p><img src="images/new-repo.png" /></p>
<p>In the text box under <strong>Repository name</strong> type <code>my-first-repo</code> and then click
the green <strong>Create repository</strong> button. Now you should see a page like this:</p>
<p><img src="images/quick-setup.png" />
GitHub offers a few suggestions about what to do with our new remote repository.
We’ve already been using a local Git repository, and what GitHub provides is a
<strong>remote</strong> Git repository. A remote Git repository is just a Git repository
stored on a computer is that always turned on and connected to the internet, so
it can act as a central point where we can share and sync our changes to files
with our friends and colleagues. We can see which remote repositories our local
repository is connected to with the <code>git remote</code> command while we have our
working directory set to <code>my-first-repo</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> remote</code></pre>
<p>Nothing is printed to the console since you haven’t set any remotes up yet!
Now let’s add your new GitHub repository as a remote in your local repository:</p>
<pre class="sourceCode bash"><code class="sourceCode bash"><span class="fu">git</span> remote add origin https://github.com/seankross/my-first-repo.git</code></pre>
<p>In the command above <code>git remote add</code> adds a new remote to your local repository,
<code>origin</code> is the name we’re assigning to this remote repository, and
<code>https://github.com/seankross/my-first-repo.git</code>