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

\title{Git Tips and Tricks}
\providecommand{\myConference}{Git course}
bow's avatar
bow committed
5
\providecommand{\myDate}{Monday, June 23, 2014}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
\author{Wibowo Arindrarto}
\providecommand{\myGroup}{Sequencing Analysis Support Core}
\providecommand{\myDepartment}{}
\providecommand{\myCenter}{}
\providecommand{\lastCenterLogo}{
  \raisebox{-0.1cm}{
    \includegraphics[height=1cm]{lgtc_logo}
    %\includegraphics[height=0.7cm]{ngi_logo}
  }
}
\providecommand{\lastRightLogo}{
  %\includegraphics[height=0.7cm]{nbic_logo}
  %\includegraphics[height=0.8cm]{nwo_logo_en}
  %\hspace{1.5cm}\includegraphics[height=0.7cm]{gen2phen_logo}
}

\usetheme{lumc}

\begin{document}

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

% Make the title page.
\bodytemplate

% First page of the presentation.
\section{Introduction}
\begin{frame}[fragile]
  \frametitle{}

37 38 39
  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.
40
  \bigskip
41 42 43 44 45

  We will look into some of these customizations now.
\end{frame}


bow's avatar
bow committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
\section{Custom Prompt}
\begin{frame}[fragile]
  \frametitle{}

  Checking which branch you are working on and its status is
  a routine task.
  \bigskip
  \pause

  ``\bt{git status}'' is useful, but quickly feels repetitive.
  \begin{lstlisting}[language=none, caption=Routine git checking]
    $ git branch
    * master
    $ git status
    nothing to commit, working directory clean
  \end{lstlisting}
  \bigskip
  \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}
  \bigskip

\end{frame}


\section{Custom Prompt}
\begin{frame}[fragile]
  \frametitle{}

  The git-approved way to do this is to use the ``\bt{\_\_git\_ps1}''
  shell function defined in the ``\bt{git-prompt.sh}'' file.
  \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

  Then, in you ``\bt{.bashrc}'' file, add ``\bt{\_\_git\_ps1}'' to the
  ``\bt{PS1}'' variable, and source your ``\bt{.bashrc}'' again.
  \bigskip
\end{frame}


\section{Useful git commands}
\begin{frame}[fragile]
  \frametitle{git blame}

  Git tracks each line of each file in its repository.
  \bigskip
  \pause

  You can view who committed the line change, the commit hash,
  and the commit time using ``\bt{git blame}''
  \begin{lstlisting}[language=none, caption=git blame command]
    $ git blame README
    a4394d28 (bow 2014-06 ...) Second version.
  \end{lstlisting}
  \bigskip
\end{frame}


\section{Useful git commands}
\begin{frame}[fragile]
  \frametitle{git tag}

  Sometimes, it is practical to refer to a commit with a name
  instead of a hash.
  \bigskip
  \pause

  Official releases, for example, are better referred as v1.0
  than a76a0fx.
  \bigskip
  \pause

  This can be done using ``\bt{git tag}''.
\end{frame}


\section{Useful git commands}
\begin{frame}[fragile]
  \frametitle{git tag}

  \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}
  \bigskip
\end{frame}


\section{Useful git commands}
\begin{frame}[fragile]
  \frametitle{git tag}

  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.
  \bigskip
\end{frame}


\section{Useful git commands}
\begin{frame}[fragile]
  \frametitle{git tag}
  \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}
  \bigskip
\end{frame}


\section{Useful git options}
\begin{frame}[fragile]
  \frametitle{git diff -w}

  Sometimes, you want to hide whitespace differences when using
  ``\bt{git diff}''.
  \bigskip
  \pause

  This can be done via ``\bt{git diff -w}''
  \begin{lstlisting}[language=none, caption=git diff without whitespace]
    $ git diff -w
  \end{lstlisting}
  \bigskip
  \pause

  Note that while this aids visualization of the diff, git will still
  commit the whitespace change.
  \bigskip
\end{frame}


\section{Useful git options}
\begin{frame}[fragile]
  \frametitle{git commit --amend}

  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
  sense than doing a ``\bt{git revert}''
  \bigskip
  \pause

  ``\bt{git commit --amend}'' allows you to do that: changing your last
  commit.
  \bigskip
\end{frame}


\section{Useful git options}
\begin{frame}[fragile]
  \frametitle{git commit --amend}

  When run on a clean branch (no uncommitted changes),
  ``\bt{git commit --amend}'' allows you to change your last commit message.
  \bigskip
  \pause

  You can also use the command to meld current staged changes to your last
  committed change.
  \begin{lstlisting}[language=none, caption=git blame command]
    $ 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}
  \bigskip
\end{frame}


\section{Useful git options}
\begin{frame}[fragile]
  \frametitle{git add --patch}

  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

  At the end of the day, only ``\bt{git commit -am "Updates"}'' is done
  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.
  \bigskip
\end{frame}


\section{Useful git options}
\begin{frame}[fragile]
  \frametitle{git add --patch}

  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}
  \bigskip
\end{frame}


309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
\section{Main Configuration File}
\begin{frame}[fragile]
  \frametitle{Viewing}

  In Linux, git uses the ``\bt{~/.gitconfig}'' file as its main
  configuration file.
  \bigskip
  \pause

  To see the current configuration values, use ``\bt{git config --list}''.
  \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}
  \bigskip
\end{frame}


\section{Main Configuration File}
\begin{frame}[fragile]
  \frametitle{Modifying}

  To edit the file directly, open ``\bt{~/.gitconfig}'' in a text editor
  and save your changes.
  \begin{lstlisting}[language=none, caption=Modifying the config file]
    $ vim ~/.gitconfig
337 338 339
  \end{lstlisting}
  \bigskip
  \pause
340

bow's avatar
bow committed
341
  You can also use the ``\bt{git config --global}'' to set the values via the shell.
342
  \begin{lstlisting}[language=none, caption=Modifying via the shell]
bow's avatar
bow committed
343
    $ git config --global user.name "Linus Torvalds"
344 345 346 347
  \end{lstlisting}
  \bigskip
\end{frame}

bow's avatar
bow committed
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
\section{Main Configuration File}
\begin{frame}[fragile]
  \frametitle{Modifying: global ignore}

  We will look at two examples now: setting a global ignore file and
  setting aliases.
  \bigskip
  \pause

  In addition to setting a directory-specific ``\bt{.gitignore}'' file,
  you can also set a global ignore file.
  \bigskip
  \pause

  You can name this file anything. The convention is to use
  ``\bt{.gitignore\_global}'' and place the file in your home directory.
  \bigskip
\end{frame}


\section{Main Configuration File}
\begin{frame}[fragile]
  \frametitle{}

  The global ignore file has the same format as the per-directory
  ``\bt{.gitignore}'' file, only visible to all git repositories.
  \bigskip
  \pause

  \begin{lstlisting}[language=none, caption=Setting the global ignore file]
    $ echo "*.out" > ~/.gitignore_global
    $ echo "testing.txt" > ~/.gitignore_global
bow's avatar
bow committed
381 382
    $ git config --global core.excludesfile \
      "~/.gitignore_global"
383 384 385 386 387 388 389 390 391 392 393
  \end{lstlisting}
  \bigskip
  \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}
  \bigskip
394 395 396
\end{frame}


bow's avatar
bow committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
\section{Main Configuration File}
\begin{frame}[fragile]
  \frametitle{Aliases}

  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}
  \bigskip
  \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}
  \bigskip
\end{frame}


\section{GitLab and GitHub}
\begin{frame}[fragile]
  \frametitle{Demo time!}
\end{frame}


428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
\section{Questions?}
\lastpagetemplate
\begin{fframe}
  \begin{center}
    Acknowledgements:
    \bigskip
    \bigskip

    Martijn Vermaat

    Jeroen F. J. Laros

  \end{center}

  \vfill
  \permfoot{http://git-scm.com/book}
\end{fframe}

\end{document}