tips.tex 9.56 KB
 bow committed Jun 20, 2014 1 2 \documentclass[slidestop]{beamer}  Laros committed Apr 24, 2016 3 4 \input{../shared/shared.tex}  Laros committed Apr 24, 2016 5 \author{Wibowo Arindrarto}  Laros committed Apr 24, 2016 6 7 \title{\couseTitle} \providecommand{\mySubTitle}{Git Tips and Tricks}  Laros committed Apr 24, 2016 8 \providecommand{\myConference}{\courseTitle}  Laros committed Apr 24, 2016 9 10 11 12 \providecommand{\myGroup}{} \providecommand{\myDepartment}{Department of Human Genetics} \providecommand{\myCenter}{Center for Human and Clinical Genetics}  bow committed Jun 20, 2014 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.  Laros committed Apr 24, 2016 21 \makeTitleSlide{\includegraphics[width=3.5cm]{git_logo}}  bow committed Jun 20, 2014 22 23 24  % First page of the presentation. \section{Introduction}  Laros committed Apr 24, 2016 25 \begin{pframe}  bow committed Jun 20, 2014 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.  bow committed Jun 20, 2014 29  \bigskip  bow committed Jun 20, 2014 30 31  We will look into some of these customizations now.  Laros committed Apr 24, 2016 32 \end{pframe}  bow committed Jun 20, 2014 33   bow committed Jun 21, 2014 34 \section{Custom Prompt}  Laros committed Apr 24, 2016 35 36 \begin{pframe} Checking which branch you are working on and its status is routine.  bow committed Jun 21, 2014 37 38 39  \bigskip \pause  Laros committed Apr 24, 2016 40  \lstinline{git status}'' is useful, but quickly feels repetitive.  bow committed Jun 21, 2014 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}  Laros committed Apr 24, 2016 54 \end{pframe}  bow committed Jun 21, 2014 55   Laros committed Apr 24, 2016 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 committed Jun 21, 2014 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  Laros committed Apr 24, 2016 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 committed Jun 21, 2014 71 72  \section{Useful git commands}  Laros committed Apr 24, 2016 73 74 \subsection{git blame} \begin{pframe}  bow committed Jun 21, 2014 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,  Laros committed Apr 24, 2016 80  and the commit time using \lstinline{git blame}''.  bow committed Jun 21, 2014 81 82 83 84  \begin{lstlisting}[language=none, caption=git blame command]$ git blame README a4394d28 (bow 2014-06 ...) Second version. \end{lstlisting}  Vermaat committed Jun 22, 2014 85  \pause  Laros committed Apr 24, 2016 86   Vermaat committed Jun 22, 2014 87 88  Similar functionality is also available in GitLab by clicking on the {\em blame} button when viewing a file.  Laros committed Apr 24, 2016 89 \end{pframe}  bow committed Jun 21, 2014 90   Laros committed Apr 24, 2016 91 92 \subsection{git tag} \begin{pframe}  bow committed Jun 21, 2014 93 94 95 96 97  Sometimes, it is practical to refer to a commit with a name instead of a hash. \bigskip \pause  Vermaat committed Jun 22, 2014 98  Official releases, for example, are better referred to as v1.0  bow committed Jun 21, 2014 99 100 101 102  than a76a0fx. \bigskip \pause  Laros committed Apr 24, 2016 103 104  This can be done using \lstinline{git tag}''. \end{pframe}  bow committed Jun 21, 2014 105   Laros committed Apr 24, 2016 106 \begin{pframe}  bow committed Jun 21, 2014 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 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}  Laros committed Apr 24, 2016 124 \end{pframe}  bow committed Jun 21, 2014 125   Laros committed Apr 24, 2016 126 \begin{pframe}  bow committed Jun 21, 2014 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.  Laros committed Apr 24, 2016 139 \end{pframe}  bow committed Jun 21, 2014 140   Laros committed Apr 24, 2016 141 142 \begin{pframe} \vspace{-1cm}  bow committed Jun 21, 2014 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 Date: Sat Jun 21 15:11:53 2014 +0200 Alpha version commit a4394d28e6ba30be19318ee74f732a103b8ffdf2 Author: bow 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}  Laros committed Apr 24, 2016 165 \end{pframe}  bow committed Jun 21, 2014 166 167  \section{Useful git options}  Laros committed Apr 24, 2016 168 169 \subsection{git diff -w} \begin{pframe}  bow committed Jun 21, 2014 170  Sometimes, you want to hide whitespace differences when using  Laros committed Apr 24, 2016 171  \lstinline{git diff}''.  bow committed Jun 21, 2014 172 173 174  \bigskip \pause  Laros committed Apr 24, 2016 175  This can be done via \lstinline{git diff -w}''.  bow committed Jun 21, 2014 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.  Laros committed Apr 24, 2016 183 \end{pframe}  bow committed Jun 21, 2014 184   Laros committed Apr 24, 2016 185 186 \subsection{git commit --amend} \begin{pframe}  bow committed Jun 21, 2014 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  Laros committed Apr 24, 2016 198  sense than doing a \lstinline{git revert}''  bow committed Jun 21, 2014 199 200 201  \bigskip \pause  Laros committed Apr 24, 2016 202  \lstinline{git commit --amend}'' allows you to do that: changing your last  bow committed Jun 21, 2014 203  commit.  Laros committed Apr 24, 2016 204 \end{pframe}  bow committed Jun 21, 2014 205   Laros committed Apr 24, 2016 206 \begin{pframe}  bow committed Jun 21, 2014 207  When run on a clean branch (no uncommitted changes),  Laros committed Apr 24, 2016 208 209  \lstinline{git commit --amend}'' allows you to change your last commit message.  bow committed Jun 21, 2014 210 211 212 213 214  \bigskip \pause You can also use the command to meld current staged changes to your last committed change.  Vermaat committed Jun 22, 2014 215  \begin{lstlisting}[language=none, caption=git commit --amend]  bow committed Jun 21, 2014 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}  Laros committed Apr 24, 2016 223 \end{pframe}  bow committed Jun 21, 2014 224   Laros committed Apr 24, 2016 225 226 \subsection{git add --patch} \begin{pframe}  bow committed Jun 21, 2014 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  Laros committed Apr 24, 2016 233  At the end of the day, only \lstinline{git commit -am "Updates"}'' is done  bow committed Jun 21, 2014 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.  Laros committed Apr 24, 2016 241 \end{pframe}  bow committed Jun 21, 2014 242   Laros committed Apr 24, 2016 243 \begin{pframe}  bow committed Jun 21, 2014 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}  Laros committed Apr 24, 2016 249 \end{pframe}  bow committed Jun 21, 2014 250   bow committed Jun 20, 2014 251 \section{Main Configuration File}  Laros committed Apr 24, 2016 252 253 \subsection{Viewing} \begin{pframe}  Vermaat committed Jun 22, 2014 254 255  % http://tex.stackexchange.com/a/9372 In Linux, git uses the  Laros committed Apr 24, 2016 256  \lstinline{~/.gitconfig}'' file as its main configuration file.  bow committed Jun 20, 2014 257 258 259  \bigskip \pause  Laros committed Apr 24, 2016 260 261  To see the current configuration values, use \lstinline{git config --list}''.  bow committed Jun 20, 2014 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}  Laros committed Apr 24, 2016 268 \end{pframe}  bow committed Jun 20, 2014 269   Laros committed Apr 24, 2016 270 271 272 273 \subsection{Modifying} \begin{pframe} To edit the file directly, open \lstinline{~/.gitconfig}'' in a text editor and save your changes.  bow committed Jun 20, 2014 274 275  \begin{lstlisting}[language=none, caption=Modifying the config file] $vim ~/.gitconfig  bow committed Jun 20, 2014 276 277  \end{lstlisting} \pause  bow committed Jun 20, 2014 278   Laros committed Apr 24, 2016 279 280  You can also use the \lstinline{git config --global}'' to set the values via the shell.  bow committed Jun 20, 2014 281  \begin{lstlisting}[language=none, caption=Modifying via the shell]  bow committed Jun 21, 2014 282 $ git config --global user.name "Linus Torvalds"  bow committed Jun 20, 2014 283  \end{lstlisting}  Laros committed Apr 24, 2016 284 \end{pframe}  bow committed Jun 20, 2014 285   Laros committed Apr 24, 2016 286 287 \subsection{Modifying: global ignore} \begin{pframe}  bow committed Jun 20, 2014 288 289 290 291 292  We will look at two examples now: setting a global ignore file and setting aliases. \bigskip \pause  Laros committed Apr 24, 2016 293  In addition to setting a directory-specific \lstinline{.gitignore}'' file,  bow committed Jun 20, 2014 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  Laros committed Apr 24, 2016 299 300  \lstinline{.gitignore\_global}'' and place the file in your home directory. \end{pframe}  bow committed Jun 20, 2014 301   Laros committed Apr 24, 2016 302 \begin{pframe}  bow committed Jun 20, 2014 303  The global ignore file has the same format as the per-directory  Laros committed Apr 24, 2016 304  \lstinline{.gitignore}'' file, only visible to all git repositories.  bow committed Jun 20, 2014 305 306 307 308 309  \bigskip \pause \begin{lstlisting}[language=none, caption=Setting the global ignore file] $echo "*.out" > ~/.gitignore_global  Vermaat committed Jun 23, 2014 310 $ echo "testing.txt" >> ~/.gitignore_global  bow committed Jun 21, 2014 311 312  $git config --global core.excludesfile \ "~/.gitignore_global"  bow committed Jun 20, 2014 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}  Laros committed Apr 24, 2016 322 \end{pframe}  bow committed Jun 21, 2014 323   Laros committed Apr 24, 2016 324 325 \subsection{Aliases} \begin{pframe}  bow committed Jun 21, 2014 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}  Laros committed Apr 24, 2016 342 \end{pframe}  bow committed Jun 21, 2014 343 344  \section{GitLab and GitHub}  Laros committed Apr 24, 2016 345 346 347 \subsection{Demo time!} \begin{pframe} \end{pframe}  bow committed Jun 21, 2014 348   Laros committed Apr 24, 2016 349 350 351 352 353 \makeAcknowledgementsSlide{ \begin{tabular}{l} \acknowledgements \end{tabular} \bigskip  bow committed Jun 21, 2014 354   Laros committed Apr 24, 2016 355  \hfill\includegraphics[width=3.5cm]{git_logo}  bow committed Jun 20, 2014 356   Laros committed Apr 24, 2016 357 358 359 360  \begin{tabular}{l} \small\url{http://git-scm.com/book} \end{tabular} }  bow committed Jun 20, 2014 361 362  \end{document}