basics.tex 15.1 KB
Newer Older
1 2
\documentclass[slidestop]{beamer}

3 4
\input{../shared/shared.tex}

5
\author{}
6 7
\title{\courseTitle}
\providecommand{\mySubTitle}{Git Basics}
8
\providecommand{\myConference}{\courseTitle}
9
\providecommand{\myGroup}{}
Mihai's avatar
Mihai committed
10
\providecommand{\myDepartment}{}
11
\providecommand{\myCenter}{}
12

13 14
\usetheme{lumc}

Mihai's avatar
Mihai committed
15 16 17 18 19 20 21 22
\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Table of contents}
    \tableofcontents[currentsection, subsectionstyle=hide/hide]
  \end{frame}
}

23 24 25 26 27
\begin{document}

% This disables the \pause command, handy in the editing phase.
%\renewcommand{\pause}{}

28
% Make the title slide.
29
\makeTitleSlide{\includegraphics[width=3.5cm]{../shared/git_logo.pdf}}
30 31

% First page of the presentation.
Mihai's avatar
Mihai committed
32 33
\section{Getting started}
\subsection{To start a project you can either:}
34
\begin{pframe}
35 36 37 38 39 40 41 42
  1. Start from scratch on your own:
  \vspace{-0.3cm}
  \begin{framed}
   \prompt\ \cmd{mkdir}\ \cmdarg{my\_project}\\
   \prompt\ \cmd{cd}\ \cmdarg{my\_project}\\
   \prompt\ \gitcmd\ \cmd{init}\\
   \cliout{Initialized empty Git repository in \{current-directory\}\/.git\/}
  \end{framed}
43
  \pause
44

45 46 47
  2. Or you can \cmd{clone} an existing remote (or local) repository:
  \vspace{-0.3cm}
  \begin{framed}
Mihai's avatar
Mihai committed
48
   \prompt\ \gitcmd\ \cmd{clone}\ \cmdarg{\{path-to-repository\}}\\
49 50
   \cliout{Cloning into \{repo-name\} ...}
  \end{framed}
51
\end{pframe}
52

53
\subsection{Where does git store its repository information?}
54
\begin{pframe}
55
  You can see a hidden directory in a Git repository.
56 57 58 59 60
  \vspace{-0.3cm}
  \begin{framed}
   \prompt\ \cmd{ls}\ \cmdopt{-a}\\
   \cliout{. .. .git}
  \end{framed}
61

62 63 64 65 66
  This is where git stores the files necessary to track your progress. You
  rarely need to edit the contents of this directory.
  \bigskip

  \important{Removing this directory means removing your repository!}
67
\end{pframe}
68

Mihai's avatar
Mihai committed
69
\subsection{Quick setup}
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
\begin{pframe}
  If you have never used \gitcmd\ before you need to tell it who you are. This
  information is saved in \cmdarg{.gitconfig}\ and used to mark each commit.
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{config}\ \cmdopt{--global user.name}\
   \cmdarg{\{your-name-or-nick-name\}}\\
   \prompt\ \gitcmd\ \cmd{config}\ \cmdopt{--global user.email}\
   \cmdarg{\{your-email-address\}}
  \end{framed}
  \pause
  \vspace{-0.2cm}

  Local configuration for each repository is possible as well.
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{config}\ \cmdopt{--local user.name}\
   \cmdarg{\{your-name-or-nick-name\}}\\
   \prompt\ \gitcmd\ \cmd{config}\ \cmdopt{--local user.email}\
   \cmdarg{\{your-email-address\}}
  \end{framed}
  \vspace{-0.2cm}
  \pause

  You would like colored output?
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{config}\ \cmdopt{--global color.ui}\ \cmdarg{auto}
  \end{framed}
99
\end{pframe}
100

Mihai's avatar
Mihai committed
101 102 103 104 105 106 107 108 109 110 111 112 113
\subsection{Playing areas}
\begin{pframe}
  Git operations track files around these three \textbf{areas}
  \footnote[1]{\scriptsize{Adapted from the
  \href{http://git-scm.com/book/en/v2/Getting-Started-Git-Basics}
  {Pro Git Book}.}}:
  \begin{center}
    \includegraphics[width=0.7\textwidth]{images/git_areas.pdf}\\
  \end{center}
  \vspace{-0.4cm}
\end{pframe}

\section{Git Operations}
114
\subsection{Checking repository state}
115
\begin{pframe}
116 117 118 119
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{status}\\
   \cliout{nothing to commit (working directory clean)}
  \end{framed}
120
\end{pframe}
121

122
\begin{pframe}
Mihai's avatar
Mihai committed
123 124 125
  \begin{framed}
   \prompt\ \cmd{echo}\ \cmdarg{"First version."}\ \op{$>$}\ \cmdarg{README}
  \end{framed}
126
\end{pframe}
127

128
\begin{pframe}
Mihai's avatar
Mihai committed
129 130 131 132 133 134 135 136 137 138 139 140
  \begin{framed}
   \prompt\ \cmd{echo}\ \cmdarg{"First version."}\ \op{$>$}\ \cmdarg{README}\\
   \prompt\ \gitcmd\ \cmd{status}\\
   \cliout{On branch} \branch{master}\ \vspace{0.1cm}\\
   \cliout{Initial commit}\ \vspace{0.1cm} \\
   \cliout{Untracked files}\ \vspace{-0.1cm} \\
   \cliout{(use "git add <file>..." to include in}\
   \cliout{what will be committed)}\
   \hspace*{0.4cm}\untrackedfile{README}\ \vspace{0.1cm}\\
   \cliout{nothing added to commit but untracked files present}\ \vspace{-0.1cm}\\
   \cliout{(use "git add" to track)}
  \end{framed}
141
\end{pframe}
142

143 144 145 146 147 148 149
\subsection{Git file states}
\begin{pframe}
  Git itself will associate your files with various \textbf{states}
  \footnote[1]{\scriptsize{Adapted from the
  \href{http://git-scm.com/book/en/v2/Getting-Started-Git-Basics}
  {Pro Git Book}.}}:
  \begin{center}
150
    \includegraphics[width=0.75\textwidth]{images/file_status_cycles_00.pdf}\\
151 152
  \end{center}
\end{pframe}
Mihai's avatar
Mihai committed
153

154 155 156 157 158 159 160 161 162 163 164
\begin{pframe}
  Git itself will associate your files with various \textbf{states}
  \footnote[1]{\scriptsize{Adapted from the
  \href{http://git-scm.com/book/en/v2/Getting-Started-Git-Basics}
  {Pro Git Book}.}}:
  \begin{center}
    \includegraphics[width=0.75\textwidth]{images/file_status_cycles_01.pdf}\\
  \end{center}
\end{pframe}


Mihai's avatar
Mihai committed
165
\subsection{Adding/staging files}
166
\begin{pframe}
Mihai's avatar
Mihai committed
167 168 169 170 171
  To start tracking the file, we need to stage it first:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\
  \end{framed}
172
  \pause
173

Mihai's avatar
Mihai committed
174 175 176 177 178 179 180 181 182 183 184 185
  We can see that the state of the file has changed:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{status}\\
   \cliout{On branch} \branch{master}\ \vspace{0.1cm}\\
   \cliout{Initial commit}\ \vspace{0.1cm} \\
   \cliout{Changes to be committed:}\ \vspace{-0.1cm} \\
   \cliout{(use "git rm --cached <file>..." to unstage)}\
   \hspace*{0.4cm}\stagedfile{new file: README}\ \vspace{0.1cm}
  \end{framed}
  \vspace{-0.2cm}
  Note also that the file is now in the \stagedfile{staging area}.
186
\end{pframe}
bow's avatar
bow committed
187

188 189 190 191 192 193 194 195 196 197 198 199 200
\begin{pframe}
  To start tracking the file, we need to stage it first:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\
  \end{framed}

  We can see that the state of the file has changed:
  \begin{center}
    \includegraphics[width=0.75\textwidth]{images/file_status_cycles_add.pdf}\\
  \end{center}
\end{pframe}

Mihai's avatar
Mihai committed
201
\subsection{Commit}
202
\begin{pframe}
Mihai's avatar
Mihai committed
203 204 205 206 207 208 209 210 211 212 213 214
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{commit}\ \cmdopt{-m} \cmdarg{"First commit"}\\
   \cliout{[}\branch{master}\ \commit{5466170}\cliout{] First commit}\\
   \cliout{1 file changed, 1 insertion(+)}\\
   \cliout{create mode 100644 README}
  \end{framed}
  \vspace{-0.2cm}
  \pause
  If the commit message is long, you can omit the \cmdopt{-m} flag and \gitcmd\
  will open a text editor in which you can write your longer message.
  \medskip
bow's avatar
bow committed
215 216
  \pause

Mihai's avatar
Mihai committed
217 218 219 220 221 222 223
  We can see that the file state has changed:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{status}\\
   \cliout{On branch} \branch{master}\\
   \cliout{nothing to commit, working directory clean}
  \end{framed}
224
\end{pframe}
bow's avatar
bow committed
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239
\begin{pframe}
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{commit}\ \cmdopt{-m} \cmdarg{"First commit"}\\
   \cliout{[}\branch{master}\ \commit{5466170}\cliout{] First commit}\\
   \cliout{1 file changed, 1 insertion(+)}\\
   \cliout{create mode 100644 README}
  \end{framed}

  \begin{center}
    \includegraphics[width=0.75\textwidth]{images/file_status_cycles_commit.pdf}\\
  \end{center}
\end{pframe}

Mihai's avatar
Mihai committed
240
\subsection{}
241
\begin{pframe}
Mihai's avatar
Mihai committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  \vspace{-0.5cm}
  Let's update the file now:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \cmd{echo}\ \cmdarg{"Second version."}\ \op{$>$}\ \cmdarg{README}
  \end{framed}
  Git knows that there are changes to the file:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{status}\\
   \cliout{On branch} \branch{master}\\
   \cliout{Changes not staged for commit:}\ \vspace{-0.1cm} \\
   \cliout{(use "git add <file>..." to update what will be committed)}\ \vspace{-0.1cm} \\
   \cliout{(use "git checkout -- <file>..." to discard changes}\ \vspace{-0.1cm} \\
   \cliout{in working directory)}\\
   \hspace*{0.4cm}\modifiedfile{modified: README}\ \vspace{0.1cm}\\
   \cliout{no changes added to commit (use "git add"  and/or}\ \vspace{-0.1cm} \\
   \cliout{"git commit -a")}
  \end{framed}
261
\end{pframe}
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276
\begin{pframe}
  \vspace{-0.5cm}
  Let's update the file now:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \cmd{echo}\ \cmdarg{"Second version."}\ \op{$>$}\ \cmdarg{README}
  \end{framed}

  \begin{center}
    \includegraphics[width=0.75\textwidth]{images/file_status_cycles_edit.pdf}\\
  \end{center}
\end{pframe}


Mihai's avatar
Mihai committed
277
\subsection{Check the differences}
278
\begin{pframe}
Mihai's avatar
Mihai committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{diff}\\
   \cliout{diff --git i\/README w\/README}\ \vspace{-0.1cm}\\
   \cliout{index}\ \commit{efe6f7c}\cliout{..}\commit{4fe6328}\ \cliout{100644}\ \vspace{-0.1cm}\\
   \cliout{--- i\/README}\ \vspace{-0.1cm}\\
   \cliout{+++ w\/README}\ \vspace{-0.1cm}\\
   \diffsummary{@@ -1 +1 @@}\ \vspace{-0.1cm}\\
   \diffminus{-First version.}\ \vspace{-0.1cm}\\
   \diffplus{+Second version.}
  \end{framed}
  This compares the \modifiedfile{working directory} with the
  \stagedfile{staging area for}\ \stagedfile{the next commit}.
  The differences are what you could tell Git to further add to the staging area
  but you still haven't.
\end{pframe}
295

Mihai's avatar
Mihai committed
296 297 298 299 300 301 302
\begin{pframe}
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\\
   \prompt\ \gitcmd\ \cmd{diff}\\
   \prompt\
  \end{framed}
303
\end{pframe}
304

305
\begin{pframe}
Mihai's avatar
Mihai committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\\
   \prompt\ \gitcmd\ \cmd{diff}\\
   \prompt\ \gitcmd\ \cmd{diff}\ \cmdopt{--cached}\\
   \cliout{diff --git i\/README w\/README}\ \vspace{-0.1cm}\\
   \cliout{index}\ \commit{efe6f7c}\cliout{..}\commit{4fe6328}\ \cliout{100644}\ \vspace{-0.1cm}\\
   \cliout{--- i\/README}\ \vspace{-0.1cm}\\
   \cliout{+++ w\/README}\ \vspace{-0.1cm}\\
   \diffsummary{@@ -1 +1 @@}\ \vspace{-0.1cm}\\
   \diffminus{-First version.}\ \vspace{-0.1cm}\\
   \diffplus{+Second version.}
  \end{framed}
  \vspace{-0.3cm}
  This displays the changes you staged for the \textbf{next commit} relative to the
  \textbf{previous commit}.
322 323
  \pause

Mihai's avatar
Mihai committed
324 325
  You can also specify a specific \textbf{commit id} to which to compare the staged
  files with: \gitcmd\ \cmdarg{diff}\ \cmdopt{--cached}\ \cmdarg{<commit-id>}
326
\end{pframe}
327

Mihai's avatar
Mihai committed
328
\subsection{}
329
\begin{pframe}
Mihai's avatar
Mihai committed
330 331 332 333 334 335 336
  \vspace{-0.5cm}
  To stage and commit the file again we could use:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\\
   \prompt\ \gitcmd\ \cmd{commit}\ \cmdopt{-m}\ \cmdarg{"Second commit"}
  \end{framed}
337 338
  \pause

Mihai's avatar
Mihai committed
339 340 341 342 343 344 345 346 347 348
  \textbf{But}, since we have already tracked the file, you can also abbreviate
  this into one \gitcmd\ \cmd{commit}\ command:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{commit}\ \cmdopt{-am} \cmdarg{"Second commit"}\\
   \cliout{[}\branch{master}\ \commit{ef70f09}\cliout{] Second commit}\\
   \cliout{1 file changed, 1 insertion(+), 1 deletion(-)}
  \end{framed}
  Notice the \cmdopt{-a} flag. This tells \gitcmd\ to automatically stage all
  previously tracked files.
349
\end{pframe}
350

Mihai's avatar
Mihai committed
351

352 353 354 355 356 357 358 359 360 361 362 363 364
\subsection{Unstage}
\begin{pframe}
%   \vspace{-0.5cm}
  \begin{framed}
   \prompt\ \cmd{echo}\ \cmdarg{"Some mistake"} \op{$>>$}\ \cmdarg{README} \\
   \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README} \\
   \prompt\ \gitcmd\ \cmd{reset}\ \textcolor{head}{\lstinline{HEAD}} \cmdarg{README}
  \end{framed}
  \begin{center}
    \includegraphics[width=0.75\textwidth]{images/file_status_cycles_reset.pdf}\\
  \end{center}
\end{pframe}

Mihai's avatar
Mihai committed
365
\subsection{Check commit history}
366
\begin{pframe}
Mihai's avatar
Mihai committed
367 368 369 370 371 372 373 374 375 376 377 378 379
%   \vspace{-0.5cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{log}\\
   \commit{commit 7a6e47cfbb38048b46937d9f8d2427a7e6e20936}\ \vspace{-0.1cm}\\
   \cliout{Author: Zorro <zorro@poor.es>}\ \vspace{-0.1cm}\\
   \cliout{Date: Tue Nov 24 16:13:59 2015 +0100}\ \vspace{0.1cm}\\
   \hspace*{0.5cm}\cliout{Second commit}\ \vspace{0.1cm}\\
   \commit{commit 54661709e859427358c97a94475643a7ccffa052}\ \vspace{-0.1cm}\\
   \cliout{Author: Zorro <zorro@poor.es>}\ \vspace{-0.1cm}\\
   \cliout{Date: Tue Nov 24 15:04:54 2015 +0100}\ \vspace{0.1cm}\\
   \hspace*{0.5cm}\cliout{First commit}
  \end{framed}
\end{pframe}
380

381
\subsection{Restoring Previous Versions}
Mihai's avatar
Mihai committed
382 383 384 385 386 387 388 389 390 391 392 393 394
\begin{pframe}
  One that is used quite commonly is to discard a \modifiedfile{working directory}\
  file changes and \textbf{restore its latest repository state}:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{checkout}\ \cmdopt{--}\ \cmdarg{\{filename\}}
  \end{framed}
  To discard all changes in the \modifiedfile{working}\
  \modifiedfile{directory}:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{checkout}\ \cmdopt{--}\ \cmdarg{.}
  \end{framed}
395 396 397 398 399 400 401
  \pause
  \begin{center}
    \includegraphics[width=0.75\textwidth]{images/file_status_cycles_checkout.pdf}\\
  \end{center}
\end{pframe}

\begin{pframe}
Mihai's avatar
Mihai committed
402 403 404 405 406
  To retrieve a file from a specific commit into the \stagedfile{staging area}:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{checkout}\ \cmdarg{\{commit-id\}}\ \cmdarg{\{filename\}}
  \end{framed}
Mihai's avatar
Mihai committed
407
  It's up to you to include this file into a new commit.
408
\end{pframe}
409

Mihai's avatar
Mihai committed
410 411 412 413 414 415 416 417
\begin{pframe}
  To revert a commit there is a special command:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{revert}\ \cmdarg{\{commit-id\}}\
  \end{framed}
  Note that you will be asked to introduce a new commit message.
\end{pframe}
Mihai's avatar
Mihai committed
418

419
\section{Extras}
Mihai's avatar
Mihai committed
420
\subsection{Explicit not tracking}
421
\begin{pframe}
Mihai's avatar
Mihai committed
422
  Certain files are not suitable for tracking by \gitcmd :
423
  \begin{itemize}
Mihai's avatar
Mihai committed
424 425
   \item Binary files / executables;
   \item PDF files / Microsoft Office files.
426
  \end{itemize}
Mihai's avatar
Mihai committed
427 428 429
  Other files are also not meant to be tracked:
  \begin{itemize}
   \item Password-containing files;
Mihai's avatar
Mihai committed
430
   \item Large files - use git-annex for those.
Mihai's avatar
Mihai committed
431
  \end{itemize}
Mihai's avatar
Mihai committed
432
  \pause
Mihai's avatar
Mihai committed
433 434 435 436 437 438 439 440 441
  You can ignore these files by listing their names in a file called
  \cmdarg{.gitignore} in your repository directory root:
  \vspace{-0.4cm}
  \begin{framed}
    \prompt\ \cmd{echo}\ \cmdarg{"my_password.txt"} \op{$>>$}\ \cmdarg{.gitignore}\\
    \prompt\ \cmd{echo}\ \cmdarg{"*.pdf"} \op{$>>$}\ \cmdarg{.gitignore}\\
    \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{.gitignore}\\
    \prompt\ \gitcmd\ \cmd{commit}\ \cmdopt{-m}\ \cmdarg{"Add .gitignore file"}
  \end{framed}
442
\end{pframe}
443

Mihai's avatar
Mihai committed
444
\subsection{Who edited what?}
445
\begin{pframe}
Mihai's avatar
Mihai committed
446 447 448 449 450 451 452
  \gitcmd\ \cmd{blame}\ shows you the last author of each line:
  \vspace{-0.4cm}
  \begin{framed}
    \prompt\ \gitcmd\ \cmd{blame}\ \cmdarg{README}\\
    \commit{a76f17de}\ \cliout{(Zorro 2015-11-24 16:13:59 +0100 1) Second version.}
  \end{framed}
\end{pframe}
453

Mihai's avatar
Mihai committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
\subsection{Cleaning untracked files}
\begin{pframe}
%   \gitcmd\ \cmd{clean}\ is your ``janitor''.
  \vspace{-0.4cm}
  \begin{framed}
    \prompt\ \gitcmd\ \cmd{status}\\
    \cliout{On branch} \branch{master}\ \vspace{0.1cm}\\
    \cliout{Initial commit}\ \vspace{0.1cm} \\
    \cliout{Untracked files}\ \vspace{-0.1cm} \\
    \cliout{(use "git add <file>..." to include in}\
    \cliout{what will be committed)}\
    \hspace*{0.4cm}\untrackedfile{README.bkp}\ \vspace{0.1cm}\\
    \cliout{nothing added to commit but untracked files present}\ \vspace{-0.1cm}\\
    \cliout{(use "git add" to track)}\\
    \prompt\ \gitcmd\ \cmd{clean}\\
    \cliout{fatal: clean.requireForce defaults to true and neither}\\
    \cliout{-i, -n, nor -f given; refusing to clean}\\
    \prompt\ \gitcmd\ \cmd{clean}\ \cmdopt{-f}\\
    \cliout{Removing README.bkp}
  \end{framed}
474
\end{pframe}
475

476 477 478 479 480
\makeAcknowledgementsSlide{
  \begin{tabular}{l}
    \acknowledgements
  \end{tabular}
  \bigskip
481

482
  \hfill\includegraphics[width=3.5cm]{git_logo}
483

484 485 486 487
  \begin{tabular}{l}
    \small\url{http://git-scm.com/book}\\
  \end{tabular}
}
488 489

\end{document}