branching.tex 13.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
\documentclass[slidestop]{beamer}

\title{Working with branches in Git}
\providecommand{\myConference}{Git course}
\providecommand{\myDate}{Monday, October 14, 2013}
\author{Martijn Vermaat}
\providecommand{\myGroup}{Leiden Genome Technology Center}
\providecommand{\myDepartment}{Department of Human Genetics}
\providecommand{\myCenter}{Center for Human and Clinical Genetics}
\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}

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
\lstset{
  basicstyle=\ttfamily,
  language=none,
  frame=none,
  numbers=none,
  numbersep=0
}

\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Table of contents}
    \tableofcontents[currentsection]
  \end{frame}
}

40 41 42 43 44 45 46 47
\begin{document}

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

% Make the title page.
\bodytemplate

48

49
\section{The Git commit graph}
50

51
\begin{frame}
52 53 54
  \frametitle{A linear history}
  \includegraphics[width=10cm]{images/linear}
  \begin{itemize}
55 56
    \item Every commit has a parent.
    \item {\em Committing} creates a new commit on top of the current one.
57
  \end{itemize}
58 59
\end{frame}

60 61 62 63
\begin{frame}
  \frametitle{The default \bt{master} branch}
  \includegraphics[width=10cm]{images/master}
  \begin{itemize}
64 65
    \item A {\em branch} is a pointer to a commit.
    \item By default there is one branch: \bt{master}.
66 67 68 69 70 71
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{The current commit: \bt{HEAD}}
  \begin{itemize}
72 73 74
    \item The current commit is called \bt{HEAD} (shown in blue).
    \item \bt{HEAD} normally points to the current branch.
    \item Or to a commit if there is no current branch.
75 76 77 78 79 80 81 82
  \end{itemize}
  \vspace{1cm}
  \includegraphics[width=10cm]{images/head}
\end{frame}

\begin{frame}
  \frametitle{Committing moves \bt{HEAD} and the current branch}
  \begin{itemize}
83 84 85
    \item Committing moves the current branch to the new commit.
    \item Of course, \bt{HEAD} moves with it.
    \item If there is no current branch, only \bt{HEAD} moves.
86 87 88 89 90 91 92 93 94
  \end{itemize}
  \vspace{1cm}
  \includegraphics[width=10cm]{images/commit}
\end{frame}

\begin{frame}
  \frametitle{A non-linear history}
  \includegraphics[width=10cm]{images/nonlinear}
  \begin{itemize}
95 96
    \item The commit graph can become non-linear.
    \item Usually by committing from the same commit twice.
97 98 99 100 101 102
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Use branches to keep track of different code paths}
  \begin{itemize}
103 104
    \item Branch names are easier to remember than commit hashes.
    \item Branch names make it clear what commits are about.
105 106 107 108 109 110 111 112
  \end{itemize}
  \vspace{0.5cm}
  \includegraphics[width=6cm]{images/branches}
\end{frame}

\begin{frame}
  \frametitle{Different code paths may later join}
  \begin{itemize}
113 114
    \item Commits from different branches can be brought together.
    \item We call this {\em merging}.
115 116 117 118 119 120
  \end{itemize}
  \vspace{0.5cm}
  \includegraphics[width=6cm]{images/merge}
\end{frame}


121
\section{Inspecting the commit graph}
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
\begin{frame}
  \frametitle{An example repository}
  \includegraphics[width=8cm]{images/example}
  \begin{itemize}
    \item Branches \bt{license} and \bt{interface} diverged from \bt{master}.
    \item Only \bt{license} has been merged back into \bt{master}.
    \item Current branch is \bt{master}.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Showing the current branch: \bt{git status}}
  \begin{lstlisting}
$ git status
# On branch master
nothing to commit (working directory clean)
  \end{lstlisting}
  \bigskip
  Remember: you cannot type \bt{git status} enough!
\end{frame}

\begin{frame}[fragile]
  \frametitle{Listing branches: \bt{git branch}}
  \begin{lstlisting}
$ git branch
  interface
  license
* master
  \end{lstlisting}
  \bigskip
  Three branches, current branch is \bt{master}.
  \pause
  \bigskip
  \begin{lstlisting}
$ git branch -v
  interface 8fc25c1 Trivial Python interface
  license   0fbe3e3 Add MIT license
* master    c7f3bd9 Add .gitignore file
  \end{lstlisting}
  \bigskip
  \bt{-v}: Shows the commit each branch points to.
\end{frame}

\begin{frame}[fragile]
  \frametitle{The commit log: \bt{git log}}
  \begin{lstlisting}
$ git log --oneline --decorate
c7f3bd9 (HEAD, master) Add .gitignore file
4a44c4e Merge branch 'license'
64af1ee Add course teachers to README
0fbe3e3 (license) Add MIT license
d1c7fd7 Initial commit with README
  \end{lstlisting}
  \bigskip
  \bt{--oneline}: Shows commit summary on one line.

  \bt{--decorate}: Adds branch information.
\end{frame}

\begin{frame}[fragile]
  \frametitle{The commit log as a graph: \bt{git log}}
  \begin{lstlisting}
$ git log --oneline --decorate --graph --all
* 8fc25c1 (interface) Trivial Python interface
| * c7f3bd9 (HEAD, master) Add .gitignore file
|/
*   4a44c4e Merge branch 'license'
|\
| * 0fbe3e3 (license) Add MIT license
* | 64af1ee Add course teachers to README
|/
* d1c7fd7 Initial commit with README
  \end{lstlisting}
  \bigskip
  \bt{--graph}: Shows the commit graph.

  \bt{--all}: Includes all branches instead of just the current.
\end{frame}
201 202 203

\begin{frame}[fragile]
  \frametitle{Switching to another branch: \bt{git checkout}}
204 205 206 207 208 209 210 211 212 213 214 215 216
  \begin{lstlisting}
$ git checkout interface
Switched to branch 'interface'
  \end{lstlisting}
  \bigskip
  This points \bt{HEAD} to the named branch and updates your working
  directory.
  \pause
  \bigskip
  \begin{lstlisting}
$ git status
# On branch interface
nothing to commit (working directory clean)
217
  \end{lstlisting}
218 219 220 221 222 223 224 225
\end{frame}


\section{Manipulating the commit graph}

\begin{frame}
  \frametitle{An example repository}
  \includegraphics[width=8cm]{images/example}
226
  \begin{itemize}
227 228 229
    \item Branches \bt{license} and \bt{interface} diverged from \bt{master}.
    \item Only \bt{license} has been merged back into \bt{master}.
    \item Current branch is \bt{master}.
230 231 232 233 234
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Creating a new branch: \bt{git branch}}
235 236 237 238 239 240 241 242 243 244
  \begin{lstlisting}
$ git branch lib
  \end{lstlisting}
  \bigskip
  Creates branch \bt{lib} at \bt{HEAD}.
  \pause
  \bigskip
  \begin{lstlisting}
$ git checkout lib
Switched to branch 'lib'
245
  \end{lstlisting}
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 309 310 311
  \pause
  \bigskip
  Or, for Git pro's, in one command:
  \bigskip
  \begin{lstlisting}
$ git checkout -b lib
Switched to a new branch 'lib'
  \end{lstlisting}
  \bigskip
  At this point, \bt{lib} is just a label to the same commit as \bt{master}.
\end{frame}

\begin{frame}
  \frametitle{Our example repository}
  \includegraphics[width=8cm]{images/example-branched}\\
  \vspace{0.5cm}
  Created branch \bt{lib} from \bt{master}.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Working on a branch: \bt{git commit}}
  We can extend the \bt{lib} branch by adding commits.
  \bigskip
  \begin{lstlisting}
$ echo 'VERSION=1' > testlib.py
$ git add testlib.py
$ git commit -m 'Add empty testlib'
  \end{lstlisting}
  \pause
  \bigskip
  \begin{lstlisting}
$ git log --oneline --decorate --graph --all
* 251a51b (HEAD, lib) Add empty testlib
* c7f3bd9 (master) Add .gitignore file
| * 8fc25c1 (interface) Trivial Python interface
|/
*   4a44c4e Merge branch 'license'
|\
| * 0fbe3e3 (license) Add MIT license
...
  \end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Our example repository}
  \includegraphics[width=8cm]{images/example-committed}\\
  \vspace{0.5cm}
  Extended branch \bt{lib}.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Fast forward merging: \bt{git merge}}
  Let's merge our work on \bt{lib} back into \bt{master}.
  \bigskip
  \begin{lstlisting}
$ git checkout master
Switched to branch 'master'
$ git merge lib
Updating c7f3bd9..251a51b
Fast-forward
 testlib.py |    1 +
 1 file changed, 1 insertion(+)
 create mode 100644 testlib.py
  \end{lstlisting}
  \bigskip
  This merge was easy:
312
  \begin{itemize}
313 314 315
    \item \bt{lib} was directly upstream of \bt{master}.
    \item Git just moves \bt{HEAD} and \bt{master} to point to \bt{lib}.
    \item This is called a {\em fast forward merge}.
316
  \end{itemize}
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
\end{frame}

\begin{frame}
  \frametitle{Our example repository}
  \includegraphics[width=8cm]{images/example-merged-ff}\\
  \vspace{0.5cm}
  Merged branch \bt{lib} into \bt{master} (fast forward).
\end{frame}

\begin{frame}[fragile]
  \frametitle{Three-way merging: \bt{git merge}}
  Let's merge our work on \bt{interface} back into \bt{master}.
  \bigskip
  \begin{lstlisting}
$ git merge interface
Merge made by the 'recursive' strategy.
 interface.py |    3 +++
 1 file changed, 3 insertions(+)
 create mode 100755 interface.py
  \end{lstlisting}
  \bigskip
  This merge was harder:
  \begin{itemize}
    \item \bt{interface} and \bt{master} had diverged.
    \item Git determines changes in \bt{interface} and \bt{master} since their
      most recent common ancestor and creates a new commit from that.
    \item This is called a {\em recursive three-way merge} and the resulting
      merge a {\em merge commit}.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Our example repository}
  \includegraphics[width=8cm]{images/example-merged}\\
351
  \vspace{0.5cm}
352
  Merged branch \bt{interface} into \bt{master}.
353 354
\end{frame}

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
\begin{frame}[fragile]
  \frametitle{Deleting branches: \bt{git branch}}
  Old branches that have been merged can be deleted.
  \bigskip
  \begin{lstlisting}
$ git branch -d interface
Deleted branch interface (was 8fc25c1).
  \end{lstlisting}
  \bigskip
  \begin{lstlisting}
$ git branch -d lib license
Deleted branch lib (was 251a51b).
Deleted branch license (was 0fbe3e3).
  \end{lstlisting}
  \bigskip
  No history is lost, just labels removed.
\end{frame}


374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 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 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
\section{Basic merge conflicts}

\begin{frame}
  \frametitle{Merge conflicts}
  Git is pretty good at merging:
  \begin{itemize}
    \item The changes might have been in different files.
    \item Or in different parts of the same file.
    \item Git tries to figure out a sensible result.
  \end{itemize}
  \pause
  \bigskip
  Sometimes this is not possible:
  \begin{itemize}
    \item The changes might be incompatible.
    \item When we try \bt{git merge}, Git gives up.
    \item This situation is called a {\em merge conflict}.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Setting the stage (1/3)}
  \begin{lstlisting}
$ git log --oneline --decorate --graph --all
* 9d2ad27 (HEAD, master) State character preference
| * 374ab60 (simpsons) State character preference
|/
* 4012f4f Initial commit
  \end{lstlisting}
  \bigskip
  We'd like to merge branch \bt{simpsons} into \bt{master}.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Setting the stage (2/3)}
  The last commit on \bt{master}:
  \bigskip
  \begin{lstlisting}
$ git show --oneline
9d2ad27 State character preference
diff --git a/README b/README
index de15194..ef40359 100644
--- a/README
+++ b/README
@@ -1,2 +1,4 @@
 Facts about television series
 =============================
+
+My favorite character is Eric Cartman.
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Setting the stage (3/3)}
  The last commit on \bt{simpsons}:
  \bigskip
  \begin{lstlisting}
$ git show --oneline simpsons
374ab60 State character preference
diff --git a/README b/README
index de15194..9dd729b 100644
--- a/README
+++ b/README
@@ -1,2 +1,4 @@
 Facts about television series
 =============================
+
+My favorite character is Homer Simpson.
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Creating a merge conflict}
  \begin{lstlisting}
$ git merge simpsons
Auto-merging README
CONFLICT (content): Merge conflict in README
Automatic merge failed; fix conflicts and then
commit the result.
  \end{lstlisting}
  \bigskip
  \begin{lstlisting}
$ git status
# On branch master
# Unmerged paths:
#   (use "git add/rm <file>.." as appropriate to
#    mark resolution)
#
#              both modified:      README
#
no changes added to commit (use "git add" and/or
"git commit -a")
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Resolving a merge conflict (1/2)}
  \begin{lstlisting}
$ cat README
Facts about television series
=============================

<<<<<<< HEAD
My favorite character is Eric Cartman.
=======
My favorite character is Homer Simpson.
>>>>>>> simpsons
  \end{lstlisting}
  \bigskip
  What we had is under \bt{HEAD}, what \bt{simpsons} had is above
  \bt{simpsons}.
  \pause
  \bigskip
  \begin{lstlisting}
$ emacs README
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Resolving a merge conflict (1/2)}
  We resolve the conflict by hand.
  \bigskip
  \begin{lstlisting}
$ cat README
Facts about television series
=============================

My favorite characters are Eric Cartman
and Homer Simpson.
  \end{lstlisting}
  \pause
  \bigskip
  And can now finish the merge commit.
  \bigskip
  \begin{lstlisting}
$ git add README
$ git commit
[master 1e496cb] Merge branch 'simpsons'
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Resolving conflicts with \bt{git mergetool}}
  We can also use graphical merge tools such as {\em Meld}.
  \bigskip
  \begin{lstlisting}
$ git mergetool
merge tool candidates: meld opendiff kdiff3 ...
Merging:
README

Normal merge conflict for 'README':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (meld):
  \end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Meld example}
  \includegraphics[width=11cm]{images/meld}\\
  \vspace{0.5cm}
  Tools like Meld provide an editable three-way diff.
\end{frame}
538 539


540 541 542 543 544 545 546 547 548 549 550 551 552
\section{Questions?}
\lastpagetemplate
\begin{frame}
  \begin{center}
    Acknowledgements:
    \bigskip
    \bigskip

    Jeroen Laros

    Zuotian Tatum

  \end{center}
553 554 555
  \vfill
  \permfoot{http://git-scm.com/book}\\
  \permfoot{https://www.atlassian.com/git}
556 557 558
\end{frame}

\end{document}