basics.tex 12.8 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 15 16 17 18 19
\usetheme{lumc}

\begin{document}

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

20
% Make the title slide.
21
\makeTitleSlide{\includegraphics[width=3.5cm]{../shared/git_logo.pdf}}
22 23

% First page of the presentation.
24 25
\section{Starting a project}
\subsection{You can either}
26
\begin{pframe}
27 28 29 30 31 32 33 34
  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}
35
  \pause
36

37 38 39 40 41 42
  2. Or you can \cmd{clone} an existing remote (or local) repository:
  \vspace{-0.3cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{clone}\ \cmdarg{\<path-to-repository\>}\\
   \cliout{Cloning into \{repo-name\} ...}
  \end{framed}
43
\end{pframe}
44

45
\subsection{Where does git store its repository information?}
46
\begin{pframe}
47
  You can see a hidden directory in a Git repository.
48 49 50 51 52
  \vspace{-0.3cm}
  \begin{framed}
   \prompt\ \cmd{ls}\ \cmdopt{-a}\\
   \cliout{. .. .git}
  \end{framed}
53

54 55 56 57 58
  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!}
59
\end{pframe}
60

61
\section{Git Operations}
62
\begin{pframe}
63 64
  \vspace{-0.9cm}
  Operations can be local or remote.\\
Mihai's avatar
Mihai committed
65 66
  Most importantly, they track files around these three \textbf{areas}
  \footnote[1]{\scriptsize{Adapted from the
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
  \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}
\end{pframe}

\subsection{Prelude: quick setup}
\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}
104
\end{pframe}
105

106 107
\subsection{Checking the status of your files}
\begin{pframe}
108 109 110 111
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{status}\\
   \cliout{nothing to commit (working directory clean)}
  \end{framed}
112
\end{pframe}
113

114
\begin{pframe}
Mihai's avatar
Mihai committed
115 116 117 118 119 120 121
  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}
122
\end{pframe}
123

Mihai's avatar
Mihai committed
124
\subsection{Checking the status of your files}
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

Mihai's avatar
Mihai committed
146 147

\subsection{Adding/staging files}
148
\begin{pframe}
Mihai's avatar
Mihai committed
149 150 151 152 153
  To start tracking the file, we need to stage it first:
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\
  \end{framed}
154
  \pause
155

Mihai's avatar
Mihai committed
156 157 158 159 160 161 162 163 164 165 166 167
  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}.
168
\end{pframe}
bow's avatar
bow committed
169

Mihai's avatar
Mihai committed
170
\subsection{Commit}
171
\begin{pframe}
Mihai's avatar
Mihai committed
172 173 174 175 176 177 178 179 180 181 182 183
  \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
184 185
  \pause

Mihai's avatar
Mihai committed
186 187 188 189 190 191 192
  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}
193
\end{pframe}
bow's avatar
bow committed
194

Mihai's avatar
Mihai committed
195
\subsection{}
196
\begin{pframe}
Mihai's avatar
Mihai committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  \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}
216
\end{pframe}
217

Mihai's avatar
Mihai committed
218
\subsection{Check the differences}
219
\begin{pframe}
Mihai's avatar
Mihai committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  \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}
236

Mihai's avatar
Mihai committed
237 238 239 240 241 242 243
\begin{pframe}
  \vspace{-0.4cm}
  \begin{framed}
   \prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\\
   \prompt\ \gitcmd\ \cmd{diff}\\
   \prompt\
  \end{framed}
244
\end{pframe}
245

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

Mihai's avatar
Mihai committed
265 266
  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>}
267
\end{pframe}
268

Mihai's avatar
Mihai committed
269
\subsection{}
270
\begin{pframe}
Mihai's avatar
Mihai committed
271 272 273 274 275 276 277
  \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}
278 279
  \pause

Mihai's avatar
Mihai committed
280 281 282 283 284 285 286 287 288 289
  \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.
290
\end{pframe}
291

Mihai's avatar
Mihai committed
292 293

\subsection{Check commit history}
294
\begin{pframe}
Mihai's avatar
Mihai committed
295 296 297 298 299 300 301 302 303 304 305 306 307
%   \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}
308

Mihai's avatar
Mihai committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
\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}
329
\end{pframe}
330

Mihai's avatar
Mihai committed
331 332 333 334 335 336
% \subsection{Revert}
% \begin{pframe}
%
% \end{pframe}


337
\section{Extras}
Mihai's avatar
Mihai committed
338
\subsection{Explicit not tracking}
339
\begin{pframe}
Mihai's avatar
Mihai committed
340
  Certain files are not suitable for tracking by \gitcmd :
341
  \begin{itemize}
Mihai's avatar
Mihai committed
342 343
   \item Binary files / executables;
   \item PDF files / Microsoft Office files.
344
  \end{itemize}
Mihai's avatar
Mihai committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358
  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}
359
\end{pframe}
360

Mihai's avatar
Mihai committed
361
\subsection{Who edited what?}
362
\begin{pframe}
Mihai's avatar
Mihai committed
363 364 365 366 367 368 369
  \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}
370

Mihai's avatar
Mihai committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
\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}
391
\end{pframe}
392

393 394 395 396 397
\makeAcknowledgementsSlide{
  \begin{tabular}{l}
    \acknowledgements
  \end{tabular}
  \bigskip
398

399
  \hfill\includegraphics[width=3.5cm]{git_logo}
400

401 402 403 404
  \begin{tabular}{l}
    \small\url{http://git-scm.com/book}\\
  \end{tabular}
}
405 406

\end{document}