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

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

5
\author{Jeroen F.J. Laros}
6 7
\title{\courseTitle}
\providecommand{\mySubTitle}{Git Basics}
8
\providecommand{\myConference}{\courseTitle}
9
\providecommand{\myGroup}{}
10 11
\providecommand{\myDepartment}{Department of Human Genetics}
\providecommand{\myCenter}{Center for Human and Clinical Genetics}
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 21
% Make the title slide.
\makeTitleSlide{\includegraphics[width=3.5cm]{git_logo}}
22 23

% First page of the presentation.
24
\section{Initialisation}
25 26
\subsection{Starting a project}
\begin{pframe}
27 28 29
  Creating a new repository is easy. You do not need a server, no registration,
  etc.
  \begin{lstlisting}[language=none, caption=Make a new repository.]
30
    $ cd ~/projects
31 32 33
    $ git init
    Initialized empty Git repository in <path>/.git/
  \end{lstlisting}
34
  \pause
35 36 37 38 39

  Or you can ``clone'' an existing repository.
  \begin{lstlisting}[language=none, caption=Clone an existing repository.]
    $ git clone <path-to-repository>
  \end{lstlisting}
40
\end{pframe}
41

42
\begin{pframe}
43 44 45 46 47 48 49
  You can see a hidden directory in a Git repository.
  \begin{lstlisting}[language=none, caption=A hidden directory is added.]
    $ ls -a
    .  ..  .git
  \end{lstlisting}

  This directory contains almost everything that Git stores and manipulates.
50
\end{pframe}
51 52

\section{Manipulation}
53 54
\subsection{Local operations}
\begin{pframe}
55 56
  \begin{figure}[]
    \begin{center}
57
      \includegraphics[height=0.7\textheight]{18333fig0106-tn}
58 59 60
    \end{center}
    \caption{Working directory, staging area, and Git directory.}
  \end{figure}
61
\end{pframe}
62

63 64
\subsection{Checking the status of your files}
\begin{pframe}
65 66 67 68 69
  \begin{lstlisting}[language=none, caption=Check status.]
    $ git status
    nothing to commit (working directory clean)
  \end{lstlisting}

70
  ``\lstinline{git status}'' can tell you whether your files are:
71 72 73 74 75 76
  \begin{itemize}
    \item Untracked.
    \item Unmodified.
    \item Modified.
    \item Staged.
  \end{itemize}
77
\end{pframe}
78

79
\begin{pframe}
80 81
  \begin{figure}[]
    \begin{center}
82
      \includegraphics[height=0.7\textheight]{18333fig0201-tn}
83 84 85
    \end{center}
    \caption{The lifecycle of the status of your files.}
  \end{figure}
86
\end{pframe}
87

88 89
\subsection{Adding, removing, renaming}
\begin{pframe}
90 91 92 93 94 95
  \begin{lstlisting}[language=none, caption=This file is not tracked.]
    $ echo First version. > README
    $ git status
    # Untracked files:
    #       README
  \end{lstlisting}
96
  \pause
97 98 99 100 101 102 103 104

  \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}

105 106 107
  To rename a file, use ``\lstinline{git mv}'', to remove, use
  ``\lstinline{git rm}''.
\end{pframe}
108 109

\section{Working with versions}
110 111
\subsection{commit}
\begin{pframe}
112 113 114 115 116 117
  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}

118 119
  This will open an editor, give a short description (50 characters) and a list
  of the changes (72 column format).
120
  \begin{itemize}
121
    \item Useful when you want to search.
122
  \end{itemize}
123 124 125 126 127
  \pause

  \begin{lstlisting}[language=none, caption=Commit a new version.]
    $ git commit -m "Solved the counting bug."
  \end{lstlisting}
128
\end{pframe}
129

130 131
\subsection{Undoing changes}
\begin{pframe}
132 133 134 135 136 137 138 139
  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}
140
  \pause
141

142
  If you want to undo this, you can use ``\lstinline{git reset}''.
143 144 145 146 147 148
  \begin{lstlisting}[language=none, caption=Unstage a file.]
    $ git reset README
    $ git status
    # Changes not staged for commit:
    #       modified:   README
  \end{lstlisting}
149
\end{pframe}
bow's avatar
bow committed
150

151 152 153
\begin{pframe}
  Keep in mind that ``\lstinline{git reset}'' by default sets a staged file
  back to unstaged.
bow's avatar
bow committed
154 155 156 157 158 159 160 161
  \begin{lstlisting}[language=none, caption=Unstage a file.]
    $ git reset README
    $ git status
    # Changes not staged for commit:
    #       modified:   README
  \end{lstlisting}
  \pause

162 163
  To discard all changes in an unstaged file, you can use
  ``\lstinline{git checkout}''
bow's avatar
bow committed
164 165 166 167 168
  \begin{lstlisting}[language=none, caption=Discarding changes.]
    $ git checkout -- .
    $ git status
    nothing to commit, working directory clean
  \end{lstlisting}
169
\end{pframe}
bow's avatar
bow committed
170

171
\begin{pframe}
bow's avatar
bow committed
172 173 174 175 176 177 178
  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}
179
\end{pframe}
180

181 182 183
\subsection{Viewing the history}
\begin{pframe}
  To see the history of your project, use ``\lstinline{git log}''.
184 185 186 187 188 189 190 191 192 193 194 195 196 197
  \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}
198
\end{pframe}
199

200 201
\subsection{Undo a commit}
\begin{pframe}
202
  Sometimes we want to undo an entire commit. This is done with
203
  ``\lstinline{git revert}''.
204
  \begin{lstlisting}[language=none, caption=Revert a commit.]
205
    $ git revert cc61ee7cd7
206 207 208 209
    $ cat README
    First version.
  \end{lstlisting}

210
  The hash can be found with ``\lstinline{git log}''.
211 212 213
  \bigskip
  \pause

bow's avatar
bow committed
214
  You can also use a unique prefix of this hash, usually six characters is
215
  enough.
216
\end{pframe}
217

218 219 220
\subsection{Viewing changes in detail}
\begin{pframe}
  If you want to see what has changed, use ``\lstinline{git diff}''.
221
  \begin{lstlisting}[language=none, caption=Difference between the working copy
222
    and the staging area.]
223 224 225 226 227 228 229 230
    $ echo Third version. > README
    $ git diff
    --- a/README
    +++ b/README
    @@ -1 +1 @@
    -First version.
    +Third version.
  \end{lstlisting}
231 232
  \pause

233 234
  For staged files, use the ``\lstinline{--cached}'' or
  ``\lstinline{--staged}'' option.
235 236 237 238
  \begin{lstlisting}[language=none, caption=Difference between the staging area
    and the last commit.]
    $ git diff --cached
  \end{lstlisting}
239
\end{pframe}
240

241
\begin{pframe}
242 243 244 245 246 247 248 249 250 251
  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}
252 253 254 255 256 257
  \pause

  \begin{lstlisting}[language=none, caption=Difference between two committed
    versions.]
    $ git diff 8e10be cc61ee
  \end{lstlisting}
258
\end{pframe}
259 260

\section{Extras}
261 262
\subsection{Explicit no tracking}
\begin{pframe}
263 264 265
  Sometimes you do not want to track certain files:
  \begin{itemize}
    \item Executables.
266 267
    \item PDF files (if you still have the \LaTeX\ source).
    \item Python bytecode (\lstinline{.pyc}) files.
268 269
    \item Files containing passwords.
  \end{itemize}
270
  \medskip
271
  \pause
272

273
  Use the special ``\lstinline{.gitignore}'' file.
274 275 276 277 278 279 280 281
  \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}
282
\end{pframe}
283

284 285 286 287
\subsection{Help}
\begin{pframe}
  With the ``\lstinline{help}'' command get the manual of a particular
  subcommand.
288 289 290 291 292 293 294 295 296 297
  \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}
298
\end{pframe}
299

300 301 302 303 304
\makeAcknowledgementsSlide{
  \begin{tabular}{l}
    \acknowledgements
  \end{tabular}
  \bigskip
305

306
  \hfill\includegraphics[width=3.5cm]{git_logo}
307

308 309 310 311
  \begin{tabular}{l}
    \small\url{http://git-scm.com/book}\\
  \end{tabular}
}
312 313

\end{document}