basics.tex 12.9 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}{}
10
\providecommand{\myDepartment}{Department of Human Genetics}
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 115
\subsection{Checking the status of your files}
\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 126 127 128 129
  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.85\textwidth]{images/file_status_cycles.pdf}\\
  \end{center}
130
\end{pframe}
131

Mihai's avatar
Mihai committed
132
\subsection{Checking the status of your files}
133
\begin{pframe}
Mihai's avatar
Mihai committed
134 135 136
  \begin{framed}
   \prompt\ \cmd{echo}\ \cmdarg{"First version."}\ \op{$>$}\ \cmdarg{README}
  \end{framed}
137
\end{pframe}
138

139
\begin{pframe}
Mihai's avatar
Mihai committed
140 141 142 143 144 145 146 147 148 149 150 151
  \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}
152
\end{pframe}
153

Mihai's avatar
Mihai committed
154 155

\subsection{Adding/staging files}
156
\begin{pframe}
Mihai's avatar
Mihai committed
157 158 159 160 161
  To start tracking the file, we need to stage it first:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\
  \end{framed}
162
  \pause
163

Mihai's avatar
Mihai committed
164 165 166 167 168 169 170 171 172 173 174 175
  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}.
176
\end{pframe}
bow's avatar
bow committed
177

Mihai's avatar
Mihai committed
178
\subsection{Commit}
179
\begin{pframe}
Mihai's avatar
Mihai committed
180 181 182 183 184 185 186 187 188 189 190 191
  \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
192 193
  \pause

Mihai's avatar
Mihai committed
194 195 196 197 198 199 200
  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}
201
\end{pframe}
bow's avatar
bow committed
202

Mihai's avatar
Mihai committed
203
\subsection{}
204
\begin{pframe}
Mihai's avatar
Mihai committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  \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}
224
\end{pframe}
225

Mihai's avatar
Mihai committed
226
\subsection{Check the differences}
227
\begin{pframe}
Mihai's avatar
Mihai committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  \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}
244

Mihai's avatar
Mihai committed
245 246 247 248 249 250 251
\begin{pframe}
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\\
   \prompt\ \gitcmd\ \cmd{diff}\\
   \prompt\
  \end{framed}
252
\end{pframe}
253

254
\begin{pframe}
Mihai's avatar
Mihai committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  \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}.
271 272
  \pause

Mihai's avatar
Mihai committed
273 274
  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>}
275
\end{pframe}
276

Mihai's avatar
Mihai committed
277
\subsection{}
278
\begin{pframe}
Mihai's avatar
Mihai committed
279 280 281 282 283 284 285
  \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}
286 287
  \pause

Mihai's avatar
Mihai committed
288 289 290 291 292 293 294 295 296 297
  \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.
298
\end{pframe}
299

Mihai's avatar
Mihai committed
300 301

\subsection{Check commit history}
302
\begin{pframe}
Mihai's avatar
Mihai committed
303 304 305 306 307 308 309 310 311 312 313 314 315
%   \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}
316

Mihai's avatar
Mihai committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
\section{Restoring Previous Versions}
\subsection{Checkout}
\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}
  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}
337
\end{pframe}
338

Mihai's avatar
Mihai committed
339 340 341 342 343 344
% \subsection{Revert}
% \begin{pframe}
%
% \end{pframe}


345
\section{Extras}
Mihai's avatar
Mihai committed
346
\subsection{Explicit not tracking}
347
\begin{pframe}
Mihai's avatar
Mihai committed
348
  Certain files are not suitable for tracking by \gitcmd :
349
  \begin{itemize}
Mihai's avatar
Mihai committed
350 351
   \item Binary files / executables;
   \item PDF files / Microsoft Office files.
352
  \end{itemize}
Mihai's avatar
Mihai committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366
  Other files are also not meant to be tracked:
  \begin{itemize}
   \item Password-containing files;
   \item Large files.
  \end{itemize}
  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}
367
\end{pframe}
368

Mihai's avatar
Mihai committed
369
\subsection{Who edited what?}
370
\begin{pframe}
Mihai's avatar
Mihai committed
371 372 373 374 375 376 377
  \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}
378

Mihai's avatar
Mihai committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
\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}
399
\end{pframe}
400

401 402 403 404 405
\makeAcknowledgementsSlide{
  \begin{tabular}{l}
    \acknowledgements
  \end{tabular}
  \bigskip
406

407
  \hfill\includegraphics[width=3.5cm]{git_logo}
408

409 410 411 412
  \begin{tabular}{l}
    \small\url{http://git-scm.com/book}\\
  \end{tabular}
}
413 414

\end{document}