basics.tex 9.36 KB
Newer Older
Laros's avatar
Laros committed
1 2
\documentclass[slidestop]{beamer}

Laros's avatar
Laros committed
3 4
\input{../shared/shared.tex}

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

Laros's avatar
Laros committed
13 14 15 16 17 18 19
\usetheme{lumc}

\begin{document}

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

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

% First page of the presentation.
24 25
\section{Starting a project}
\subsection{You can either}
Laros's avatar
Laros committed
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}
Laros's avatar
Laros committed
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}
Laros's avatar
Laros committed
43
\end{pframe}
44

45
\subsection{Where does git store its repository information?}
Laros's avatar
Laros committed
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!}
Laros's avatar
Laros committed
59
\end{pframe}
60

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

Laros's avatar
Laros committed
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}
Laros's avatar
Laros committed
112
\end{pframe}
113

Laros's avatar
Laros committed
114
\begin{pframe}
115 116
  \begin{figure}[]
    \begin{center}
Laros's avatar
Laros committed
117
      \includegraphics[height=0.7\textheight]{18333fig0201-tn}
118 119 120
    \end{center}
    \caption{The lifecycle of the status of your files.}
  \end{figure}
Laros's avatar
Laros committed
121
\end{pframe}
122

Laros's avatar
Laros committed
123 124
\subsection{Adding, removing, renaming}
\begin{pframe}
125 126 127 128 129 130
  \begin{lstlisting}[language=none, caption=This file is not tracked.]
    $ echo First version. > README
    $ git status
    # Untracked files:
    #       README
  \end{lstlisting}
Laros's avatar
Laros committed
131
  \pause
132 133 134 135 136 137 138 139

  \begin{lstlisting}[language=none, caption=Now the file will be tracked.]
    $ git add README
    $ git status
    # Changes to be committed:
    #       new file:   README
  \end{lstlisting}

Laros's avatar
Laros committed
140 141 142
  To rename a file, use ``\lstinline{git mv}'', to remove, use
  ``\lstinline{git rm}''.
\end{pframe}
143 144

\section{Working with versions}
Laros's avatar
Laros committed
145 146
\subsection{commit}
\begin{pframe}
147 148 149 150 151 152
  Once you have modified your files and added them to the \emph{staging area},
  you can commit them (add them to the repository).
  \begin{lstlisting}[language=none, caption=Commit a new version.]
    $ git commit
  \end{lstlisting}

Laros's avatar
Laros committed
153 154
  This will open an editor, give a short description (50 characters) and a list
  of the changes (72 column format).
155
  \begin{itemize}
Laros's avatar
Laros committed
156
    \item Useful when you want to search.
157
  \end{itemize}
Laros's avatar
Laros committed
158 159 160 161 162
  \pause

  \begin{lstlisting}[language=none, caption=Commit a new version.]
    $ git commit -m "Solved the counting bug."
  \end{lstlisting}
Laros's avatar
Laros committed
163
\end{pframe}
164

Laros's avatar
Laros committed
165 166
\subsection{Undoing changes}
\begin{pframe}
167 168 169 170 171 172 173 174
  Sometimes you will accidentally add a file.
  \begin{lstlisting}[language=none, caption=Adding a new version of a file.]
    $ echo Second version. > README
    $ git add README
    $ git status
    # Changes to be committed:
    #       modified:   README
  \end{lstlisting}
Laros's avatar
Laros committed
175
  \pause
176

Laros's avatar
Laros committed
177
  If you want to undo this, you can use ``\lstinline{git reset}''.
178 179 180 181 182 183
  \begin{lstlisting}[language=none, caption=Unstage a file.]
    $ git reset README
    $ git status
    # Changes not staged for commit:
    #       modified:   README
  \end{lstlisting}
Laros's avatar
Laros committed
184
\end{pframe}
bow's avatar
bow committed
185

Laros's avatar
Laros committed
186 187 188
\begin{pframe}
  Keep in mind that ``\lstinline{git reset}'' by default sets a staged file
  back to unstaged.
bow's avatar
bow committed
189 190 191 192 193 194 195 196
  \begin{lstlisting}[language=none, caption=Unstage a file.]
    $ git reset README
    $ git status
    # Changes not staged for commit:
    #       modified:   README
  \end{lstlisting}
  \pause

Laros's avatar
Laros committed
197 198
  To discard all changes in an unstaged file, you can use
  ``\lstinline{git checkout}''
bow's avatar
bow committed
199 200 201 202 203
  \begin{lstlisting}[language=none, caption=Discarding changes.]
    $ git checkout -- .
    $ git status
    nothing to commit, working directory clean
  \end{lstlisting}
Laros's avatar
Laros committed
204
\end{pframe}
bow's avatar
bow committed
205

Laros's avatar
Laros committed
206
\begin{pframe}
bow's avatar
bow committed
207 208 209 210 211 212 213
  For now, let's commit our change and move on.
  \begin{lstlisting}[language=none, caption=Adding a new version of a file.]
    $ echo Second version. > README
    $ git commit
    $ git status
    nothing to commit, working directory clean
  \end{lstlisting}
Laros's avatar
Laros committed
214
\end{pframe}
215

Laros's avatar
Laros committed
216 217 218
\subsection{Viewing the history}
\begin{pframe}
  To see the history of your project, use ``\lstinline{git log}''.
219 220 221 222 223 224 225 226 227 228 229 230 231 232
  \begin{lstlisting}[language=none, caption=The log of our project.]
    $ git log
    commit cc61ee7cd72590f3bebcc9e1ff3e9435c7f7dd28
    Author: J.F.J. Laros <j.f.j.laros@lumc.nl>
    Date:   Fri Oct 11 14:18:13 2013 +0200

        Second version.

    commit 8e10be812fd78f69a5e3bac7670c62438161b6b0
    Author: J.F.J. Laros <j.f.j.laros@lumc.nl>
    Date:   Fri Oct 11 14:17:51 2013 +0200

        First version.
  \end{lstlisting}
Laros's avatar
Laros committed
233
\end{pframe}
234

Laros's avatar
Laros committed
235 236
\subsection{Undo a commit}
\begin{pframe}
237
  Sometimes we want to undo an entire commit. This is done with
Laros's avatar
Laros committed
238
  ``\lstinline{git revert}''.
239
  \begin{lstlisting}[language=none, caption=Revert a commit.]
Laros's avatar
Laros committed
240
    $ git revert cc61ee7cd7
241 242 243 244
    $ cat README
    First version.
  \end{lstlisting}

Laros's avatar
Laros committed
245
  The hash can be found with ``\lstinline{git log}''.
Laros's avatar
Laros committed
246 247 248
  \bigskip
  \pause

bow's avatar
bow committed
249
  You can also use a unique prefix of this hash, usually six characters is
Laros's avatar
Laros committed
250
  enough.
Laros's avatar
Laros committed
251
\end{pframe}
252

Laros's avatar
Laros committed
253 254 255
\subsection{Viewing changes in detail}
\begin{pframe}
  If you want to see what has changed, use ``\lstinline{git diff}''.
256
  \begin{lstlisting}[language=none, caption=Difference between the working copy
Laros's avatar
Laros committed
257
    and the staging area.]
258 259 260 261 262 263 264 265
    $ echo Third version. > README
    $ git diff
    --- a/README
    +++ b/README
    @@ -1 +1 @@
    -First version.
    +Third version.
  \end{lstlisting}
Laros's avatar
Laros committed
266 267
  \pause

Laros's avatar
Laros committed
268 269
  For staged files, use the ``\lstinline{--cached}'' or
  ``\lstinline{--staged}'' option.
Laros's avatar
Laros committed
270 271 272 273
  \begin{lstlisting}[language=none, caption=Difference between the staging area
    and the last commit.]
    $ git diff --cached
  \end{lstlisting}
Laros's avatar
Laros committed
274
\end{pframe}
275

Laros's avatar
Laros committed
276
\begin{pframe}
277 278 279 280 281 282 283 284 285 286
  You can see the differences between any two versions.
  \begin{lstlisting}[language=none, caption=Difference between the working copy
    and an other commit.]
    $ git diff cc61ee
    --- a/README
    +++ b/README
    @@ -1 +1 @@
    -Second version.
    +Third version.
  \end{lstlisting}
Laros's avatar
Laros committed
287 288 289 290 291 292
  \pause

  \begin{lstlisting}[language=none, caption=Difference between two committed
    versions.]
    $ git diff 8e10be cc61ee
  \end{lstlisting}
Laros's avatar
Laros committed
293
\end{pframe}
294 295

\section{Extras}
Laros's avatar
Laros committed
296 297
\subsection{Explicit no tracking}
\begin{pframe}
298 299 300
  Sometimes you do not want to track certain files:
  \begin{itemize}
    \item Executables.
Laros's avatar
Laros committed
301 302
    \item PDF files (if you still have the \LaTeX\ source).
    \item Python bytecode (\lstinline{.pyc}) files.
303 304
    \item Files containing passwords.
  \end{itemize}
Laros's avatar
Laros committed
305
  \medskip
Laros's avatar
Laros committed
306
  \pause
307

Laros's avatar
Laros committed
308
  Use the special ``\lstinline{.gitignore}'' file.
309 310 311 312 313 314 315 316
  \begin{lstlisting}[language=none, caption=Ignoring certain files.]
    $ touch notrack.txt
    $ echo notrack.txt > .gitignore
    $ git add .gitignore
    $ git commit
    $ git status
    nothing to commit, working directory clean
  \end{lstlisting}
Laros's avatar
Laros committed
317
\end{pframe}
Laros's avatar
Laros committed
318

Laros's avatar
Laros committed
319 320 321 322
\subsection{Help}
\begin{pframe}
  With the ``\lstinline{help}'' command get the manual of a particular
  subcommand.
Laros's avatar
Laros committed
323 324 325 326 327 328 329 330 331 332
  \begin{lstlisting}[language=none, caption=Get the full manual.]
    $ git help <command>
  \end{lstlisting}
  \pause

  Example.
  \begin{lstlisting}[language=none, caption=Get the manual for the diff
    subcommand.]
    $ git help diff
  \end{lstlisting}
Laros's avatar
Laros committed
333
\end{pframe}
Laros's avatar
Laros committed
334

Laros's avatar
Laros committed
335 336 337 338 339
\makeAcknowledgementsSlide{
  \begin{tabular}{l}
    \acknowledgements
  \end{tabular}
  \bigskip
340

Laros's avatar
Laros committed
341
  \hfill\includegraphics[width=3.5cm]{git_logo}
342

Laros's avatar
Laros committed
343 344 345 346
  \begin{tabular}{l}
    \small\url{http://git-scm.com/book}\\
  \end{tabular}
}
Laros's avatar
Laros committed
347 348

\end{document}