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
\usepackage[inkscapelatex=false,inkscapearea=page]{svg}

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

25 26 27 28 29
\begin{document}

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

30
% Make the title slide.
Mihai's avatar
Mihai committed
31 32
\makeTitleSlide{\includesvg[width=3.5cm]{../shared/git_logo.svg}}

33 34

% First page of the presentation.
Mihai's avatar
Mihai committed
35 36
\section{Getting started}
\subsection{To start a project you can either:}
37
\begin{pframe}
38 39 40 41 42 43 44 45
  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}
46
  \pause
47

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

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

65 66 67 68 69
  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!}
70
\end{pframe}
71

Mihai's avatar
Mihai committed
72
\subsection{Quick setup}
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
\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}
102
\end{pframe}
103

Mihai's avatar
Mihai committed
104 105 106 107 108 109 110
\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}
Mihai's avatar
Mihai committed
111
    \includesvg[width=0.7\textwidth]{images/git_areas.svg}\\
Mihai's avatar
Mihai committed
112 113 114 115 116
  \end{center}
  \vspace{-0.4cm}
\end{pframe}

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

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

131
\begin{pframe}
Mihai's avatar
Mihai committed
132 133 134 135 136 137 138 139 140 141 142 143
  \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}
144
\end{pframe}
145

146 147 148 149 150 151 152
\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}
Mihai's avatar
Mihai committed
153
    \includesvg[width=0.75\textwidth]{images/file_status_cycles_00.svg}\\
154 155
  \end{center}
\end{pframe}
Mihai's avatar
Mihai committed
156

157 158 159 160 161 162
\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}
Mihai's avatar
Mihai committed
163
    \includesvg[width=0.75\textwidth]{images/file_status_cycles_01.svg}\\
164 165 166 167
  \end{center}
\end{pframe}


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

Mihai's avatar
Mihai committed
177 178 179 180 181 182 183 184 185 186 187 188
  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}.
189
\end{pframe}
bow's avatar
bow committed
190

191 192 193 194 195 196 197 198 199
\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}
Mihai's avatar
Mihai committed
200
    \includesvg[width=0.75\textwidth]{images/file_status_cycles_add.svg}\\
201 202 203
  \end{center}
\end{pframe}

Mihai's avatar
Mihai committed
204
\subsection{Commit}
205
\begin{pframe}
Mihai's avatar
Mihai committed
206 207 208 209 210 211 212 213 214 215 216 217
  \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
218 219
  \pause

Mihai's avatar
Mihai committed
220 221 222 223 224 225 226
  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}
227
\end{pframe}
bow's avatar
bow committed
228

229 230 231 232 233 234 235 236 237 238
\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}
Mihai's avatar
Mihai committed
239
    \includesvg[width=0.75\textwidth]{images/file_status_cycles_commit.svg}\\
240 241 242
  \end{center}
\end{pframe}

Mihai's avatar
Mihai committed
243
\subsection{}
244
\begin{pframe}
Mihai's avatar
Mihai committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  \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}
264
\end{pframe}
265

266 267 268 269 270 271 272 273 274
\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}
Mihai's avatar
Mihai committed
275
    \includesvg[width=0.75\textwidth]{images/file_status_cycles_edit.svg}\\
276 277 278 279
  \end{center}
\end{pframe}


Mihai's avatar
Mihai committed
280
\subsection{Check the differences}
281
\begin{pframe}
Mihai's avatar
Mihai committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  \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}
298

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

308
\begin{pframe}
Mihai's avatar
Mihai committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  \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}.
325 326
  \pause

Mihai's avatar
Mihai committed
327 328
  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>}
329
\end{pframe}
330

Mihai's avatar
Mihai committed
331
\subsection{}
332
\begin{pframe}
Mihai's avatar
Mihai committed
333 334 335 336 337 338 339
  \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}
340 341
  \pause

Mihai's avatar
Mihai committed
342 343 344 345 346 347 348 349 350 351
  \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.
352
\end{pframe}
353

Mihai's avatar
Mihai committed
354

355 356 357 358 359 360 361 362 363
\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}
Mihai's avatar
Mihai committed
364
    \includesvg[width=0.75\textwidth]{images/file_status_cycles_reset.svg}\\
365 366 367
  \end{center}
\end{pframe}

Mihai's avatar
Mihai committed
368
\subsection{Check commit history}
369
\begin{pframe}
Mihai's avatar
Mihai committed
370 371 372 373 374 375 376 377 378 379 380 381 382
%   \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}
383

384
\subsection{Restoring Previous Versions}
Mihai's avatar
Mihai committed
385 386 387 388 389 390 391 392 393 394 395 396 397
\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}
398 399
  \pause
  \begin{center}
Mihai's avatar
Mihai committed
400
    \includesvg[width=0.75\textwidth]{images/file_status_cycles_checkout.svg}\\
401 402 403 404
  \end{center}
\end{pframe}

\begin{pframe}
Mihai's avatar
Mihai committed
405 406 407 408 409
  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
410
  It's up to you to include this file into a new commit.
411
\end{pframe}
412

413 414 415 416 417 418 419 420
% \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
421

422
\section{Extras}
Mihai's avatar
Mihai committed
423
\subsection{Explicit not tracking}
424
\begin{pframe}
Mihai's avatar
Mihai committed
425
  Certain files are not suitable for tracking by \gitcmd :
426
  \begin{itemize}
Mihai's avatar
Mihai committed
427 428
   \item Binary files / executables;
   \item PDF files / Microsoft Office files.
429
  \end{itemize}
Mihai's avatar
Mihai committed
430 431 432
  Other files are also not meant to be tracked:
  \begin{itemize}
   \item Password-containing files;
Mihai's avatar
Mihai committed
433
   \item Large files - use git-annex for those.
Mihai's avatar
Mihai committed
434
  \end{itemize}
Mihai's avatar
Mihai committed
435
  \pause
Mihai's avatar
Mihai committed
436 437 438 439 440 441 442 443 444
  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}
445
\end{pframe}
446

Mihai's avatar
Mihai committed
447
\subsection{Who edited what?}
448
\begin{pframe}
Mihai's avatar
Mihai committed
449 450 451 452 453 454 455
  \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}
456

Mihai's avatar
Mihai committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
\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}
477
\end{pframe}
478

479 480 481 482 483
\makeAcknowledgementsSlide{
  \begin{tabular}{l}
    \acknowledgements
  \end{tabular}
  \bigskip
484

485
  \hfill\includegraphics[width=3.5cm]{git_logo}
486

487 488 489 490
  \begin{tabular}{l}
    \small\url{http://git-scm.com/book}\\
  \end{tabular}
}
491 492

\end{document}