tips.tex 9.56 KB
Newer Older
1 2
\documentclass[slidestop]{beamer}

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

5
\author{Wibowo Arindrarto}
6 7
\title{\couseTitle}
\providecommand{\mySubTitle}{Git Tips and Tricks}
8
\providecommand{\myConference}{\courseTitle}
9 10 11 12
\providecommand{\myGroup}{}
\providecommand{\myDepartment}{Department of Human Genetics}
\providecommand{\myCenter}{Center for Human and Clinical Genetics}

13 14 15 16 17 18 19 20
\usetheme{lumc}

\begin{document}

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

% Make the title page.
21
\makeTitleSlide{\includegraphics[width=3.5cm]{git_logo}}
22 23 24

% First page of the presentation.
\section{Introduction}
25
\begin{pframe}
26 27 28
  We have only been playing with some of what git offers. In reality,
  you can do much more with it. It also allows for a wide array of
  customizations.
29
  \bigskip
30 31

  We will look into some of these customizations now.
32
\end{pframe}
33

bow's avatar
bow committed
34
\section{Custom Prompt}
35 36
\begin{pframe}
  Checking which branch you are working on and its status is routine.
bow's avatar
bow committed
37 38 39
  \bigskip
  \pause

40
  ``\lstinline{git status}'' is useful, but quickly feels repetitive.
bow's avatar
bow committed
41 42 43 44 45 46 47 48 49 50 51 52 53
  \begin{lstlisting}[language=none, caption=Routine git checking]
    $ git branch
    * master
    $ git status
    nothing to commit, working directory clean
  \end{lstlisting}
  \pause

  Solution: use a custom shell prompt that displays git status.
  \begin{lstlisting}[language=none, caption=Git checking with custom prompt]
    (master) $ git st
    nothing to commit, working directory clean
  \end{lstlisting}
54
\end{pframe}
bow's avatar
bow committed
55

56 57 58
\begin{pframe}
  The git-approved way to do this is to use the ``\lstinline{\_\_git\_ps1}''
  shell function defined in the ``\lstinline{git-prompt.sh}'' file.
bow's avatar
bow committed
59 60 61 62 63 64 65 66
  \bigskip
  \pause

  The location of this file depends on your OS and git version. For now,
  you can download a copy of this file from our GitLab.
  \bigskip
  \pause

67 68 69 70
  Then, in your ``\lstinline{.bashrc}'' file, add ``\lstinline{\_\_git\_ps1}''
  to the ``\lstinline{PS1}'' variable, and source your ``\lstinline{.bashrc}''
  again.
\end{pframe}
bow's avatar
bow committed
71 72

\section{Useful git commands}
73 74
\subsection{git blame}
\begin{pframe}
bow's avatar
bow committed
75 76 77 78 79
  Git tracks each line of each file in its repository.
  \bigskip
  \pause

  You can view who committed the line change, the commit hash,
80
  and the commit time using ``\lstinline{git blame}''.
bow's avatar
bow committed
81 82 83 84
  \begin{lstlisting}[language=none, caption=git blame command]
    $ git blame README
    a4394d28 (bow 2014-06 ...) Second version.
  \end{lstlisting}
85
  \pause
86

87 88
  Similar functionality is also available in GitLab by clicking on the {\em
    blame} button when viewing a file.
89
\end{pframe}
bow's avatar
bow committed
90

91 92
\subsection{git tag}
\begin{pframe}
bow's avatar
bow committed
93 94 95 96 97
  Sometimes, it is practical to refer to a commit with a name
  instead of a hash.
  \bigskip
  \pause

98
  Official releases, for example, are better referred to as v1.0
bow's avatar
bow committed
99 100 101 102
  than a76a0fx.
  \bigskip
  \pause

103 104
  This can be done using ``\lstinline{git tag}''.
\end{pframe}
bow's avatar
bow committed
105

106
\begin{pframe}
bow's avatar
bow committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  \begin{lstlisting}[language=none, caption=Adding a tag]
    $ git tag "v0.0.1"
    $ git show v0.0.1
    commit a4394d28e6ba30be19318ee74f732a103b8ffdf2
    Author: bow <bow@bow.web.id>
    Date:   Sat Jun 21 13:47:03 2014 +0200

        Second commit

        diff --git a/README b/README
        index efe6f7c..4fe6328 100644
        --- a/README
        +++ b/README
        @@ -1 +1 @@
        -First version.
        +Second version.
  \end{lstlisting}
124
\end{pframe}
bow's avatar
bow committed
125

126
\begin{pframe}
bow's avatar
bow committed
127 128 129 130 131 132 133 134 135 136 137 138

  What we did previously is to add what is called a lightweight tag.
  Lightweight tags are essentially commit aliases.
  \bigskip
  \pause

  There is another type of tag, called the annotated tag.
  \bigskip
  \pause

  Annotated tags contain more information: tagger identity,
  tagging message, tagging date, and can be verified with GPG.
139
\end{pframe}
bow's avatar
bow committed
140

141 142
\begin{pframe}
  \vspace{-1cm}
bow's avatar
bow committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  \begin{lstlisting}[language=none, caption=Adding an annotated tag]
    $ git tag -a "v0.0.1" -m "Alpha version"
    tag v0.1.0
    Tagger: bow <bow@bow.web.id>
    Date:   Sat Jun 21 15:11:53 2014 +0200

    Alpha version

    commit a4394d28e6ba30be19318ee74f732a103b8ffdf2
    Author: bow <bow@bow.web.id>
    Date:   Sat Jun 21 13:47:03 2014 +0200

        Second commit

        diff --git a/README b/README
        index efe6f7c..4fe6328 100644
        --- a/README
        +++ b/README
        @@ -1 +1 @@
        -First version.
        +Second version.
  \end{lstlisting}
165
\end{pframe}
bow's avatar
bow committed
166 167

\section{Useful git options}
168 169
\subsection{git diff -w}
\begin{pframe}
bow's avatar
bow committed
170
  Sometimes, you want to hide whitespace differences when using
171
  ``\lstinline{git diff}''.
bow's avatar
bow committed
172 173 174
  \bigskip
  \pause

175
  This can be done via ``\lstinline{git diff -w}''.
bow's avatar
bow committed
176 177 178 179 180 181 182
  \begin{lstlisting}[language=none, caption=git diff without whitespace]
    $ git diff -w
  \end{lstlisting}
  \pause

  Note that while this aids visualization of the diff, git will still
  commit the whitespace change.
183
\end{pframe}
bow's avatar
bow committed
184

185 186
\subsection{git commit --amend}
\begin{pframe}
bow's avatar
bow committed
187 188 189 190 191 192 193 194 195 196 197
  Git gives you total control over your history, which means you can also
  change them.
  \bigskip
  \pause

  This practice is potentially dangerous and should not be part of your
  regular workflow. This is especially true for public commits.
  \bigskip
  \pause

  Still, there are times when changing that one last commit makes more
198
  sense than doing a ``\lstinline{git revert}''
bow's avatar
bow committed
199 200 201
  \bigskip
  \pause

202
  ``\lstinline{git commit --amend}'' allows you to do that: changing your last
bow's avatar
bow committed
203
  commit.
204
\end{pframe}
bow's avatar
bow committed
205

206
\begin{pframe}
bow's avatar
bow committed
207
  When run on a clean branch (no uncommitted changes),
208 209
  ``\lstinline{git commit --amend}'' allows you to change your last commit
  message.
bow's avatar
bow committed
210 211 212 213 214
  \bigskip
  \pause

  You can also use the command to meld current staged changes to your last
  committed change.
215
  \begin{lstlisting}[language=none, caption=git commit --amend]
bow's avatar
bow committed
216 217 218 219 220 221 222
    $ git status
    Changes to be committed:
        modified:   README
    $ git commit --amend -m "Update README"
    [master b60437d] Second update
     1 file changed, 1 insertion(+), 1 deletion(-)
  \end{lstlisting}
223
\end{pframe}
bow's avatar
bow committed
224

225 226
\subsection{git add --patch}
\begin{pframe}
bow's avatar
bow committed
227 228 229 230 231 232
  Often, uncommited change overextends. You start with the intention of
  fixing bug A, but in the middle found that you can implement feature B
  and feature C while also squashing bug D.
  \bigskip
  \pause

233
  At the end of the day, only ``\lstinline{git commit -am "Updates"}'' is done
bow's avatar
bow committed
234 235 236 237 238 239 240
  and the whole change is saved in a single comit.
  \bigskip
  \pause

  This defeats the purpose of tracking your changes in commits. Commits
  ideally represents a single, functional update, which you can understand
  later on.
241
\end{pframe}
bow's avatar
bow committed
242

243
\begin{pframe}
bow's avatar
bow committed
244 245 246 247 248
  You can, infact, commit the line changes selectively. This helps
  split a mesh of changes into separate commits.
  \begin{lstlisting}[language=none, caption=git add --patch]
    $ git add --patch
  \end{lstlisting}
249
\end{pframe}
bow's avatar
bow committed
250

251
\section{Main Configuration File}
252 253
\subsection{Viewing}
\begin{pframe}
254 255
  % http://tex.stackexchange.com/a/9372
  In Linux, git uses the
256
  ``\lstinline{~/.gitconfig}'' file as its main configuration file.
257 258 259
  \bigskip
  \pause

260 261
  To see the current configuration values, use
  ``\lstinline{git config --list}''.
262 263 264 265 266 267
  \begin{lstlisting}[language=none, caption=Git config values]
    $ git config --list
    user.name=bow
    user.email=bow@bow.web.id
    color.ui=auto
  \end{lstlisting}
268
\end{pframe}
269

270 271 272 273
\subsection{Modifying}
\begin{pframe}
  To edit the file directly, open ``\lstinline{~/.gitconfig}'' in a text editor
  and save your changes.
274 275
  \begin{lstlisting}[language=none, caption=Modifying the config file]
    $ vim ~/.gitconfig
276 277
  \end{lstlisting}
  \pause
278

279 280
  You can also use the ``\lstinline{git config --global}'' to set the values
  via the shell.
281
  \begin{lstlisting}[language=none, caption=Modifying via the shell]
bow's avatar
bow committed
282
    $ git config --global user.name "Linus Torvalds"
283
  \end{lstlisting}
284
\end{pframe}
285

286 287
\subsection{Modifying: global ignore}
\begin{pframe}
288 289 290 291 292
  We will look at two examples now: setting a global ignore file and
  setting aliases.
  \bigskip
  \pause

293
  In addition to setting a directory-specific ``\lstinline{.gitignore}'' file,
294 295 296 297 298
  you can also set a global ignore file.
  \bigskip
  \pause

  You can name this file anything. The convention is to use
299 300
  ``\lstinline{.gitignore\_global}'' and place the file in your home directory.
\end{pframe}
301

302
\begin{pframe}
303
  The global ignore file has the same format as the per-directory
304
  ``\lstinline{.gitignore}'' file, only visible to all git repositories.
305 306 307 308 309
  \bigskip
  \pause

  \begin{lstlisting}[language=none, caption=Setting the global ignore file]
    $ echo "*.out" > ~/.gitignore_global
310
    $ echo "testing.txt" >> ~/.gitignore_global
bow's avatar
bow committed
311 312
    $ git config --global core.excludesfile \
      "~/.gitignore_global"
313 314 315 316 317 318 319 320 321
  \end{lstlisting}
  \pause

  \begin{lstlisting}[language=none, caption=Ignoring via the global file]
    $ echo "Is this the real life?" > this.out
    $ touch testing.txt
    $ git status
    nothing to commit, working directory clean
  \end{lstlisting}
322
\end{pframe}
bow's avatar
bow committed
323

324 325
\subsection{Aliases}
\begin{pframe}
bow's avatar
bow committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  You can alias simple commands.
  \begin{lstlisting}[language=none, caption=Simple alias]
    $ git config --global alias.st status
    $ git st
    nothing to commit, working directory clean
  \end{lstlisting}
  \pause

  Or more complex commands.
  \begin{lstlisting}[language=none, caption=Complex alias]
    $ git config --global alias.qlog \
      "log --pretty=oneline"
    $ git qlog
    a4394d28e... Second commit
    80eafd7e6... First commit
  \end{lstlisting}
342
\end{pframe}
bow's avatar
bow committed
343 344

\section{GitLab and GitHub}
345 346 347
\subsection{Demo time!}
\begin{pframe}
\end{pframe}
bow's avatar
bow committed
348

349 350 351 352 353
\makeAcknowledgementsSlide{
  \begin{tabular}{l}
    \acknowledgements
  \end{tabular}
  \bigskip
bow's avatar
bow committed
354

355
  \hfill\includegraphics[width=3.5cm]{git_logo}
356

357 358 359 360
  \begin{tabular}{l}
    \small\url{http://git-scm.com/book}
  \end{tabular}
}
361 362

\end{document}