-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2021-03-12_Understanding-Git--A-Beginners-Guide-Containing-Cheat-Sheets---Resources--b50c9c01a107.html
More file actions
1626 lines (1623 loc) · 152 KB
/
2021-03-12_Understanding-Git--A-Beginners-Guide-Containing-Cheat-Sheets---Resources--b50c9c01a107.html
File metadata and controls
1626 lines (1623 loc) · 152 KB
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 http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Understanding Git (A Beginners Guide Containing Cheat Sheets & Resources)</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<article class="h-entry">
<header>
<h1 class="p-name">Understanding Git (A Beginners Guide Containing Cheat Sheets & Resources)</h1>
</header>
<section data-field="subtitle" class="p-summary">
Basic Git Work Flow.
</section>
<section data-field="body" class="e-content">
<section name="63c8" class="section section--body section--first">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="3315" id="3315" class="graf graf--h3 graf--leading graf--title">Understanding Git (A Beginners
Guide Containing Cheat Sheets & Resources)</h3>
<h3 name="cb18" id="cb18" class="graf graf--h3 graf-after--h3">Resources, Cheat Sheets & Links @Bottom
of the Page!</h3>
<figure name="8397" id="8397" class="graf graf--figure graf-after--h3"><img class="graf-image"
data-image-id="0*68fyM5AI85U_I3vo.jpg" data-width="800" data-height="566" data-is-featured="true"
src="https://cdn-images-1.medium.com/max/800/0*68fyM5AI85U_I3vo.jpg"></figure>
<p name="d6e4" id="d6e4" class="graf graf--p graf-after--figure">For More Advanced Readers, or those with
very limited free time… here’s an abridged Git Reference.</p>
<div name="517f" id="517f" class="graf graf--mixtapeEmbed graf-after--p"><a
href="https://bryanguner.medium.com/git-tricks-57e8d0292285"
data-href="https://bryanguner.medium.com/git-tricks-57e8d0292285"
class="markup--anchor markup--mixtapeEmbed-anchor"
title="https://bryanguner.medium.com/git-tricks-57e8d0292285"><strong
class="markup--strong markup--mixtapeEmbed-strong">Git-Tricks</strong><br><em
class="markup--em markup--mixtapeEmbed-em">Refs</em>bryanguner.medium.com</a><a
href="https://bryanguner.medium.com/git-tricks-57e8d0292285"
class="js-mixtapeImage mixtapeImage u-ignoreBlock" data-media-id="b880d4e300b144d2409afe947de99ecd"
data-thumbnail-img-id="1*yyaUC-O43Gs1qAVkdHrMdw.png"
style="background-image: url(https://cdn-images-1.medium.com/fit/c/160/160/1*yyaUC-O43Gs1qAVkdHrMdw.png);"></a>
</div>
<h3 name="77c5" id="77c5" class="graf graf--h3 graf-after--mixtapeEmbed">What’s a distributed version
control system?</h3>
<p name="50ae" id="50ae" class="graf graf--p graf-after--h3">Git is an example of a distributed version
control system (DVCS) commonly used for open source and commercial software development. DVCSs allow
full access to every file, branch, and iteration of a project, and allows every user access to a full
and self-contained history of all changes. Unlike once popular centralized version control systems,
DVCSs like Git don’t need a constant connection to a central repository. Developers can work anywhere
and collaborate asynchronously from any time zone.</p>
<p name="ef5b" id="ef5b" class="graf graf--p graf-after--p">Without version control, team members are
subject to redundant tasks, slower timelines, and multiple copies of a single project. To eliminate
unnecessary work, Git and other VCSs give each contributor a unified and consistent view of a project,
surfacing work that’s already in progress. Seeing a transparent history of changes, who made them, and
how they contribute to the development of a project helps team members stay aligned while working
independently.</p>
<h3 name="88ed" id="88ed" class="graf graf--h3 graf-after--p">Why Git?</h3>
<p name="bbf8" id="bbf8" class="graf graf--p graf-after--h3">According to the latest <a
href="https://insights.stackoverflow.com/survey/2017#technology"
data-href="https://insights.stackoverflow.com/survey/2017#technology"
class="markup--anchor markup--p-anchor" rel="noopener" target="_blank">Stack Overflow developer
survey</a>, more than 70 percent of developers use Git, making it the most-used VCS in the world. Git
is commonly used for both open source and commercial software development, <strong
class="markup--strong markup--p-strong">with significant benefits</strong> for individuals, teams and
businesses.</p>
<ul class="postList">
<li name="0276" id="0276" class="graf graf--li graf-after--p">Git lets developers see the entire
timeline of their changes, decisions, and progression of any project in one place. From the moment
they access the history of a project, the developer has all the context they need to understand it and
start contributing.</li>
<li name="2be5" id="2be5" class="graf graf--li graf-after--li">Developers work in every time zone. With
a DVCS like Git, collaboration can happen any time while maintaining source code integrity. Using
branches, developers can safely propose changes to production code.</li>
<li name="6d54" id="6d54" class="graf graf--li graf-after--li">Businesses using Git can break down
communication barriers between teams and keep them focused on doing their best work. Plus, Git makes
it possible to align experts across a business to collaborate on major projects.</li>
</ul>
<h3 name="4efe" id="4efe" class="graf graf--h3 graf-after--li">Table Of Contents:</h3>
<p name="48b5" id="48b5" class="graf graf--p graf-after--h3"><a
href="#editing-understanding-git-a-beginners-guide-containing-cheat-sheets--resources--medium"
data-href="#editing-understanding-git-a-beginners-guide-containing-cheat-sheets--resources--medium"
class="markup--anchor markup--p-anchor"><em class="markup--em markup--p-em">Editing Understanding Git
(A Beginners Guide Containing Cheat Sheets & Resources) — Medium</em></a></p>
<blockquote name="e23c" id="e23c" class="graf graf--blockquote graf-after--p"><a
href="#whats-a-distributed-version-controlsystem"
data-href="#whats-a-distributed-version-controlsystem"
class="markup--anchor markup--blockquote-anchor"><strong
class="markup--strong markup--blockquote-strong"><em class="markup--em markup--blockquote-em">What’s
a distributed version control system?</em></strong></a></blockquote>
<blockquote name="5eaa" id="5eaa" class="graf graf--blockquote graf-after--blockquote"><a href="#why-git"
data-href="#why-git" class="markup--anchor markup--blockquote-anchor"><em
class="markup--em markup--blockquote-em">Why Git?</em></a></blockquote>
<blockquote name="7e73" id="7e73" class="graf graf--blockquote graf-after--blockquote"><a
href="#whats-a-repository" data-href="#whats-a-repository"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">What’s a
repository?</em></a></blockquote>
<blockquote name="2b69" id="2b69" class="graf graf--blockquote graf-after--blockquote"><a href="#git-flow"
data-href="#git-flow" class="markup--anchor markup--blockquote-anchor"><strong
class="markup--strong markup--blockquote-strong"><em class="markup--em markup--blockquote-em">Git
Flow</em></strong></a></blockquote>
<blockquote name="b47b" id="b47b" class="graf graf--blockquote graf-after--blockquote"><a
href="#cloning-a-repo-and-changing-the-remoteurl"
data-href="#cloning-a-repo-and-changing-the-remoteurl"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Cloning
a repo and changing the remote url</em></a></blockquote>
<blockquote name="005d" id="005d" class="graf graf--blockquote graf-after--blockquote"><a
href="#1-the-first-step-is-to-clone-therepo" data-href="#1-the-first-step-is-to-clone-therepo"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">1. The
first step is to clone the repo!</em></a></blockquote>
<blockquote name="8bbc" id="8bbc" class="graf graf--blockquote graf-after--blockquote"><a
href="#2-sweet-you-have-the-cloned-repo-in-your-preferred-directory-now-lets-make-your-own-repo-on-github-create-a-new-repository"
data-href="#2-sweet-you-have-the-cloned-repo-in-your-preferred-directory-now-lets-make-your-own-repo-on-github-create-a-new-repository"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">2. Make
your own repo</em></a><em class="markup--em markup--blockquote-em">.</em></blockquote>
<blockquote name="cf45" id="cf45" class="graf graf--blockquote graf-after--blockquote"><a
href="#3-next-copy-thegit-link-that-is-on-the-next-page-do-not-do-any-other-steps-on-this-pagethat-is-for-when-you-do-not-clone-arepo"
data-href="#3-next-copy-thegit-link-that-is-on-the-next-page-do-not-do-any-other-steps-on-this-pagethat-is-for-when-you-do-not-clone-arepo"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">3. Next,
copy the .git link that is on the next page.</em></a></blockquote>
<blockquote name="9b6e" id="9b6e" class="graf graf--blockquote graf-after--blockquote"><a
href="#4-whenver-you-clone-a-repo-it-already-has-agit-directory-with-certain-configurations-set-up-to-be-able-to-push-this-repo-to-your-newly-created-github-repo-we-have-to-change-the-remoteorigin"
data-href="#4-whenver-you-clone-a-repo-it-already-has-agit-directory-with-certain-configurations-set-up-to-be-able-to-push-this-repo-to-your-newly-created-github-repo-we-have-to-change-the-remoteorigin"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">4.
Whenver you clone a repo</em></a></blockquote>
<blockquote name="8d89" id="8d89" class="graf graf--blockquote graf-after--blockquote"><a
href="#5-thats-its-you-can-now-run-git-push-and-it-will-push-to-your-newly-createdrepo"
data-href="#5-thats-its-you-can-now-run-git-push-and-it-will-push-to-your-newly-createdrepo"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">5. Thats
its! You can now run </em></a><code class="markup--code markup--blockquote-code"><a
href="#5-thats-its-you-can-now-run-git-push-and-it-will-push-to-your-newly-createdrepo"
data-href="#5-thats-its-you-can-now-run-git-push-and-it-will-push-to-your-newly-createdrepo"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">git
push</em></a></code><a
href="#5-thats-its-you-can-now-run-git-push-and-it-will-push-to-your-newly-createdrepo"
data-href="#5-thats-its-you-can-now-run-git-push-and-it-will-push-to-your-newly-createdrepo"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em"> and it
will push to your newly created repo.</em></a></blockquote>
<blockquote name="2ab6" id="2ab6" class="graf graf--blockquote graf-after--blockquote"><a
href="#basic-git-workflow" data-href="#basic-git-workflow"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Basic
Git Work Flow.</em></a></blockquote>
<blockquote name="bf59" id="bf59" class="graf graf--blockquote graf-after--blockquote"><a
href="#cheat-sheet" data-href="#cheat-sheet" class="markup--anchor markup--blockquote-anchor"><strong
class="markup--strong markup--blockquote-strong"><em class="markup--em markup--blockquote-em">Cheat
Sheet:</em></strong></a></blockquote>
<blockquote name="3c39" id="3c39" class="graf graf--blockquote graf-after--blockquote"><a
href="#my-git-reference-repo" data-href="#my-git-reference-repo"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">My Git
Reference Repo:</em></a></blockquote>
<blockquote name="3ebe" id="3ebe" class="graf graf--blockquote graf-after--blockquote"><a
href="#git-basics" data-href="#git-basics" class="markup--anchor markup--blockquote-anchor"><em
class="markup--em markup--blockquote-em">Git basics</em></a></blockquote>
<blockquote name="e483" id="e483" class="graf graf--blockquote graf-after--blockquote"><a
href="#a-glance-intogit" data-href="#a-glance-intogit"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">A glance
into GIT</em></a></blockquote>
<blockquote name="4a78" id="4a78" class="graf graf--blockquote graf-after--blockquote"><a
href="#tracking-changes-in-a-repository" data-href="#tracking-changes-in-a-repository"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Tracking
changes in a repository</em></a></blockquote>
<blockquote name="82c0" id="82c0" class="graf graf--blockquote graf-after--blockquote"><a
href="#branches-andworkflow" data-href="#branches-andworkflow"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Branches
and workflow</em></a></blockquote>
<blockquote name="900d" id="900d" class="graf graf--blockquote graf-after--blockquote"><a
href="#bringing-it-backtogether" data-href="#bringing-it-backtogether"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Bringing
it back together</em></a></blockquote>
<blockquote name="a3c0" id="a3c0" class="graf graf--blockquote graf-after--blockquote"><a
href="#connect-w-github" data-href="#connect-w-github"
class="markup--anchor markup--blockquote-anchor"><em
class="markup--em markup--blockquote-em">Connect-W-Github</em></a></blockquote>
<blockquote name="d17b" id="d17b" class="graf graf--blockquote graf-after--blockquote"><a
href="#i-%EF%B8%8F-opensource" data-href="#i-%EF%B8%8F-opensource"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">I ❤️
Open Source</em></a></blockquote>
<blockquote name="39c2" id="39c2" class="graf graf--blockquote graf-after--blockquote"><a
href="#merging-your-code-ongithub" data-href="#merging-your-code-ongithub"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Merging
your code on GitHub</em></a></blockquote>
<blockquote name="3a1a" id="3a1a" class="graf graf--blockquote graf-after--blockquote"><a
href="#browsing-your-git-repository" data-href="#browsing-your-git-repository"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Browsing
Your Git Repository</em></a></blockquote>
<blockquote name="9c74" id="9c74" class="graf graf--blockquote graf-after--blockquote"><a
href="#seeing-changes-in-realtime" data-href="#seeing-changes-in-realtime"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Seeing
changes in real time</em></a></blockquote>
<blockquote name="c8bd" id="c8bd" class="graf graf--blockquote graf-after--blockquote"><a
href="#diff-options" data-href="#diff-options" class="markup--anchor markup--blockquote-anchor"><em
class="markup--em markup--blockquote-em">Diff options</em></a></blockquote>
<blockquote name="c086" id="c086" class="graf graf--blockquote graf-after--blockquote"><a
href="#time-travel" data-href="#time-travel" class="markup--anchor markup--blockquote-anchor"><em
class="markup--em markup--blockquote-em">Time travel</em></a></blockquote>
<blockquote name="cc60" id="cc60" class="graf graf--blockquote graf-after--blockquote"><a
href="#why-checkout" data-href="#why-checkout" class="markup--anchor markup--blockquote-anchor"><em
class="markup--em markup--blockquote-em">Why checkout?</em></a></blockquote>
<blockquote name="c5bd" id="c5bd" class="graf graf--blockquote graf-after--blockquote"><a
href="#git-do-overs-reset-rebase" data-href="#git-do-overs-reset-rebase"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Git
‘Do-Overs’: Reset & Rebase</em></a></blockquote>
<blockquote name="b92a" id="b92a" class="graf graf--blockquote graf-after--blockquote"><a
href="#resetting-thepast" data-href="#resetting-thepast"
class="markup--anchor markup--blockquote-anchor"><em
class="markup--em markup--blockquote-em">Resetting the past</em></a></blockquote>
<blockquote name="0133" id="0133" class="graf graf--blockquote graf-after--blockquote"><a
href="#soft-resets" data-href="#soft-resets" class="markup--anchor markup--blockquote-anchor"><em
class="markup--em markup--blockquote-em">Soft resets</em></a></blockquote>
<blockquote name="71af" id="71af" class="graf graf--blockquote graf-after--blockquote"><a
href="#risky-business-mixedresets" data-href="#risky-business-mixedresets"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Risky
Business: Mixed resets</em></a></blockquote>
<blockquote name="094b" id="094b" class="graf graf--blockquote graf-after--blockquote"><a
href="#red-alert-hardresets" data-href="#red-alert-hardresets"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Red
alert! Hard resets</em></a></blockquote>
<blockquote name="44c0" id="44c0" class="graf graf--blockquote graf-after--blockquote"><a
href="#rebase-alt-time-travel" data-href="#rebase-alt-time-travel"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">Rebase:
‘Alt-time travel’</em></a></blockquote>
<blockquote name="1552" id="1552" class="graf graf--blockquote graf-after--blockquote"><a
href="#i-see-you-too-like-to-live-life-dangerously-tell-me-aboutrebase"
data-href="#i-see-you-too-like-to-live-life-dangerously-tell-me-aboutrebase"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">I see
you too like to live life Dangerously… tell me about Rebase..</em></a></blockquote>
<blockquote name="3075" id="3075" class="graf graf--blockquote graf-after--blockquote"><code
class="markup--code markup--blockquote-code"><a href="#working-on-the-header"
data-href="#working-on-the-header" class="markup--anchor markup--blockquote-anchor"><em
class="markup--em markup--blockquote-em">working-on-the-header</em></a></code></blockquote>
<blockquote name="0254" id="0254"
class="graf graf--blockquote graf--startsWithDoubleQuote graf-after--blockquote"><a
href="#golden-rule-ofgit" data-href="#golden-rule-ofgit"
class="markup--anchor markup--blockquote-anchor"><em class="markup--em markup--blockquote-em">“Golden
Rule of Git”</em></a></blockquote>
<blockquote name="a60c" id="a60c" class="graf graf--blockquote graf-after--blockquote"><strong
class="markup--strong markup--blockquote-strong">How 2's</strong></blockquote>
<blockquote name="a0b5" id="a0b5" class="graf graf--blockquote graf-after--blockquote"><a
href="#troubleshooting-git" data-href="#troubleshooting-git"
class="markup--anchor markup--blockquote-anchor"><strong
class="markup--strong markup--blockquote-strong"><em
class="markup--em markup--blockquote-em">Troubleshooting Git</em></strong></a></blockquote>
<blockquote name="4628" id="4628" class="graf graf--blockquote graf-after--blockquote"><strong
class="markup--strong markup--blockquote-strong">Further Reading & Resources</strong></blockquote>
<h3 name="4828" id="4828" class="graf graf--h3 graf-after--blockquote">What’s a repository?</h3>
<p name="b831" id="b831" class="graf graf--p graf-after--h3">A <em
class="markup--em markup--p-em">repository</em>, or <a href="https://git-scm.com/"
data-href="https://git-scm.com/" class="markup--anchor markup--p-anchor" rel="noopener"
target="_blank">Git project</a>, encompasses the entire collection of files and folders associated
with a project, along with each file’s revision history. The file history appears as snapshots in time
called <em class="markup--em markup--p-em">commits</em>, and the commits exist as a linked-list
relationship, and can be organized into multiple lines of development called <em
class="markup--em markup--p-em">branches</em>. Because Git is a DVCS, repositories are self-contained
units and anyone who owns a copy of the repository can access the entire codebase and its history. Using
the command line or other ease-of-use interfaces, a git repository also allows for: interaction with the
history, cloning, creating branches, committing, merging, comparing changes across versions of code, and
more.</p>
<p name="8948" id="8948" class="graf graf--p graf-after--p">Working in repositories keeps development
projects organized and protected. Developers are encouraged to fix bugs, or create fresh features,
without fear of derailing mainline development efforts. Git facilitates this through the use of topic
branches: lightweight pointers to commits in history that can be easily created and deprecated when no
longer needed.</p>
<h3 name="90cd" id="90cd" class="graf graf--h3 graf-after--p">Git Flow</h3>
<h3 name="f951" id="f951" class="graf graf--h3 graf-after--h3">Cloning a repo and changing the remote url
</h3>
<p name="0525" id="0525" class="graf graf--p graf-after--h3">(These steps are only for when you initially
clone a project repo. Not when you clone your partners repo to collaborate together. To do that, you
only have to complete step 1!)</p>
<h3 name="f1dd" id="f1dd" class="graf graf--h3 graf-after--p">1. The first step is to clone the repo!</h3>
<ul class="postList">
<li name="b2f6" id="b2f6" class="graf graf--li graf-after--h3">Navigate to the repo you want to clone
and hit the big green code button. Copy the link given.</li>
</ul>
<figure name="9964" id="9964" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="1*63LoanrbOPBIwDC6oFarFA.png" data-width="1761" data-height="748"
src="https://cdn-images-1.medium.com/max/800/1*63LoanrbOPBIwDC6oFarFA.png"></figure>
<ul class="postList">
<li name="8563" id="8563" class="graf graf--li graf-after--figure">Navigate in your terminal to the
directory where you want this repo to live. I’ve chosen downloads</li>
<li name="c9b3" id="c9b3" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code u-paddingRight0 u-marginRight0">git clone <a
href="http://HTTPS://LINKTOURL/THATYOUCOPIED" data-href="http://HTTPS://LINKTOURL/THATYOUCOPIED"
class="markup--anchor markup--li-anchor" rel="noopener"
target="_blank">HTTPS://LINKTOURL/THATYOUCOPIED</a></code></li>
</ul>
<figure name="023d" id="023d" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="1*PRiaHOvU-wvpAYQtIqduGQ.png" data-width="1262" data-height="123"
src="https://cdn-images-1.medium.com/max/800/1*PRiaHOvU-wvpAYQtIqduGQ.png"></figure>
<h3 name="0852" id="0852" class="graf graf--h3 graf-after--figure">2. Sweet, you have the cloned repo in
your preferred directory. Now lets make your own repo. On github, create a new repository.</h3>
<ul class="postList">
<li name="c98b" id="c98b" class="graf graf--li graf-after--h3">Default settings are fine. Hit the big
green button <code class="markup--code markup--li-code">Create Repository</code></li>
</ul>
<figure name="5ccb" id="5ccb" class="graf graf--figure graf-after--li"><img class="graf-image"
data-image-id="1*U1qwd0OEBYhcToXYt2i6iA.png" data-width="1855" data-height="615"
src="https://cdn-images-1.medium.com/max/800/1*U1qwd0OEBYhcToXYt2i6iA.png"></figure>
<figure name="f508" id="f508" class="graf graf--figure graf-after--figure"><img class="graf-image"
data-image-id="1*jk3-RTC0rRV_OF931B4Fsg.png" data-width="1706" data-height="949"
src="https://cdn-images-1.medium.com/max/800/1*jk3-RTC0rRV_OF931B4Fsg.png"></figure>
<h3 name="a705" id="a705" class="graf graf--h3 graf-after--figure">3. Next,</h3>
<p name="a27d" id="a27d" class="graf graf--p graf-after--h3 graf--trailing"><strong
class="markup--strong markup--p-strong"><em class="markup--em markup--p-em">copy the .git link that is
on the next page. Do not do any other steps on this page — That is for when you do not clone a
repo.</em></strong></p>
</div>
</div>
</section>
<section name="7148" class="section section--body">
<div class="section-divider">
<hr class="section-divider">
</div>
<div class="section-content">
<div class="section-inner sectionLayout--insetColumn">
<h3 name="26bc" id="26bc" class="graf graf--h3 graf--leading">These are the commands GitHub provides when
you create a new Repo:</h3>
<h4 name="d5a0" id="d5a0" class="graf graf--h4 graf-after--h3">Quick setup — if you’ve done this kind of
thing before</h4>
<p name="f3a6" id="f3a6" class="graf graf--p graf-after--h4">Set up in Desktop</p>
<p name="530f" id="530f" class="graf graf--p graf-after--p">or</p>
<p name="ac70" id="ac70" class="graf graf--p graf-after--p">HTTPSSSH</p>
<p name="5813" id="5813" class="graf graf--p graf-after--p">Get started by <a
href="https://github.com/bgoonz/the-meaning-of-life-is-42/new/master"
data-href="https://github.com/bgoonz/the-meaning-of-life-is-42/new/master"
class="markup--anchor markup--p-anchor" rel="noopener" target="_blank">creating a new file</a> or <a
href="https://github.com/bgoonz/the-meaning-of-life-is-42/upload"
data-href="https://github.com/bgoonz/the-meaning-of-life-is-42/upload"
class="markup--anchor markup--p-anchor" rel="noopener" target="_blank">uploading an existing file</a>.
We recommend every repository include a <a
href="https://github.com/bgoonz/the-meaning-of-life-is-42/new/master?readme=1"
data-href="https://github.com/bgoonz/the-meaning-of-life-is-42/new/master?readme=1"
class="markup--anchor markup--p-anchor" rel="noopener" target="_blank">README</a>, <a
href="https://github.com/bgoonz/the-meaning-of-life-is-42/new/master?filename=LICENSE.md"
data-href="https://github.com/bgoonz/the-meaning-of-life-is-42/new/master?filename=LICENSE.md"
class="markup--anchor markup--p-anchor" rel="noopener" target="_blank">LICENSE</a>, and <a
href="https://github.com/bgoonz/the-meaning-of-life-is-42/new/master?filename=.gitignore"
data-href="https://github.com/bgoonz/the-meaning-of-life-is-42/new/master?filename=.gitignore"
class="markup--anchor markup--p-anchor" rel="noopener" target="_blank">.gitignore</a>.</p>
<h3 name="a0b3" id="a0b3" class="graf graf--h3 graf-after--p">…or create a new repository on the
command line</h3>
<pre name="3372" id="3372"
class="graf graf--pre graf-after--h3">echo "# the-meaning-of-life-is-42" >> README.md<br>git init<br>git add README.md<br>git commit -m "first commit"<br>git branch -M master<br>git remote add origin <a href="https://github.com/bgoonz/the-meaning-of-life-is-42.git" data-href="https://github.com/bgoonz/the-meaning-of-life-is-42.git" class="markup--anchor markup--pre-anchor" rel="noopener" target="_blank">https://github.com/bgoonz/the-meaning-of-life-is-42.git</a><br>git push -u origin master</pre>
<h3 name="0bf5" id="0bf5" class="graf graf--h3 graf-after--pre">…or push an existing repository from the
command line</h3>
<pre name="955b" id="955b"
class="graf graf--pre graf-after--h3">git remote add origin <a href="https://github.com/bgoonz/the-meaning-of-life-is-42.git" data-href="https://github.com/bgoonz/the-meaning-of-life-is-42.git" class="markup--anchor markup--pre-anchor" rel="noopener" target="_blank">https://github.com/bgoonz/the-meaning-of-life-is-42.git</a><br>git branch -M master<br>git push -u origin master</pre>
<h3 name="303c" id="303c" class="graf graf--h3 graf-after--pre">…or import code from another repository
</h3>
<p name="8d47" id="8d47" class="graf graf--p graf-after--h3">You can initialize this repository with code
from a Subversion, Mercurial, or TFS project.</p>
<p name="e5b5" id="e5b5" class="graf graf--p graf-after--p"><a
href="https://github.com/bgoonz/the-meaning-of-life-is-42/import"
data-href="https://github.com/bgoonz/the-meaning-of-life-is-42/import"
class="markup--anchor markup--p-anchor" rel="noopener" target="_blank">Import code</a></p>
<h3 name="426b" id="426b" class="graf graf--h3 graf-after--p">4. Whenever you clone a repo:</h3>
<h4 name="2ebf" id="2ebf" class="graf graf--h4 graf-after--h3">It already has a .git directory with
certain configurations set up. To be able to push this repo to your newly created GitHub repo we have to
change the remote origin.</h4>
<ul class="postList">
<li name="dda6" id="dda6" class="graf graf--li graf-after--h4">To do that, just run this command: (Make
sure you are inside the repo you cloned)</li>
</ul>
<pre name="f34a" id="f34a"
class="graf graf--pre graf-after--li">git remote set-url origin <a href="https://LINK/TO/YOUR/GIT/THAT/YOU/COPIED/FROM/PREVIOUS/STEP.git" data-href="https://LINK/TO/YOUR/GIT/THAT/YOU/COPIED/FROM/PREVIOUS/STEP.git" class="markup--anchor markup--pre-anchor" rel="nofollow noopener" target="_blank">https://LINK/TO/YOUR/GIT/THAT/YOU/COPIED/FROM/PREVIOUS/STEP.git</a></pre>
<p name="ad30" id="ad30" class="graf graf--p graf-after--pre">OR:</p>
<figure name="3a6c" id="3a6c" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*HTnMuxJ4tCDJ3I49cUrP8w.png" data-width="716" data-height="56"
src="https://cdn-images-1.medium.com/max/800/1*HTnMuxJ4tCDJ3I49cUrP8w.png"></figure>
<h3 name="760b" id="760b" class="graf graf--h3 graf-after--figure">5. You can now run <code
class="markup--code markup--h3-code">git push</code> and it will push to your newly created repo.</h3>
<h3 name="dc1c" id="dc1c" class="graf graf--h3 graf-after--h3">Basic Git Work Flow.</h3>
<ul class="postList">
<li name="23ce" id="23ce" class="graf graf--li graf-after--h3">After making changes to a file and you
are ready to commit / push to your repo you can run the following commands:</li>
<li name="9934" id="9934" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">git add .</code> - stages modified files to be committed.</li>
<li name="6ce0" id="6ce0" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">git status</code> - displays files that have been modified</li>
<li name="ce65" id="ce65" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">git commit -m 'A helpfully commit message'</code> -
commits the changes to your local repo. Get in the habit now of making helpful commit messages</li>
<li name="70ec" id="70ec" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">git push</code> - pushes your local commits to your GitHub
repo!</li>
<li name="4ca3" id="4ca3" class="graf graf--li graf-after--li">To pull down changes that your partner
pushed to the repo you simply have to run:</li>
<li name="3bef" id="3bef" class="graf graf--li graf-after--li"><code
class="markup--code markup--li-code">git pull</code> - this will fetch the most recent updates!</li>
</ul>
<h3 name="248a" id="248a" class="graf graf--h3 graf-after--li">Cheat Sheet:</h3>
<figure name="bc2a" id="bc2a" class="graf graf--figure graf--iframe graf-after--h3">
<script src="https://gist.github.com/bgoonz/047223711786562db835107417130c28.js"></script>
</figure>
<h3 name="bfc5" id="bfc5" class="graf graf--h3 graf-after--figure">My Git Reference Repo:</h3>
<div name="f295" id="f295" class="graf graf--mixtapeEmbed graf-after--h3"><a
href="https://github.com/bgoonz/github-reference-repo"
data-href="https://github.com/bgoonz/github-reference-repo"
class="markup--anchor markup--mixtapeEmbed-anchor"
title="https://github.com/bgoonz/github-reference-repo"><strong
class="markup--strong markup--mixtapeEmbed-strong">bgoonz/github-reference-repo</strong><br><em
class="markup--em markup--mixtapeEmbed-em">Cloning a repo and changing the remote url Basic Git
Workflow (These steps are only for when you initially clone a…</em>github.com</a><a
href="https://github.com/bgoonz/github-reference-repo"
class="js-mixtapeImage mixtapeImage u-ignoreBlock" data-media-id="097d038858dc41fa58c54f8755eee3e7"
data-thumbnail-img-id="0*gh1fl0kyc6zBSPvD"
style="background-image: url(https://cdn-images-1.medium.com/fit/c/160/160/0*gh1fl0kyc6zBSPvD);"></a>
</div>
<h3 name="8713" id="8713" class="graf graf--h3 graf-after--mixtapeEmbed">Git basics</h3>
<p name="fdf8" id="fdf8" class="graf graf--p graf-after--h3">Like many disciplines, learning Git is just a
matter of learning a new language. You’ll cover a lot of new vocabulary in this lesson! Remember that
the vocabulary you’ll learn will allow you to communicate clearly with other developers worldwide, so
it’s important to understand the meaning of each term.</p>
<p name="6d76" id="6d76" class="graf graf--p graf-after--p">It’s also important to note that Git is a
complex and powerful tool. As such, its documentation and advanced examples may be tough to understand.
As your knowledge grows, you may choose to dive deeper into Git. Today, you’ll focus on the commands
you’ll use every day — possibly for the rest of your programming career! Get comfortable with these
commands and resist the urge to copy/paste or create keyboard shortcuts as you’re getting started.</p>
<h3 name="00ed" id="00ed" class="graf graf--h3 graf-after--p">A glance into GIT</h3>
<p name="c60a" id="c60a" class="graf graf--p graf-after--h3">Before you look at any practical examples,
let’s talk about how Git works behind the scenes.</p>
<p name="931d" id="931d" class="graf graf--p graf-after--p">Here is your first new word in Git-speak: <em
class="markup--em markup--p-em">repository</em>, often shortened to <em
class="markup--em markup--p-em">repo</em>. A Git repo comprises all the source code for a particular
project. In the “dark ages” example above, the repo is the first directory you created, where work is
saved to, and which acts as the source for code shared to others. Without a repo, Git has nothing to act
on.</p>
<p name="1ef1" id="1ef1" class="graf graf--p graf-after--p">Git manages your project as a series of <em
class="markup--em markup--p-em">commits</em>. A commit is a collection of changes grouped towards a
shared purpose. By tracking these commits, you can see your project on a timeline instead of only as a
finished project:</p>
<figure name="8fe3" id="8fe3" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*Sc7e8RGGxhoCnMFG8KR6og.png" data-width="722" data-height="376"
src="https://cdn-images-1.medium.com/max/800/1*Sc7e8RGGxhoCnMFG8KR6og.png"></figure>
<p name="f56f" id="f56f" class="graf graf--p graf-after--figure">Notice the notes and seemingly random
numbers by each commit? These are referred to as <em class="markup--em markup--p-em">commit
messages</em> and <em class="markup--em markup--p-em">commit hashes</em>, respectively. Git identifies
your commits by their hash, a specially-generated series of letters and numbers. You add commit messages
to convey meaning and to help humans track your commits, as those hashes aren’t very friendly to read!
</p>
<p name="77c3" id="77c3" class="graf graf--p graf-after--p">A Git hash is 40 characters long, but you only
need the first few characters to identify which hash you’re referring to. By default, Git abbreviates
hashes to 7 characters. You’ll follow this convention, too.</p>
<p name="9207" id="9207" class="graf graf--p graf-after--p">Git provides a helpful way for us to “alias” a
commit in plain English as well. These aliases are called <em class="markup--em markup--p-em">refs</em>,
short for “references”. A special one that Git creates for all repositories is <code
class="markup--code markup--p-code">HEAD</code>, which references the most recent commit. You'll
learn more about creating your own refs when you learn about "branching".</p>
<p name="1ced" id="1ced" class="graf graf--p graf-after--p">Git maintains three separate lists of changes:
the <em class="markup--em markup--p-em">working directory</em>, the <em
class="markup--em markup--p-em">staging area</em>, and the <em class="markup--em markup--p-em">commit
history</em>. The working directory includes all of your in-progress changes, the staging area is
reserved for changes you’re ready to commit, and the commit history is made up of changes you’ve already
committed. You’ll look more at these three lists soon.</p>
<p name="4b8d" id="4b8d" class="graf graf--p graf-after--p">Git only cares about changes that are
“tracked”. To track a file, you must add it to the commit history. The working directory will always
show the changes, even if they aren’t tracked. In the commit history, you’ll only have a history of
files that have been formally tracked by your repository.</p>
<h3 name="0242" id="0242" class="graf graf--h3 graf-after--p">Tracking changes in a repository</h3>
<p name="c9c5" id="c9c5" class="graf graf--p graf-after--h3">Now, let’s get practical!</p>
<p name="253b" id="253b" class="graf graf--p graf-after--p">You can create a repository with <code
class="markup--code markup--p-code">git init</code>. Running this command will initialize a new Git
repo in your current directory. It's important to remember that you only want a repository for your
project and not your whole hard drive, so always run this command inside a project folder and not your
home folder or desktop. You can create a new repo in an empty folder or within a project directory
you've already created.</p>
<p name="9893" id="9893" class="graf graf--p graf-after--p">What good is an empty repo? Not much! To add
content to your repository, use <code class="markup--code markup--p-code">git add</code>. You can pass
this command a specific filename, a directory, a "wildcard" to select a series of
similarly-named files, or a <code class="markup--code markup--p-code">.</code> to add every untracked
file in the current directory:</p>
<pre name="9a66" id="9a66"
class="graf graf--pre graf-after--p"><code class="markup--code markup--pre-code"># This will add only my_app.js to the repo:</code></pre>
<pre name="3a3c" id="3a3c"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git add my_app.js</code></pre>
<pre name="9174" id="9174"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># This will add all the files within ./objects:</code></pre>
<pre name="5e90" id="5e90"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git add objects/</code></pre>
<pre name="01c3" id="01c3"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># This will add all the files in the current directory ending in `.js`:</code></pre>
<pre name="ebc4" id="ebc4"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git add *.js</code></pre>
<pre name="8f3d" id="8f3d"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># This will add everything in your current directory:</code></pre>
<pre name="5c8b" id="5c8b"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git add .</code></pre>
<p name="b90a" id="b90a" class="graf graf--p graf-after--pre">Adding a file (or files) moves them from
Git’s working directory to the staging area. You can see what’s been “staged” and what hasn’t by using
<code class="markup--code markup--p-code">git status</code>:</p>
<figure name="179a" id="179a" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*iiehU7FvC-JK90x6Fr0q6g.png" data-width="758" data-height="367"
src="https://cdn-images-1.medium.com/max/800/1*iiehU7FvC-JK90x6Fr0q6g.png"></figure>
<p name="97e3" id="97e3" class="graf graf--p graf-after--figure">In this example, “Changes to be
committed” is your staging area and “Changes not staged for commit” is your working directory. Notice
that you also have “Untracked files”, Git’s way of reminding us that you may have forgotten to <code
class="markup--code markup--p-code">git add</code> a file to your repo. Most Git commands will include
a bit of help text in the output, so always read the messages carefully before moving forward. Thanks,
Git!</p>
<p name="f49d" id="f49d" class="graf graf--p graf-after--p">Once you’re happy with your files and have
staged them, you’ll use <code class="markup--code markup--p-code">git commit</code> to push them into
the commit history. It's significantly more work to make changes after a commit, so be sure your
files are staged and exactly as you'd like them before running this command. Your default text
editor will pop up, and you'll be asked to enter a commit message for this group of changes.</p>
<p name="bd0b" id="bd0b" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">Heads Up:</strong> You may find yourself in an unfamiliar
place! The default text editor for MacOS (and many variants of Linux) is called <em
class="markup--em markup--p-em">Vim</em>. Vim is a terminal-based text editor you’ll discuss in the
near future. It’s visually bare and may just look like terminal text to you! If this happens, don’t
worry — just type <code class="markup--code markup--p-code">:q</code> and press your "return"
key to exit.</p>
<p name="82bd" id="82bd" class="graf graf--p graf-after--p">You’ll want to ensure that future commit
messages open in a more familiar editor. You can run the following commands in your terminal to ensure
that Visual Studio Code is your <code class="markup--code markup--p-code">git commit</code> editor from
now on:</p>
<pre name="046a" id="046a"
class="graf graf--pre graf-after--p"><code class="markup--code markup--pre-code">> git config --global core.editor "code --wait"</code></pre>
<pre name="d024" id="d024"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git config --global -e</code></pre>
<p name="a3f7" id="a3f7" class="graf graf--p graf-after--pre">If you experience any issues, you may be
missing Visual Studio Code’s command line tools. You can find more details and some troubleshooting tips
on Microsoft’s official <a
href="https://code.visualstudio.com/docs/setup/mac#_launching-from-the-command-line"
data-href="https://code.visualstudio.com/docs/setup/mac#_launching-from-the-command-line"
class="markup--anchor markup--p-anchor" rel="noopener" target="_blank">VS Code and macOS
documentation</a>.</p>
<p name="a483" id="a483" class="graf graf--p graf-after--p">Once you close your editor, the commit will be
added to your repository’s commit history. Use <code class="markup--code markup--p-code">git log</code>
to see this history at any time. This command will show all the commits in your repository's
history, beginning with the most recent:</p>
<figure name="5726" id="5726" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*z4zhzwjkwkfXMxKDDkNE9w.png" data-width="695" data-height="421"
src="https://cdn-images-1.medium.com/max/800/1*z4zhzwjkwkfXMxKDDkNE9w.png"></figure>
<p name="1589" id="1589" class="graf graf--p graf-after--figure">Like many Git commands, <code
class="markup--code markup--p-code">git commit</code> includes some helpful shorthand. If you need a
rather short commit message, you can use the <code class="markup--code markup--p-code">-m</code> flag to
include the message inline. Here's an example:</p>
<pre name="dfda" id="dfda"
class="graf graf--pre graf-after--p"><code class="markup--code markup--pre-code">> git commit -m "Fix typo"</code></pre>
<p name="183c" id="183c" class="graf graf--p graf-after--pre">This will commit your changes with the
message “Fix typo” and avoid opening your default text editor. Remember the commit messages are how you
make your project’s history friendly to humans, so don’t use the <code
class="markup--code markup--p-code">-m</code> flag as an excuse to write lame commit messages! A
commit message should always explain why changes were made in clear, concise language. It is also best
practice to use imperative voice in commit messages (i.e. use "Fix typo" instead of
"Fixing the typo" or "Typo was fixed").</p>
<h3 name="5c20" id="5c20" class="graf graf--h3 graf-after--p">Branches and workflow</h3>
<figure name="7913" id="7913" class="graf graf--figure graf-after--h3"><img class="graf-image"
data-image-id="0*7RBsBGvfmvj4T4jm.png" data-width="974" data-height="833"
src="https://cdn-images-1.medium.com/max/800/0*7RBsBGvfmvj4T4jm.png"></figure>
<p name="f03b" id="f03b" class="graf graf--p graf-after--figure">You’ve seen what a project looks like
with a linear commit history, but that’s just scratching the surface of Git’s utility. Let’s explore a
new realm with <em class="markup--em markup--p-em">branches</em>. A branch is a separate timeline in
Git, reserved for its own changes. You’ll use branches to make your own changes independently of others.
These branches can then be <em class="markup--em markup--p-em">merged</em> back into the main branch at
a later time.</p>
<p name="2ec7" id="2ec7" class="graf graf--p graf-after--p">Let’s consider a common scenario: a school
project. It’s a lot of extra hassle to schedule time together and argue over exactly what should be done
next! Instead, group members will often assign tasks amongst themselves, work independently on their
tasks, and reunite to bring it all together as a final report. Git branches let us emulate this workflow
for code: you can make a copy of what’s been done so far, complete a task on your new branch, and merge
that branch back into the shared repository for others to use.</p>
<p name="e787" id="e787" class="graf graf--p graf-after--p">By default, Git repos begin on the <code
class="markup--code markup--p-code">master</code> branch. To create a new branch, use <code
class="markup--code markup--p-code">git branch <name-of-your-branch></code>. This will create a
named reference to your current commit and let you add commits without affecting the <code
class="markup--code markup--p-code">master</code> branch. Here's what a branch looks like:</p>
<figure name="ded7" id="ded7" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*B67PLKs3Tr6HYLklyH6XnQ.png" data-width="801" data-height="496"
src="https://cdn-images-1.medium.com/max/800/1*B67PLKs3Tr6HYLklyH6XnQ.png"></figure>
<p name="81a7" id="81a7" class="graf graf--p graf-after--figure">Notice how your refs help to identify
branches here: <code class="markup--code markup--p-code">master</code> stays to itself and can have
changes added to it independently of your new branch (<code
class="markup--code markup--p-code">footer</code>). <code
class="markup--code markup--p-code">HEAD</code>, Git's special ref, follows us around, so you know
that in the above diagram you're working on the <code
class="markup--code markup--p-code">footer</code> branch.</p>
<p name="6509" id="6509" class="graf graf--p graf-after--p">You can create a new branch or visit an
existing branch in your repository. This is especially helpful for returning the <code
class="markup--code markup--p-code">master</code> branch or for projects you've received from
teammates. To open an existing branch, use <code class="markup--code markup--p-code">git checkout
<name-of-branch></code>.</p>
<h3 name="35bf" id="35bf" class="graf graf--h3 graf-after--p">Bringing it back together</h3>
<p name="13c7" id="13c7" class="graf graf--p graf-after--h3">Once you’re happy with the code in the branch
you’ve been working on, you’ll likely want to integrate the code into the <code
class="markup--code markup--p-code">master</code> branch. You can do this via <code
class="markup--code markup--p-code">git merge</code>. Merging will bring the changes in from another
branch and integrate them into yours. Here's an example workflow:</p>
<pre name="2571" id="2571"
class="graf graf--pre graf-after--p"><code class="markup--code markup--pre-code">> git branch my-changes</code></pre>
<pre name="4417" id="4417"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git add new-file.js</code></pre>
<pre name="bacf" id="bacf"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git commit -m "Add new file"</code></pre>
<pre name="4b0f" id="4b0f"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git checkout master</code></pre>
<pre name="19a7" id="19a7"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git merge my-changes</code></pre>
<p name="38f0" id="38f0" class="graf graf--p graf-after--pre">Following these steps will integrate the
commit from <code class="markup--code markup--p-code">my-changes</code> over to <code
class="markup--code markup--p-code">master</code>. Boom! Now you have your <code
class="markup--code markup--p-code">new-file.js</code> on your default branch.</p>
<p name="a8e3" id="a8e3" class="graf graf--p graf-after--p">As you can imagine, branching can get <em
class="markup--em markup--p-em">very</em> complicated. Your repository’s history may look more like a
shrub than a beautiful tree! You’ll discuss advanced merging and other options in an upcoming lesson.
</p>
<h3 name="506a" id="506a" class="graf graf--h3 graf-after--p">Connect-W-Github</h3>
<p name="aff4" id="aff4" class="graf graf--p graf-after--h3">Git can act as a great history tool and
source code backup for your local projects, but it can also help you work with a team! Git is classified
as a “DVCS”, or “Distributed Version Control System”. This means it has built-in support for managing
code both locally and from a distant source.</p>
<p name="ed4a" id="ed4a" class="graf graf--p graf-after--p">You can refer to a repository source that’s
not local as a <em class="markup--em markup--p-em">remote</em>. Your Git repository can have any number
of remotes, but you’ll usually only have one. By default you’ll refer to the primary remote of a repo as
the <code class="markup--code markup--p-code">origin</code>.</p>
<p name="eeee" id="eeee" class="graf graf--p graf-after--p">You can add a remote to an existing repository
on your computer, or you can retrieve a repository from a remote source. You can refer to this as <em
class="markup--em markup--p-em">cloning</em> the repo. Once you have a repository with a remote, you
can update your local code from the remote by <em class="markup--em markup--p-em">pulling</em> code
down, and you can <em class="markup--em markup--p-em">push</em> up your own code so others have access
to it.</p>
<h3 name="e160" id="e160" class="graf graf--h3 graf-after--p">I ❤️ Open Source</h3>
<p name="3e0f" id="3e0f" class="graf graf--p graf-after--h3">While a remote Git server can be run
anywhere, there are a few places online that have become industry standards for hosting remote Git
repositories. The best-known and most widely-used Git source is a website called <a
href="https://github.com/" data-href="https://github.com/" class="markup--anchor markup--p-anchor"
rel="noopener" target="_blank">GitHub</a>. As the name suggests, GitHub is a global hub for Git
repositories. It’s free to make a Github account, and you’ll find literally millions of public
repositories you can browse.</p>
<p name="03be" id="03be" class="graf graf--p graf-after--p">GitHub takes a lot of work out of managing
remote Git repositories. Instead of having to manage your own server, GitHub provides managed hosting
and even includes some helpful graphical tools for complicated tasks like deployment, branch merging,
and code review.</p>
<p name="2a66" id="2a66" class="graf graf--p graf-after--p">Let’s look at a typical workflow using Git and
GitHub. Imagine it’s your first day on the job. How do you get access to your team’s codebase? By
cloning the repository!</p>
<pre name="db11" id="db11"
class="graf graf--pre graf-after--p"><code class="markup--code markup--pre-code u-paddingRight0 u-marginRight0">> git clone <a href="https://github.com/your-team/your-codebase.git" data-href="https://github.com/your-team/your-codebase.git" class="markup--anchor markup--pre-anchor" rel="noopener" target="_blank">https://github.com/your-team/your-codebase.git</a></code></pre>
<p name="a8e6" id="a8e6" class="graf graf--p graf-after--pre">Using the <code
class="markup--code markup--p-code">git clone</code> command will create a new folder in your current
directory named after the repo you're cloning (in this case, <code
class="markup--code markup--p-code">your-codebase</code>). Inside that folder will be a git repository
of your very own, containing the repo's entire commit history.</p>
<p name="8f5c" id="8f5c" class="graf graf--p graf-after--p">You’ll likely start on the <code
class="markup--code markup--p-code">master</code> branch, but remember that this is the default branch
and it's unlikely you want to make changes to it.<strong class="markup--strong markup--p-strong"><em
class="markup--em markup--p-em"> Since you're working on a team now, it's important to think
of how your changes to the repository might affect others.</em></strong></p>
<p name="ea5c" id="ea5c" class="graf graf--p graf-after--p">The safest way to make changes is to create a
new branch, make your changes there, and then push your branch up to the remote repository for review.
You'll use the <code class="markup--code markup--p-code">git push</code> command to do this.
Let's look at an example:</p>
<pre name="3758" id="3758"
class="graf graf--pre graf-after--p"><code class="markup--code markup--pre-code">> git branch add-my-new-file</code></pre>
<pre name="9dbe" id="9dbe"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git add my-new-file.js</code></pre>
<pre name="5fc4" id="5fc4"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git commit -m "Add new file"</code></pre>
<pre name="c20f" id="c20f"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git push -u origin add-my-new-file</code></pre>
<p name="e434" id="e434" class="graf graf--p graf-after--pre">Notice how you used the <code
class="markup--code markup--p-code">-u</code> flag with <code class="markup--code markup--p-code">git
push</code>. This flag, shorthand for <code class="markup--code markup--p-code">--set-upstream</code>,
lets Git know that you want your local branch to follow a remote branch. You've passed the same name
in, so you'll now have two branches in your local repository: <code
class="markup--code markup--p-code">add-my-new-file</code>, which is where your changes live after
being committed, and <code class="markup--code markup--p-code">origin/add-my-new-file</code>, which
keeps up with your remote branch and updates it after you use <code
class="markup--code markup--p-code">git push</code>.</p>
<p name="8349" id="8349" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">You only need to use the </strong><code
class="markup--code markup--p-code"><strong
class="markup--strong markup--p-strong">-u</strong></code><strong
class="markup--strong markup--p-strong"> flag the first time you push each new branch - Git will know
what to do with a simple </strong><code class="markup--code markup--p-code"><strong
class="markup--strong markup--p-strong">git push</strong></code><strong
class="markup--strong markup--p-strong"> from then on.</strong></p>
<p name="8847" id="8847" class="graf graf--p graf-after--p">You now know how to push your changes up, but
what about getting the changes your teammates have made? For this, you’ll use <code
class="markup--code markup--p-code">git pull</code>. Pulling from the remote repo will update all of
your local branches with the code from each branch's remote counterpart.</p>
<p name="da52" id="da52" class="graf graf--p graf-after--p"><strong
class="markup--strong markup--p-strong">Behind the scenes,</strong> Git is running two separate
commands: <code class="markup--code markup--p-code">git fetch</code> and <code
class="markup--code markup--p-code">git merge</code>.</p>
<figure name="d5bf" id="d5bf" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="0*Y6QQHsNKuayJ_WxQ" data-width="638" data-height="359"
src="https://cdn-images-1.medium.com/max/800/0*Y6QQHsNKuayJ_WxQ"></figure>
<p name="8b47" id="8b47" class="graf graf--p graf-after--figure">Fetching retrieves the repository code
and updates any remote tracking branches in your local repo, and merge does just you've already
explored: integrates changes into the local branches. Here's a graphic to explain this a little
better:</p>
<figure name="c6ae" id="c6ae" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*eJVpPtvfIeuYqmql0XkQ8Q.png" data-width="705" data-height="477"
src="https://cdn-images-1.medium.com/max/800/1*eJVpPtvfIeuYqmql0XkQ8Q.png"></figure>
<p name="7cfd" id="7cfd" class="graf graf--p graf-after--figure">It’s important to remember to use <code
class="markup--code markup--p-code">git pull</code> often. A dynamic team may commit and push code
many times during the day, and it's easy to fall behind. The more often you <code
class="markup--code markup--p-code">pull</code>, the more certain you can be that your own code is
based on the "latest and greatest".</p>
<h3 name="780b" id="780b" class="graf graf--h3 graf-after--p">Merging your code on GitHub</h3>
<p name="6c5c" id="6c5c" class="graf graf--p graf-after--h3">If you’re paying close attention, you may
have noticed that there’s a missing step in your workflows so far: how do you get your code merged into
your default branch? This is done by a process called a <em class="markup--em markup--p-em">Pull
Request</em>.</p>
<p name="4057" id="4057" class="graf graf--p graf-after--p">A pull request (or “PR”) is a feature specific
to GitHub, not a feature of Git. It’s a safety net to prevent bugs, and it’s a critical part of the
collaboration workflow. Here’s a high-level of overview of how it works:</p>
<p name="5234" id="5234" class="graf graf--p graf-after--p">You push your code up to GitHub in its own
branch.</p>
<p name="c36b" id="c36b" class="graf graf--p graf-after--p">You open a pull request against a <em
class="markup--em markup--p-em">base branch</em>.</p>
<p name="fed9" id="fed9" class="graf graf--p graf-after--p">GitHub creates a comparison page just for your
code, detailing the changes you’ve made.</p>
<p name="686d" id="686d" class="graf graf--p graf-after--p">Other members of the team can review your code
for errors.</p>
<p name="4103" id="4103" class="graf graf--p graf-after--p">You make changes to your branch based on
feedback and push the new commits up.</p>
<p name="d6b6" id="d6b6" class="graf graf--p graf-after--p">The PR automatically updates with your
changes.</p>
<p name="8053" id="8053" class="graf graf--p graf-after--p">Once everyone is satisfied, a repo maintainer
on GitHub can merge your branch.</p>
<p name="bb99" id="bb99" class="graf graf--p graf-after--p">Huzzah! Your code is in the main branch and
ready for everyone else to <code class="markup--code markup--p-code">git pull</code>.</p>
<p name="b38b" id="b38b" class="graf graf--p graf-after--p">You’ll create and manage your pull requests
via GitHub’s web portal, instead of the command line. You’ll walk through the process of creating,
reviewing, and merging a pull request in an upcoming project.</p>
<h3 name="a271" id="a271" class="graf graf--h3 graf-after--p">Browsing Your Git Repository</h3>
<p name="f02e" id="f02e" class="graf graf--p graf-after--h3">Repositories can feel intimidating at first,
but it won’t take you long to navigate code like you own the place — because you do! Let’s discuss a few
tools native to Git that help us browse our changes over time.</p>
<p name="1e4e" id="1e4e" class="graf graf--p graf-after--p">We’ll be covering:</p>
<p name="3851" id="3851" class="graf graf--p graf-after--p">Comparing changes with <code
class="markup--code markup--p-code">git diff</code></p>
<p name="076b" id="076b" class="graf graf--p graf-after--p">Browsing through our code “checkpoints” with
<code class="markup--code markup--p-code">git checkout</code></p>
<h3 name="87de" id="87de" class="graf graf--h3 graf-after--p">Seeing changes in real time</h3>
<p name="dc61" id="dc61" class="graf graf--p graf-after--h3">Git is all about change tracking, so it makes
sense that it would include a utility for visualizing a set of changes. We refer to a list of
differences between two files (or the same file over time) as a <em
class="markup--em markup--p-em">diff</em>, and we can use <code
class="markup--code markup--p-code">git diff</code> to visualize diffs in our repo!</p>
<p name="b651" id="b651" class="graf graf--p graf-after--p">When run with no extra options, <code
class="markup--code markup--p-code">git diff</code> will return any tracked changes in our working
directory since the last commit. <strong class="markup--strong markup--p-strong">Tracked</strong> is a
key word here; <code class="markup--code markup--p-code">git diff</code> won't show us changes to
files that haven't been included in our repo via <code class="markup--code markup--p-code">git
add</code>. This is helpful for seeing what you've changed before committing! Here's an
example of a small change:</p>
<figure name="0129" id="0129" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*zRnx1PxLRHNl9fYtBzxxsg.png" data-width="429" data-height="282"
src="https://cdn-images-1.medium.com/max/800/1*zRnx1PxLRHNl9fYtBzxxsg.png"></figure>
<p name="dfd1" id="dfd1" class="graf graf--p graf-after--figure">Let’s break down some of the new syntax
in this output.</p>
<p name="220b" id="220b" class="graf graf--p graf-after--p">The diff opens with some Git-specific data,
including the branch/files we’re checking, and some unique hashes that Git uses to track each diff. You
can skip past this to get to the important bits.</p>
<p name="a074" id="a074" class="graf graf--p graf-after--p"><code
class="markup--code markup--p-code">---</code> & <code
class="markup--code markup--p-code">+++</code> let us know that there are both additions and
subtractions in the file "App.js". A diff doesn't have a concept of inline changes - it
treats a single change as removing something old and replacing it with something new.</p>
<p name="4988" id="4988" class="graf graf--p graf-after--p"><code
class="markup--code markup--p-code">@@</code> lets us know that we're starting a single
"chunk" of the diff. A diff could have multiple chunks for a single file (for example: if you
made changes far apart, like the header & footer). The numbers in between tell us how many lines
we're seeing and where they start. For example: <code class="markup--code markup--p-code">@@ +1,3
-1,3 @@</code> means we'll see three lines of significant content, including both addition &
removal, beginning at line one.</p>
<p name="6acb" id="6acb" class="graf graf--p graf-after--p">In the code itself, lines that were removed
are prefixed with a <code class="markup--code markup--p-code">-</code> and lines that were added are
prefixed with a <code class="markup--code markup--p-code">+</code>. Remember that you won't see
these on the same lines. Even if you only changed a few words, Git will still treat it like the whole
line was replaced.</p>
<h3 name="66fd" id="66fd" class="graf graf--h3 graf-after--p">Diff options</h3>
<p name="ee37" id="ee37" class="graf graf--p graf-after--h3">Remember that, by default, <code
class="markup--code markup--p-code">git diff</code> compares the current working directory to the last
commit. You can compare the staging area instead of the working directory with <code
class="markup--code markup--p-code">git diff --staged</code>. This is another great way to
double-check your work before pushing up to a remote branch.</p>
<p name="4d0d" id="4d0d" class="graf graf--p graf-after--p">You’re also not limited to your current
branch — or even your current commit! You can pass a base & target branch to compare, and you can
use some special characters to help you browse faster! Here are a few examples:</p>
<pre name="a289" id="a289"
class="graf graf--pre graf-after--p"><code class="markup--code markup--pre-code"># See differences between the 'feature'</code></pre>
<pre name="de3a" id="de3a"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># branch and the 'master' branch.</code></pre>
<pre name="2a35" id="2a35"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git diff master feature</code></pre>
<pre name="f1ba" id="f1ba"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># Compare two different commits</code></pre>
<pre name="1653" id="1653"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git diff 1fc345a 2e3dff</code></pre>
<pre name="b17b" id="b17b"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># Compare a specific file across separate commits</code></pre>
<pre name="7e51" id="7e51"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git diff 1fc345a 2e3dff my-file.js</code></pre>
<h3 name="3b99" id="3b99" class="graf graf--h3 graf-after--pre">Time travel</h3>
<p name="4c47" id="4c47" class="graf graf--p graf-after--h3"><code class="markup--code markup--p-code">git
diff</code> gives us the opportunity to explore our code's current state, but what if we wanted to
see its state at a different point in time? We can use <em
class="markup--em markup--p-em">checkout</em>! <code class="markup--code markup--p-code">git
checkout</code> lets us take control of our <code class="markup--code markup--p-code">HEAD</code> to
bounce around our timeline as we please.</p>
<p name="419d" id="419d" class="graf graf--p graf-after--p">Remember that <code
class="markup--code markup--p-code">HEAD</code> is a special Git reference that usually follows the
latest commit on our current branch. We can use <code class="markup--code markup--p-code">git
checkout</code> to point our <code class="markup--code markup--p-code">HEAD</code> reference at a
different commit, letting us travel to any commit in our repository's history. By reading the commit
message and exploring the code at the time of the commit, we can see not only what changed but also why
it changed! This can be great for debugging a problem or understanding how an app evolved.</p>
<p name="ca72" id="ca72" class="graf graf--p graf-after--p">Let’s look at a diagram to understand what
<code class="markup--code markup--p-code">checkout</code> does a little better:</p>
<figure name="64be" id="64be" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*eqVSR_YD0kYQWHoLLbJs9Q.png" data-width="750" data-height="615"
src="https://cdn-images-1.medium.com/max/800/1*eqVSR_YD0kYQWHoLLbJs9Q.png"></figure>
<p name="30aa" id="30aa" class="graf graf--p graf-after--figure">Notice that we haven’t lost any commits,
commit messages, or code changes. Using <code class="markup--code markup--p-code">git checkout</code> is
entirely non-destructive.</p>
<p name="e087" id="e087" class="graf graf--p graf-after--p">To browse to a different commit, simply pass
in a reference or hash for the commit you’d like to explore. <code
class="markup--code markup--p-code">git checkout</code> also supports a few special characters &
reserved references:</p>
<pre name="ae12" id="ae12"
class="graf graf--pre graf-after--p"><code class="markup--code markup--pre-code"># You can checkout a branch name.</code></pre>
<pre name="169d" id="169d"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># You'll be using this particular branch a lot!</code></pre>
<pre name="18f0" id="18f0"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git checkout master</code></pre>
<pre name="cace" id="cace"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># You can also use commit hashes directly</code></pre>
<pre name="f365" id="f365"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git checkout 7d3e2f1</code></pre>
<pre name="d9eb" id="d9eb"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># Using a hyphen instead of a hash will take</code></pre>
<pre name="7d70" id="7d70"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># you to the last branch you checked out</code></pre>
<pre name="68d7" id="68d7"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git checkout -</code></pre>
<pre name="bceb" id="bceb"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># You can use "HEAD~N" to move N commits prior</code></pre>
<pre name="34b1" id="34b1"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code"># to the current HEAD</code></pre>
<pre name="6004" id="6004"
class="graf graf--pre graf-after--pre"><code class="markup--code markup--pre-code">> git checkout HEAD~3</code></pre>
<p name="b92d" id="b92d" class="graf graf--p graf-after--pre">Once you’re done browsing the repo’s
history, you can use <code class="markup--code markup--p-code">git checkout
<your-branch-name></code> to move <code class="markup--code markup--p-code">HEAD</code> back to
the front of the line (your most recent commit). For example, in our diagram above, we could use <code
class="markup--code markup--p-code">git checkout master</code> to take our <code
class="markup--code markup--p-code">HEAD</code> reference back to commit <code
class="markup--code markup--p-code">42ffa1</code>.</p>
<h3 name="c1e8" id="c1e8" class="graf graf--h3 graf-after--p">Why checkout?</h3>
<p name="6aa2" id="6aa2" class="graf graf--p graf-after--h3">Most of Git’s power comes from a simple
ability: viewing commits in the past and understanding how they connect. This is why mastering the <code
class="markup--code markup--p-code">git checkout</code> command is so important: it lets you think
more like Git and gives you full freedom of navigation without risking damage to the repo's
contents.</p>
<p name="5e68" id="5e68" class="graf graf--p graf-after--p">That said, you’ll likely use shortcuts like
<code class="markup--code markup--p-code">git checkout -</code> far more often than specifically
checking out commit hashes. Especially with the advent of user-friendly tools like GitHub, it's much
easier to visualize changes outside the command line. We'll demonstrate browsing commit histories on
GitHub in a future lesson.</p>
<h3 name="03f4" id="03f4" class="graf graf--h3 graf-after--p">Git ‘Do-Overs’: Reset & Rebase</h3>
<figure name="45ae" id="45ae" class="graf graf--figure graf-after--h3"><img class="graf-image"
data-image-id="0*5yBLWI2EMAqgm01n.jpg" data-width="638" data-height="359"
src="https://cdn-images-1.medium.com/max/800/0*5yBLWI2EMAqgm01n.jpg"></figure>
<p name="9cd3" id="9cd3" class="graf graf--p graf-after--figure">Git is designed to protect you — not only
from others, but also from yourself! Of course, there are times where you’d like to exercise your own
judgement, even if it may not be the best thing to do. For this, Git provides some helpful tools to
change commits and “time travel”.</p>
<p name="abb9" id="abb9" class="graf graf--p graf-after--p">Before we talk about these, a warning: <strong
class="markup--strong markup--p-strong">The commands in this lesson are destructive!</strong> If used
improperly, you could lose work, damage a teammate’s branch, or even rewrite the history of your entire
project. You should exercise caution when using these on production code, and don’t hesitate to ask for
help if you’re unsure what a command might do.</p>
<p name="8162" id="8162" class="graf graf--p graf-after--p">After this lesson, you should:</p>
<p name="bf70" id="bf70" class="graf graf--p graf-after--p">Be able to roll back changes to particular
commit.</p>
<p name="53f2" id="53f2" class="graf graf--p graf-after--p">Have an understanding of how rebasing affects
your commit history.</p>
<p name="1f82" id="1f82" class="graf graf--p graf-after--p">Know when to rebase/reset and when <strong
class="markup--strong markup--p-strong">not</strong> to.</p>
<h3 name="1750" id="1750" class="graf graf--h3 graf-after--p">Resetting the past</h3>
<p name="ab80" id="ab80" class="graf graf--p graf-after--h3">Remember how our commits form a timeline? We
can see the state of our project at any point using <code class="markup--code markup--p-code">git
checkout</code>. What if we want to travel back in time to a point before we caused a new bug or chose
a terrible font? <code class="markup--code markup--p-code">git reset</code> is the answer!</p>
<blockquote name="3261" id="3261" class="graf graf--blockquote graf-after--p"><em
class="markup--em markup--blockquote-em">Resetting</em> involves moving our <code
class="markup--code markup--blockquote-code">HEAD</code> ref back to a different commit.</blockquote>
<p name="d898" id="d898" class="graf graf--p graf-after--blockquote">No matter how we reset, <code
class="markup--code markup--p-code">HEAD</code> will move with us. Unlike <code
class="markup--code markup--p-code">git checkout</code>, this will also destroy intermediate commits.
We can use some additional flags to determine how our code changes are handled.</p>
<h3 name="6d40" id="6d40" class="graf graf--h3 graf-after--p">Soft resets</h3>
<p name="9ce7" id="9ce7" class="graf graf--p graf-after--h3">The least-dangerous reset of all is <code
class="markup--code markup--p-code">git reset --soft</code>.</p>
<p name="d681" id="d681" class="graf graf--p graf-after--p">A soft reset will move our <code
class="markup--code markup--p-code">HEAD</code> ref to the commit we've specified, and will leave
any intermediate changes in the staging area.</p>
<p name="5d1c" id="5d1c" class="graf graf--p graf-after--p">This means you won't lose any code, though
you will lose commit messages.</p>
<p name="3af8" id="3af8" class="graf graf--p graf-after--p">A practical example of when a soft reset would
be handy is joining some small commits into a larger one. We’ll pretend we’ve been struggling with
“their”, “there”, and “they’re” in our app. Here’s our commit history:Those commit messages aren’t
great: they’re not very explanatory, and they don’t provide a lot of value in our commit history. We’ll
fix them with a soft reset:</p>
<pre name="832c" id="832c"
class="graf graf--pre graf-after--p"><code class="markup--code markup--pre-code">git reset --soft 9c5e2fc</code></pre>
<p name="4528" id="4528" class="graf graf--p graf-after--pre">This moves our <code
class="markup--code markup--p-code">HEAD</code> ref back to our first commit. Looking at our commit
log now, we might be worried we've lost our changes:</p>
<figure name="2afe" id="2afe" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*slbLxXdNbv3L7UsCHGdCIg.png" data-width="738" data-height="365"
src="https://cdn-images-1.medium.com/max/800/1*slbLxXdNbv3L7UsCHGdCIg.png"></figure>
<p name="1c54" id="1c54" class="graf graf--p graf-after--figure">Our changes are still present in the
staging area, ready to be re-committed when we’re ready! We can use <code
class="markup--code markup--p-code">git commit</code> to re-apply those changes to our commit history
with a new, more helpful message instead:</p>
<figure name="ff58" id="ff58" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*UleGR-ijDRZw4UqbEbAk0Q.png" data-width="757" data-height="42"
src="https://cdn-images-1.medium.com/max/800/1*UleGR-ijDRZw4UqbEbAk0Q.png"></figure>
<p name="b58d" id="b58d" class="graf graf--p graf-after--figure">Notice that the new commit has a totally
new hash. The old commit messages (and their associated hashes) have been lost, but our code changes are
safe and sound!</p>
<h3 name="e62b" id="e62b" class="graf graf--h3 graf-after--p">Risky Business: Mixed resets</h3>
<p name="d828" id="d828" class="graf graf--p graf-after--h3">If soft resets are the safest form of <code
class="markup--code markup--p-code">git reset</code>, mixed resets are the most average! This is
exactly why they're the default: running <code class="markup--code markup--p-code">git reset</code>
without adding a flag is the same as running <code class="markup--code markup--p-code">git reset
--mixed</code>.</p>
<p name="de9f" id="de9f" class="graf graf--p graf-after--p">In a mixed reset, your changes are preserved,
but they’re moved from the commit history directly to the working directory. This means you’ll have to
use <code class="markup--code markup--p-code">git add</code> to choose everything you want in future
commits.</p>
<p name="446e" id="446e" class="graf graf--p graf-after--p">Mixed resets are a good option when you want
to alter a change in a previous commit. Let’s use a mixed reset with our “their”, “there”, “they’re”
example again.</p>
<p name="a7a9" id="a7a9" class="graf graf--p graf-after--p">We’ll start with “they’re”:</p>
<figure name="396b" id="396b" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*I71W4NpNdNz8NPr7i2tcAQ.png" data-width="772" data-height="489"
src="https://cdn-images-1.medium.com/max/800/1*I71W4NpNdNz8NPr7i2tcAQ.png"></figure>
<p name="8c54" id="8c54" class="graf graf--p graf-after--figure">Notice again that you don’t lose your
code with a mixed reset, but you do lose your commit messages & hashes. The difference between <code
class="markup--code markup--p-code">--soft</code> and <code
class="markup--code markup--p-code">--mixed</code> comes down to whether you'll be keeping the
code exactly the same before re-committing it or making changes.</p>
<h3 name="dedd" id="dedd" class="graf graf--h3 graf-after--p">Hard resets</h3>
<p name="3ce0" id="3ce0" class="graf graf--p graf-after--h3">Hard resets are the most dangerous type of
reset in Git. Hard resets adjust your <code class="markup--code markup--p-code">HEAD</code> ref and <em
class="markup--em markup--p-em">totally destroy any interim code changes</em>. Poof. Gone forever.</p>
<p name="f073" id="f073" class="graf graf--p graf-after--p">There are very few good uses for a hard reset,
but one is to get yourself out of a tight spot. Let’s say you’ve made a few changes to your repository
but you now realize those changes were unnecessary. You’d like to move back in time so that your code
looks exactly as it did before any changes were made. <code class="markup--code markup--p-code">git
reset --hard</code> can take you there.</p>
<p name="8262" id="8262" class="graf graf--p graf-after--p">It’s our last round with “their”, “there”, and
“they’re”. We’ve tried it all three ways and decided we don’t need to use that word at all! Let’s walk
through a hard reset to get rid of our changes.</p>
<p name="4876" id="4876" class="graf graf--p graf-after--p">We’ll start in the same place we began for our
soft reset:</p>
<figure name="3b3a" id="3b3a" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*PHWuTBoZeQ1L2hgRdXu6fg.png" data-width="756" data-height="122"
src="https://cdn-images-1.medium.com/max/800/1*PHWuTBoZeQ1L2hgRdXu6fg.png"></figure>
<p name="41c4" id="41c4" class="graf graf--p graf-after--figure">It turns out that we’ll be using a video
on our homepage and don’t need text at all! Let’s step back in time:</p>
<pre name="f863" id="f863"
class="graf graf--pre graf-after--p"><code class="markup--code markup--pre-code">git reset --hard 9c5e2fc</code></pre>
<p name="3d9e" id="3d9e" class="graf graf--p graf-after--pre">Our Git log output is much simpler now:</p>
<figure name="1d92" id="1d92" class="graf graf--figure graf-after--p"><img class="graf-image"
data-image-id="1*1NovgUyM_ed7h5MozcBY-g.png" data-width="651" data-height="219"
src="https://cdn-images-1.medium.com/max/800/1*1NovgUyM_ed7h5MozcBY-g.png"></figure>
<p name="110d" id="110d" class="graf graf--p graf-after--figure">It’s empty — no changes in your working
directory and no changes in your staging area. This is major difference between a hard reset and a
soft/mixed reset: you will lose <em class="markup--em markup--p-em">all your changes</em> back to the
commit you’ve reset to.</p>
<p name="252f" id="252f" class="graf graf--p graf-after--p">If your teammate came rushing in to tell you
that the boss has changed their mind and wants that homepage text after all, you’re going to be re-doing
all that work! Be very confident that the changes you’re losing are unimportant before embarking on a
hard reset.</p>
<h3 name="6e14" id="6e14" class="graf graf--h3 graf-after--p">Rebase: ‘Alt-time travel’</h3>
<figure name="e9c9" id="e9c9" class="graf graf--figure graf-after--h3"><img class="graf-image"
data-image-id="0*C_Y5Tr_o5BAS1l-3.jpeg" data-width="868" data-height="546"
src="https://cdn-images-1.medium.com/max/800/0*C_Y5Tr_o5BAS1l-3.jpeg"></figure>
<p name="79ef" id="79ef" class="graf graf--p graf-after--figure">Sometimes we want to change more than a
few commits on a linear timeline. What if we want to move multiple commits across branches? <code
class="markup--code markup--p-code">git rebase</code> is the tool for us!</p>
<p name="4501" id="4501" class="graf graf--p graf-after--p"><em
class="markup--em markup--p-em">Rebasing</em> involves changing your current branch’s base branch. We
might do this if we accidentally started our branch from the wrong commit or if we’d like to incorporate
changes from another branch into our own.</p>
<blockquote name="a906" id="a906" class="graf graf--blockquote graf-after--p">Isn’t that the same as git
merge?</blockquote>
<p name="2bf2" id="2bf2" class="graf graf--p graf-after--blockquote"><code
class="markup--code markup--p-code">git merge</code>?" In almost all cases, you'd be right.
Rebasing is a dangerous process that effectively rewrites history.</p>
<h3 name="d09c" id="d09c" class="graf graf--h3 graf-after--p">I see you too like to live life Dangerously…
tell me about Rebase..</h3>