-
Notifications
You must be signed in to change notification settings - Fork 2
/
understanding-git.tex
1946 lines (1728 loc) · 56.1 KB
/
understanding-git.tex
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
\documentclass{beamer}
%\documentclass[notes]{beamer}
%\documentclass[notes=only]{beamer}
\usepackage{tikz}
\usepackage{gitdags}
\usepackage{wrapfig}
\usepackage{xcolor}
\usepackage{xcolor-solarized}
\usepackage{soul}
\usepackage{multicol}
\usetheme{Antibes}
\usecolortheme{dolphin}
\newcommand\gitcmd[1]{\texttt{git #1}}
\newcommand\gitsubcmd[1]{\texttt{#1}}
\newcommand\gflag[1]{\texttt{#1}}
\newcommand\grefspec[1]{\texttt{#1}}
\newcommand\gbranch[1]{\texttt{#1}}
\newcommand\gremotebranch[1]{\texttt{#1}}
\newcommand\gtag[1]{\texttt{#1}}
\newcommand\gHEAD{\texttt{HEAD}}
\newcommand\gremote[1]{\texttt{#1}}
\newcommand\goal[1]{\textbf{Goal:} #1}
\newcommand\defaultrepo{%
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{above=of D} {D}
\gitbranch{topic}
{above=of E} {E}
\gitHEAD
{above=of topic} {topic}
\end{tikzpicture}
\end{figure}
}
\title{Understanding Git}
\author{Johann Tutor}
\date{August 2, 2019}
\makeatletter
\hypersetup{
pdftitle={\@title (\@date)},
pdfauthor={\@author}
}
\makeatother
\begin{document}
\frame{\titlepage\begin{center}\includegraphics[height=\baselineskip]{cc-by-sa.eps}\end{center}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
\begin{frame}
\frametitle{Aims}
By the end of this presentation, you should be able to:
\begin{itemize}
\item Read Git graphs.
\item Visualize actions done on a Git repository.
\item Understand what commits and branches are and how they interact.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Assumptions}
\begin{itemize}
\item You are familiar enough with Git commands to use them.
\item You understand the Git workflow (\gitsubcmd{add}, \gitsubcmd{commit}, \gitsubcmd{push}, etc.).
\item You have a vague idea of what commits and branches are.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{What is Git?}
\framesubtitle{``The stupid content tracker''}
\begin{itemize}
\item A distributed version control system (DVCS).
\item Created in 2005 by Linus Torvalds to replace BitKeeper for Linux kernel development.
\item Not GitHub.
\end{itemize}
\vfill
\begin{block}{\texttt{man git}}
``Git is a fast, scalable, distributed revision control system with an
unusually rich command set that provides both high-level operations and
full access to internals.''
\end{block}
\end{frame}
\begin{frame}
\frametitle{Reading Git graphs}
Git repositories are often represented using a directed acyclic graph (DAG).
\note{
\textbf{Directed:} The graph edges are one-way.\\
\textbf{Acyclic:} There is no way to return to a node once you have passed it.\\
\textbf{Graph:} A mathematical model with nodes and edges that connect nodes.
A DAG forms a tree structure, and you can refer to a connected Git DAG as a tree.\\
}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
{Commit A} -- {Commit B} -- {
D,
C -- E
} -- {Merge commit}
};
\gittag{tag}
{above=of Commit B} {Commit B}
\gitbranch{branch1}
{above=of Merge commit} {Merge commit}
\gitremotebranch{remote/branch1}
{above=of D} {D}
\gitbranch{branch2}
{right=of E} {E}
\gitHEAD
{left=of branch1} {branch1}
\end{tikzpicture}
\end{figure}
\vfill
\begin{block}{Arrows}
The arrows point to the commit's parent(s), not the flow of time.
\note{Think of the arrows as representing a ``depends on'' relationship.}
\end{block}
\end{frame}
\begin{frame}
\frametitle{Our reference repository}
\defaultrepo
\note[item]{The initial commit is \grefspec{A}.}
\note[item]{There are two branches, \gbranch{master} and \gbranch{topic}.}
\note[item]{Commit \grefspec{D} was made after \gbranch{topic} split from the mainline.}
\end{frame}
\begin{frame}
\frametitle{Outline}
\begin{multicols}{2}
\tableofcontents
\end{multicols}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Commits}
\begin{frame}
\sectionpage
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranchfade{master}
{above=of D} {D}
\gitremotebranchfade{origin/master}
{right=of master} {D}
\gitbranchfade{topic}
{above=of E} {E}
\end{tikzpicture}
\end{figure}
\end{frame}
\subsection{Introduction}
\begin{frame}
\frametitle{What is a commit?}
\begin{itemize}
\item Commits record the changes of a repository.
\item Commits also include a message, a timestamp (usually of creation), and an author.
\item Except for the first commit, commits reference one or more parent commits.
\note[item]{More precisely, root commits have no parent. The first commit is a root commit, but a repo can have multiple root commits.}
\item Commits are identified by a SHA-1 hash. This hash can be abbreviated to its unique prefix (usually 7).
\note[item]{The minimum abbreviated hash length is 4 characters.}
\note[item]{Commits are effectively read-only because changing any of the properties of a commit changes its hash.}
\note[item]{Linux Torvalds on SHA-1 collisions: \url{https://marc.info/?l=git&m=148787047422954}}
\item The bulk of the Git model is the DAG of commits.
\note[item]{The rest of the Git model is the plumbing and the Git file system. This is highly out of scope for this presentation.}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{What is a commit?}
In our reference repository, objects \grefspec{A} through \grefspec{E} are all commits.
\defaultrepo
\end{frame}
\begin{frame}
\frametitle{Committing}
Committing occurs in two steps:
\begin{enumerate}
\item Staging: moving changes from the working tree into the staging area.
\item Committing: recording changes from the staging area into a commit.
\end{enumerate}
\begin{figure}
\centering
\begin{tikzpicture}
\SAandWT
\end{tikzpicture}
\end{figure}
\vfill
\begin{block}{Working tree}
The tree of actual checked-out files.
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\gitcmd{add}}
\begin{frame}
\frametitle{\gitcmd{add}}
\framesubtitle{``Add file contents to the index''}
\begin{itemize}
\item Stages changes to commit.
\item Files can be selectively staged by specifying them as arguments.
\item Hunks can be selectively staged by specifying the \gflag{-p} flag.
\end{itemize}
\vfill
\begin{block}{Changes after staging}
If changes are made to a file after the file has been staged, the new changes are not automatically staged; they must be staged separately. Staged changes are cumulative.
\end{block}
\end{frame}
\begin{frame}
\frametitle{Exercise}
The file \texttt{README} contains changes.
\goal{Stage these changes.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{right=of D} {D}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{above=of topic} {topic}
\SAandWT
\gitblob[left=of workingtree]{README}
\end{tikzpicture}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\gitcmd{add README}}
\framesubtitle{Stage the changes of \texttt{README}.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{right=of D} {D}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{above=of topic} {topic}
\SAandWT
\gitblob[left=of stagingarea]{README}
\end{tikzpicture}
\end{figure}
The changes in \texttt{README} have been moved to the staging area.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\gitcmd{commit}}
\begin{frame}
\frametitle{\gitcmd{commit}}
\framesubtitle{``Record changes to the repository''}
\begin{itemize}
\item Creates a revision (commit) from the staged changes.
\item Advances \gHEAD{} and the current branch to the new commit.
\end{itemize}
\vfill
\begin{block}{\gHEAD{}}
A reference to the current branch or commit from which the working tree is derived. In other words, \gHEAD{} is what is currently checked out.
\end{block}
\end{frame}
\begin{frame}
\frametitle{Exercise}
The changes in \texttt{README} have been staged.
\goal{Commit the staged changes.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{right=of D} {D}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{above=of topic} {topic}
\SAandWT
\gitblob[left=of stagingarea]{README}
\end{tikzpicture}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\gitcmd{commit}}
\framesubtitle{Commit the changes in the staging area.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E -- F
}
};
\gitbranch{master}
{right=of D} {D}
\gitbranch{topic}
{above=of F} {F}
\gitHEAD
{right=of topic} {topic}
\SAandWT
\end{tikzpicture}
\end{figure}
After entering a commit message in the editor, commit \grefspec{F} is added containing the staged changes.
\gHEAD{} and \gbranch{topic} are advanced to \grefspec{F}.
\note{
The commit message can be entered on the command line using the \gflag{-m} (\gflag{--message}) flag, with multiple \gflag{-m} flags being combined as multiple paragraphs.
Using this flag may create bad commit message habits, so consider using \gflag{-m} to start your message
and additionally pass the \gflag{-e} (\gflag{--edit}) flag to bring up the editor to continue editing your message.
}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\gitcmd{cherry-pick}}
\begin{frame}
\frametitle{\gitcmd{cherry-pick}}
\framesubtitle{``Apply the changes introduced by some existing commits''}
\begin{itemize}
\item Takes the changes from an existing commit and creates a new commit from them under the current \gHEAD{}.
\item \gHEAD{} and the current branch are advanced to the new commit.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Exercise}
The \gbranch{master} branch is currently checked out.
Commit \grefspec{E} contains a useful change that you want to integrate right away.
\goal{Cherry-pick \grefspec{E} into the current branch.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{above=of D} {D}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{left=of master} {master}
\end{tikzpicture}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\gitcmd{cherry-pick E}}
\framesubtitle{Create a new commit from the changes in \grefspec{E}.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D -- E$'$,
C -- E
}
};
\gitbranch{master}
{above=of E$'$} {E$'$}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{left=of master} {master}
\end{tikzpicture}
\end{figure}
A new commit, \grefspec{E$'$}, that contains the same changes as \grefspec{E} was added on top of \grefspec{D}. \gHEAD{} and \gbranch{master} were advanced to \grefspec{E$'$}.
\note{
Cherry-picking may result in merge conflicts later on.
Raymond Chen suggests using a merge-based strategy instead:
{\scriptsize\url{https://blogs.msdn.microsoft.com/oldnewthing/20180323-01/?p=98325}}
}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\gitcmd{revert}}
\begin{frame}
\frametitle{\gitcmd{revert}}
\framesubtitle{``Revert some existing commits''}
\begin{itemize}
\item Creates new commits that have the reverse patches of the given commits.
\item Advances \gHEAD{} and the current branch to the last of the new commits.
\end{itemize}
\vfill
\begin{block}{Creating a single revert commit}
\gitcmd{revert} creates a commit for each given commit by default.
To create a single revert commit can be created instead by passing the \gflag{-n} flag and committing the result.
\end{block}
\note{
To undo the effects of a revert commit, revert the revert commit.
}
\end{frame}
\begin{frame}
\frametitle{Exercise}
The \gbranch{master} branch is currently checked out.
Commit \grefspec{A} is problematic and must be reverted.
\goal{Revert commit \grefspec{A} (and only \grefspec{A}) non-destructively.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{above=of D} {D}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{left=of master} {master}
\end{tikzpicture}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\gitcmd{revert A}}
\framesubtitle{Create a new commit with the reverse patch of \grefspec{A}.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D -- A$^{-1}$,
C -- E
}
};
\gitbranch{master}
{above=of A$^{-1}$} {A$^{-1}$}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{left=of master} {master}
\end{tikzpicture}
\end{figure}
A new commit, \grefspec{A$^{-1}$}, that contains the reverse changes as \grefspec{A} was added on top of \grefspec{D}.
\gHEAD{} and \gbranch{master} were advanced to \grefspec{A$^{-1}$}.
\end{frame}
\begin{frame}
\frametitle{Exercise}
The \gbranch{master} branch is currently checked out.
Commits \grefspec{B} and \grefspec{D} were found to be problematic and need to be reverted.
\goal{Revert commits \grefspec{B} and \grefspec{D} non-destructively in a single commit.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{above=of D} {D}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{left=of master} {master}
\end{tikzpicture}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Attempt 1: \gitcmd{revert B..D}}
\framesubtitle{Revert the commits using the default commit strategy.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D -- D$^{-1}$ -- B$^{-1}$,
C -- E
}
};
\gitbranch{master}
{above=of B$^{-1}$} {B$^{-1}$}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{left=of master} {master}
\end{tikzpicture}
\end{figure}
By default, \gitcmd{revert} will create one commit for each commit being reverted.
\note{
The revert commits are in reverse order because the range \texttt{..} is used.
If the commits were passed individually, they would be reverted in the order given.
}
\end{frame}
\begin{frame}
\frametitle{Attempt 2: \texttt{\gitcmd{revert -n B..D} \&\& \gitcmd{commit}}}
\framesubtitle{Revert the commits and manually commit the result.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D -- DB$^{-1}$,
C -- E
}
};
\gitbranch{master}
{above=of DB$^{-1}$} {DB$^{-1}$}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{left=of master} {master}
\end{tikzpicture}
\end{figure}
By using the \gflag{-n} flag, \gitcmd{revert} will batch the changes and wait for you to commit the result.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Branches}
\begin{frame}
\sectionpage
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{[nodes=unreachable]
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{above=of D} {D}
\gitremotebranchfade{origin/master}
{right=of master} {D}
\gitbranch{topic}
{above=of E} {E}
\end{tikzpicture}
\end{figure}
\end{frame}
\subsection{Introduction}
\begin{frame}
\frametitle{What is a branch?}
\begin{itemize}
\item Branches represent a line of development.
\item Branches are lightweight labels that reference commits.
\item Branches should \textbf{not} be thought of as a series of commits.
\note{
The last point is worth repeating: branches should \textbf{not} be thought of as a series of commits.
Much confusion about how branch operations in Git work can be caused by thinking of branches as a series of commits.
Though it seems to contradict the first point, upon closer inspection, it does not.
}
\end{itemize}
\vfill
\begin{block}{\gbranch{master}}
The default branch is called \gbranch{master}, but neither this branch nor name is special to Git.
\end{block}
\end{frame}
\begin{frame}
\frametitle{What is a branch?}
In our reference repository:
\begin{itemize}
\item \gbranch{master} is a branch pointing to \grefspec{D} representing the line of development from \grefspec{A} to \grefspec{D}.
\item \gbranch{topic} is a branch pointing to \grefspec{E} representing the line of development from \grefspec{A} to \grefspec{E}.
\end{itemize}
\defaultrepo
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\gitcmd{branch}}
\begin{frame}
\frametitle{\gitcmd{branch}}
\framesubtitle{``List, create, or delete branches''}
\begin{itemize}
\item Creates a branch with the specified name from the specified commit, defaulting to the current commit.
\item With the \gflag{-d} or \gflag{-D} flags, it deletes the branch.
\item Does not move \gHEAD{}.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Exercise}
The repository is checked out at \gbranch{topic}.
\goal{Without moving \gHEAD{}, create a new branch called \gbranch{newbranch} on the current commit.}
\defaultrepo
\end{frame}
\begin{frame}
\frametitle{\gitcmd{branch newbranch}}
\framesubtitle{Create branch \gbranch{newbranch} on the current commit.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{above=of D} {D}
\gitbranch{topic}
{above=of E} {E}
\gitbranch{newbranch}
{right=of topic} {E}
\gitHEAD
{above=of topic} {topic}
\end{tikzpicture}
\end{figure}
A branch called \gbranch{newbranch} is created on the current commit and \gHEAD{} is not moved.
\end{frame}
\begin{frame}
\frametitle{\gitcmd{branch -d newbranch}}
\framesubtitle{Delete the branch \gbranch{newbranch}.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{above=of D} {D}
\gitbranch{topic}
{above=of E} {E}
\gitHEAD
{above=of topic} {topic}
\end{tikzpicture}
\end{figure}
The branch \gbranch{newbranch} has been deleted.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\gitcmd{tag}}
\begin{frame}
\frametitle{\gitcmd{tag}}
\framesubtitle{``Create, list, delete or verify a tag object signed with GPG''}
\begin{itemize}
\item Creates a lightweight tag with the given name on the specified commit, defaulting to the current commit.
\item With the \gflag{-a} flag, it creates an annotated tag (a tag with a message).
\item \gHEAD{} is not moved.
\end{itemize}
\vfill
\begin{block}{Tags}
Tags, like branches, are labels that reference commits. They differ from branches in that tags do not move. Tags are typically used to mark important points in the project's history, like releases.
\end{block}
\end{frame}
\begin{frame}
\frametitle{Exercise}
The repository is checked out at the \gbranch{topic} branch.
\goal{Tag commit \grefspec{B} with the tag \gtag{v1.0.0}.}
\defaultrepo
\end{frame}
\begin{frame}
\frametitle{\gitcmd{tag v1.0.0 B}}
\framesubtitle{Tag commit \grefspec{B} with lightweight tag \gtag{v1.0.0}.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gittag[v1-0-0]{v1.0.0}
{above=of B} {B}
\gitbranch{master}
{above=of D} {D}
\gitbranch{topic}
{above=of E} {E}
\gitHEAD
{above=of topic} {topic}
\end{tikzpicture}
\end{figure}
Commit \grefspec{B} has been tagged with the tag \gtag{v1.0.0}. \gHEAD{} has not been moved.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\gitcmd{checkout}}
\begin{frame}
\frametitle{\gitcmd{checkout}}
\framesubtitle{``Switch branches or restore working tree files''}
\begin{itemize}
\item Moves \gHEAD{} to the specified branch, commit or tag.
\item Updates the files in the working tree to match those in the commit.
\end{itemize}
\vfill
\begin{block}{\gitcmd{checkout -b newbranch}}
The \gflag{-b} flag is transactionally equivalent to doing a\\
\hspace{2em}\gitcmd{branch newbranch}\\
followed by a\\
\hspace{2em}\gitcmd{checkout newbranch}.
\end{block}
\note{
Another useful trick is to pass a \texttt{-} as the argument,
which checks out the last branch, commit or tag that you performed a \gitsubcmd{checkout} on.
Read the man page to see how you can go even further back.
}
\end{frame}
\begin{frame}
\frametitle{Exercise}
\gHEAD{} is at the \gbranch{topic} branch.
\goal{Move \gHEAD{} to the \gbranch{master} branch.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{right=of D} {D}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{above=of topic} {topic}
\SAandWT
\toWTfrom{E}
\end{tikzpicture}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\gitcmd{checkout master}}
\framesubtitle{Move \gHEAD{} to the tip of \gbranch{master} (\grefspec{D}) and update the working tree.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{right=of D} {D}
\gitbranch{topic}
{right=of E} {E}
\gitHEAD
{right=of master} {master}
\SAandWT
\toWTfrom{D}
\end{tikzpicture}
\end{figure}
\gHEAD{} is moved to \gbranch{master} and the working tree is updated to the contents of commit \grefspec{D}.
\end{frame}
\begin{frame}
\frametitle{Detached \gHEAD{}}
\begin{itemize}
\item Directly checking out a commit or tag results in a ``detached \gHEAD{}''.
\note[item]{A detached \gHEAD{} results in any situation where the ref that is checked out cannot normally be updated. Tags cannot be updated by issuing a \gitcmd{commit}, for example, so checking out a tag results in a detached \gHEAD{}.}
\item Commits from this commit may be made, but they will be lost unless a branch or tag is made.
\end{itemize}
\vfill
\begin{block}{Unreachable and dangling commits}
Unreachable commits are commits that are not referenced by a branch or tag. Dangling commits are unreachable commits that aren't referenced by another commit. Unreachable commits may be checked out using their SHA-1 hash until they are garbage collected.
\note[item]{By default, garbage collection usually removes unneeded objects after two weeks. See the man page for \gitcmd{gc} for details.}
\end{block}
\end{frame}
\begin{frame}
\frametitle{\gitcmd{checkout B}}
\framesubtitle{Move \gHEAD{} to commit \grefspec{B} and update the working tree.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E
}
};
\gitbranch{master}
{above=of D} {D}
\gitbranch{topic}
{above=of E} {E}
\gitHEAD
{above=of B} {B}
\SAandWT
\toWTfrom{B}
\end{tikzpicture}
\end{figure}
Checking out commit \grefspec{B} results in a detached \gHEAD{}.
\end{frame}
\begin{frame}
\frametitle{\gitcmd{commit}}
\framesubtitle{Make a new commit \grefspec{F} off \grefspec{B}.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C -- E,
F
}
};
\gitbranch{master}
{above=of D} {D}
\gitbranch{topic}
{above=of E} {E}
\gitHEAD
{right=of F} {F}
\end{tikzpicture}
\end{figure}
\grefspec{F} will become a dangling commit if \grefspec{HEAD} moves elsewhere.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\gitcmd{reset}}
\begin{frame}
\frametitle{\gitcmd{reset}}
\framesubtitle{``Reset current HEAD to the specified state''}
\begin{itemize}
\item Moves \gHEAD{} and the current branch to the given commit.
\item With the \gflag{--hard} flag, it also resets the working tree.
Any changes to tracked files are discarded.
\end{itemize}
\vfill
\end{frame}
\begin{frame}
\frametitle{Exercise}
The \gbranch{topic} branch is checked out. Commits \grefspec{C} and \grefspec{E} were committed in error, but the changes are still relevant.
\goal{Reset \gbranch{topic} to commit \grefspec{B} without discarding changes in the working tree.}
\defaultrepo
\end{frame}
\begin{frame}
\frametitle{\gitcmd{reset B}}
\framesubtitle{Point \gbranch{topic} and \gHEAD{} to commit \grefspec{B}.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C[unreachable] -- E[unreachable]
}
};
\gitbranch{master}
{right=of D} {D}
\gitbranch{topic}
{below=of B} {B}
\gitHEAD
{left=of topic} {topic}
\SAandWT
\toWTfrom{C}
\toWTfrom{E}
\end{tikzpicture}
\end{figure}
\gHEAD{} and \gbranch{topic} have been moved to \grefspec{B}.
The changes from \grefspec{C} and \grefspec{E} stay in the working tree.
\grefspec{C} and \grefspec{E} have become unreachable.
\end{frame}
\begin{frame}
\frametitle{Exercise}
The \gbranch{topic} branch is checked out. Commits \grefspec{C} and \grefspec{E} were committed in error.
\goal{Reset \gbranch{topic} to commit \grefspec{B}, discarding changes.}
\defaultrepo
\end{frame}
\begin{frame}
\frametitle{\gitcmd{reset --hard B}}
\framesubtitle{Point \gbranch{topic} and \gHEAD{} to commit \grefspec{B}, discarding changes.}
\begin{figure}
\centering
\begin{tikzpicture}
\gitDAG{
A -- B -- {
D,
C[unreachable] -- E[unreachable]
}
};
\gitbranch{master}
{right=of D} {D}
\gitbranch{topic}
{below=of B} {B}
\gitHEAD
{left=of topic} {topic}
\SAandWT
\toWTfrom{B}
\end{tikzpicture}
\end{figure}
\gHEAD{} and \gbranch{topic} have been moved to \grefspec{B}.
The working tree has been reset to the contents of \grefspec{B}.
\grefspec{C} and \grefspec{E} have become unreachable.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\gitcmd{rebase}}