merging.tex 12.2 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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
\documentclass[slidestop]{beamer}

\title{Combining changes by merging}
\providecommand{\myConference}{Git course}
\providecommand{\myDate}{Tueday, December 1, 2015}
\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}

\lstset{
  basicstyle=\ttfamily,
  language=none,
  frame=none,
  numbers=none,
  numbersep=0
}

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

\begin{document}

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

% Make the title page.
\bodytemplate


\section{The Git commit graph}

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

\begin{frame}
  \frametitle{The default \bt{master} branch}
  \includegraphics[width=10cm]{images/master}
  \begin{itemize}
    \item A {\em branch} is a pointer to a commit.
    \item By default there is one branch: \bt{master}.
    \item \bt{HEAD} is a special pointer, it points to the current branch.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Committing moves the current branch pointer}
  \begin{itemize}
    \item Committing moves the current branch to the new commit.
    \item (Of course, \bt{HEAD} moves with it.)
  \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}
    \item The commit graph can become non-linear.
    \item Usually by committing from the same commit twice.
  \end{itemize}
\end{frame}

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

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


\section{Inspecting the commit graph}

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

Vermaat's avatar
Vermaat committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
\begin{frame}[fragile]
  \frametitle{Annotated log as an alias}
  For convenience, we can create an alias for the \bt{git log} command with
  all the arguments we just used:
  \begin{lstlisting}
$ git config --global alias.l \
    'log --oneline --decorate --graph --all'
$ git l
* 8fc25c1 (interface) Trivial Python interface
| * c7f3bd9 (HEAD, master) Add .gitignore file
|/
*   4a44c4e Merge branch 'license'
...
\end{lstlisting}
  \bigskip
  This alias is stored in the \bt{~/.gitconfig} file. You can also edit that
  file manually.
\end{frame}

188 189
\begin{frame}
  \frametitle{Remote branches}
Vermaat's avatar
Vermaat committed
190
  In this course, we don't really discuss branches, except:
191 192 193 194 195 196
  \begin{itemize}
    \item Commits from a remote show up as a remote branch.
    \item Even with only a \bt{master} branch, remotes might still diverge.
    \item Merging remote branches is just like merging local branches.
  \end{itemize}
  \bigskip
Vermaat's avatar
Vermaat committed
197
  \includegraphics[width=10cm]{images/example-hue}
198 199 200
\end{frame}


Vermaat's avatar
Vermaat committed
201
\section{Merging from remotes}
202 203 204

\begin{frame}
  \frametitle{An example repository}
Vermaat's avatar
Vermaat committed
205 206 207 208
  \includegraphics[width=10cm]{images/example-hue}\\
  \bigskip
  Branch \bt{master} on \bt{hue} is one commit ahead of our \bt{master} and we
  have already seen how to merge \bt{hue/master} into \bt{master}:
209
  \begin{itemize}
Vermaat's avatar
Vermaat committed
210 211 212 213
    \item \bt{git merge hue/master}
    \item This was easy: Git just moves \bt{master} (and \bt{HEAD}) to point
      to \bt{hue/master}.
    \item It is called a {\em fast forward merge}.
214 215 216 217
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
Vermaat's avatar
Vermaat committed
218 219
  \frametitle{Fast forward merging: \bt{git merge}}
  If a remote branch is ahead of us, it can be merged easily:
220 221
  \bigskip
  \begin{lstlisting}
Vermaat's avatar
Vermaat committed
222 223 224 225 226
$ git merge hue/master
Updating c7f3bd9..251a51b
Fast-forward
testlib.py |    2 +
1 file changed, 2 insertions(+)
227 228
  \end{lstlisting}
  \bigskip
Vermaat's avatar
Vermaat committed
229
  \includegraphics[width=10cm]{images/example-hue-merged}
230 231 232 233
\end{frame}


\begin{frame}
Vermaat's avatar
Vermaat committed
234 235
  \frametitle{An example repository}
  \includegraphics[width=8cm]{images/example-hue-diverged}\\
236
  \bigskip
Vermaat's avatar
Vermaat committed
237 238
  Branch \bt{master} on \bt{hue} and our \bt{master} have diverged and merging
  now seems less straightforward.
239
  \begin{itemize}
Vermaat's avatar
Vermaat committed
240 241 242 243
    \item \bt{git merge hue/master}
    \item Git will create a new commit in which it combines both changes.
    \item It is called a {\em three-way merge}.
    \item The {\em merge commit} has two parents.
244 245 246 247 248
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Three-way merging: \bt{git merge}}
Vermaat's avatar
Vermaat committed
249
  Let's merge the remote work on \bt{master} back into our local \bt{master}:
250 251
  \bigskip
  \begin{lstlisting}
Vermaat's avatar
Vermaat committed
252
$ git merge hue/master
253
Merge made by the 'recursive' strategy.
Vermaat's avatar
Vermaat committed
254 255
 FACTS.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
256 257
  \end{lstlisting}
  \bigskip
Vermaat's avatar
Vermaat committed
258
  \includegraphics[width=9cm]{images/example-hue-merged2}
259 260 261
\end{frame}

\begin{frame}[fragile]
Vermaat's avatar
Vermaat committed
262 263 264 265 266 267 268 269
  \frametitle{The other way around}
  \bt{git push} also does an implicit \bt{git merge} on the remote.
  \begin{itemize}
    \item We send our local commits to the remote.
    \item They are merged into the remote branch.
    \item But only if this is a fast-forward merge.
    \item Diverging commits {\em cannot be pushed directly}.
  \end{itemize}
270 271
  \bigskip
  \bigskip
Vermaat's avatar
Vermaat committed
272
  \includegraphics[width=10cm]{images/example-hue-pushed}
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
\end{frame}


\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}
Vermaat's avatar
Vermaat committed
299 300
* 5edaf08 (hue/master, hue/HEAD) State character preference
| * f1ef19c (HEAD, master) State character preference
301
|/
Vermaat's avatar
Vermaat committed
302
* 1f6d2ab Initial commit
303 304
  \end{lstlisting}
  \bigskip
Vermaat's avatar
Vermaat committed
305
  We'd like to merge branch \bt{hue/master} into \bt{master}.
306 307 308 309 310 311 312
\end{frame}

\begin{frame}[fragile]
  \frametitle{Setting the stage (2/3)}
  The last commit on \bt{master}:
  \bigskip
  \begin{lstlisting}
Vermaat's avatar
Vermaat committed
313 314
f1ef19c State character preference
diff --git a/FACTS.md b/FACTS.md
315
index de15194..ef40359 100644
Vermaat's avatar
Vermaat committed
316 317
--- a/FACTS.md
+++ b/FACTS.md
318
@@ -1,2 +1,4 @@
Vermaat's avatar
Vermaat committed
319 320
Facts about television series
=============================
321 322 323 324 325 326 327
+
+My favorite character is Eric Cartman.
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Setting the stage (3/3)}
Vermaat's avatar
Vermaat committed
328
  The last commit on \bt{hue/master}:
329 330
  \bigskip
  \begin{lstlisting}
Vermaat's avatar
Vermaat committed
331 332 333 334 335 336
$ git show --oneline hue/master
5edaf08 State character preference
diff --git a/FACTS.md b/FACTS.md
index de15194..5e69508 100644
--- a/FACTS.md
+++ b/FACTS.md
337
@@ -1,2 +1,4 @@
Vermaat's avatar
Vermaat committed
338 339
Facts about television series
=============================
340
+
Vermaat's avatar
Vermaat committed
341
+My favorite character is Milhouse.
342 343 344 345 346 347
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Creating a merge conflict}
  \begin{lstlisting}
Vermaat's avatar
Vermaat committed
348 349 350
$ git merge hue/master
Auto-merging FACTS.md
CONFLICT (content): Merge conflict in FACTS.md
351 352 353 354 355 356 357 358 359 360 361
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)
#
Vermaat's avatar
Vermaat committed
362
#              both modified:      FACTS.md
363 364 365 366 367 368 369 370 371
#
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}
Vermaat's avatar
Vermaat committed
372
$ cat FACTS.md
373 374 375 376 377 378
Facts about television series
=============================

<<<<<<< HEAD
My favorite character is Eric Cartman.
=======
Vermaat's avatar
Vermaat committed
379 380
My favorite character is Milhouse.
>>>>>>> hue/master
381 382
  \end{lstlisting}
  \bigskip
Vermaat's avatar
Vermaat committed
383 384
  What we had is under \bt{HEAD}, what \bt{hue/master} had is above
  \bt{hue/maser}.
385 386 387
  \pause
  \bigskip
  \begin{lstlisting}
Vermaat's avatar
Vermaat committed
388
$ nano FACTS.md
389 390 391 392 393 394 395 396
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Resolving a merge conflict (2/2)}
  We resolve the conflict by hand.
  \bigskip
  \begin{lstlisting}
Vermaat's avatar
Vermaat committed
397
$ cat FACTS.md
398 399 400 401
Facts about television series
=============================

My favorite characters are Eric Cartman
Vermaat's avatar
Vermaat committed
402
and Milhouse.
403 404 405 406 407 408
  \end{lstlisting}
  \pause
  \bigskip
  And can now finish the merge commit.
  \bigskip
  \begin{lstlisting}
Vermaat's avatar
Vermaat committed
409
$ git add FACTS.md
410
$ git commit
Vermaat's avatar
Vermaat committed
411
[master 1e496cb] Merge remote-tracking branch 'hue/master'
412 413 414 415 416 417 418 419 420
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Aborting a merge}
  If you don't feel like resolving the merge conflict, you can go back with
  \bt{git merge --abort}.
  \bigskip
  \begin{lstlisting}
Vermaat's avatar
Vermaat committed
421 422 423
$ git merge hue/master
Auto-merging FACTS.md
CONFLICT (content): Merge conflict in FACTS.md
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
Automatic merge failed; fix conflicts and then
commit the result.
  \end{lstlisting}
  \bigskip
  \begin{lstlisting}
$ git merge --abort
  \end{lstlisting}
  \bigskip
  \begin{lstlisting}
$ git status
# On branch master
nothing to commit (working directory clean)
  \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:
Vermaat's avatar
Vermaat committed
447
FACTS.md
448

Vermaat's avatar
Vermaat committed
449
Normal merge conflict for 'FACTS.md':
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
  {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}


\section{Questions?}
\lastpagetemplate
\begin{frame}
  \begin{center}
    Acknowledgements:
    \bigskip
    \bigskip

    Jeroen Laros

    Zuotian Tatum

    Wibowo Arindrarto
  \end{center}
  \vfill
  \permfoot{http://git-scm.com/book}\\
  \permfoot{https://www.atlassian.com/git}
\end{frame}

\end{document}