Commit 2a53a7bf authored by Mihai's avatar Mihai

Basics lecture reformated

parent 60bd8476
......@@ -62,8 +62,8 @@
\begin{pframe}
\vspace{-0.9cm}
Operations can be local or remote.\\
Most importantly, they track files around these three areas
\footnote[1]{\scriptsize{Adapted from
Most importantly, they track files around these three \textbf{areas}
\footnote[1]{\scriptsize{Adapted from the
\href{http://git-scm.com/book/en/v2/Getting-Started-Git-Basics}
{Pro Git Book}.}}:
\begin{center}
......@@ -112,224 +112,282 @@
\end{pframe}
\begin{pframe}
\begin{figure}[]
\begin{center}
\includegraphics[height=0.7\textheight]{18333fig0201-tn}
\end{center}
\caption{The lifecycle of the status of your files.}
\end{figure}
Git itself will associate your files with various \textbf{states}
\footnote[1]{\scriptsize{Adapted from the
\href{http://git-scm.com/book/en/v2/Getting-Started-Git-Basics}
{Pro Git Book}.}}:
\begin{center}
\includegraphics[width=0.85\textwidth]{images/file_status_cycles.pdf}\\
\end{center}
\end{pframe}
\subsection{Adding, removing, renaming}
\subsection{Checking the status of your files}
\begin{pframe}
\begin{lstlisting}[language=none, caption=This file is not tracked.]
$ echo First version. > README
$ git status
# Untracked files:
# README
\end{lstlisting}
\pause
\begin{lstlisting}[language=none, caption=Now the file will be tracked.]
$ git add README
$ git status
# Changes to be committed:
# new file: README
\end{lstlisting}
To rename a file, use ``\lstinline{git mv}'', to remove, use
``\lstinline{git rm}''.
\begin{framed}
\prompt\ \cmd{echo}\ \cmdarg{"First version."}\ \op{$>$}\ \cmdarg{README}
\end{framed}
\end{pframe}
\section{Working with versions}
\subsection{commit}
\begin{pframe}
Once you have modified your files and added them to the \emph{staging area},
you can commit them (add them to the repository).
\begin{lstlisting}[language=none, caption=Commit a new version.]
$ git commit
\end{lstlisting}
This will open an editor, give a short description (50 characters) and a list
of the changes (72 column format).
\begin{itemize}
\item Useful when you want to search.
\end{itemize}
\pause
\begin{lstlisting}[language=none, caption=Commit a new version.]
$ git commit -m "Solved the counting bug."
\end{lstlisting}
\begin{framed}
\prompt\ \cmd{echo}\ \cmdarg{"First version."}\ \op{$>$}\ \cmdarg{README}\\
\prompt\ \gitcmd\ \cmd{status}\\
\cliout{On branch} \branch{master}\ \vspace{0.1cm}\\
\cliout{Initial commit}\ \vspace{0.1cm} \\
\cliout{Untracked files}\ \vspace{-0.1cm} \\
\cliout{(use "git add <file>..." to include in}\
\cliout{what will be committed)}\
\hspace*{0.4cm}\untrackedfile{README}\ \vspace{0.1cm}\\
\cliout{nothing added to commit but untracked files present}\ \vspace{-0.1cm}\\
\cliout{(use "git add" to track)}
\end{framed}
\end{pframe}
\subsection{Undoing changes}
\subsection{Adding/staging files}
\begin{pframe}
Sometimes you will accidentally add a file.
\begin{lstlisting}[language=none, caption=Adding a new version of a file.]
$ echo Second version. > README
$ git add README
$ git status
# Changes to be committed:
# modified: README
\end{lstlisting}
To start tracking the file, we need to stage it first:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\
\end{framed}
\pause
If you want to undo this, you can use ``\lstinline{git reset}''.
\begin{lstlisting}[language=none, caption=Unstage a file.]
$ git reset README
$ git status
# Changes not staged for commit:
# modified: README
\end{lstlisting}
We can see that the state of the file has changed:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{status}\\
\cliout{On branch} \branch{master}\ \vspace{0.1cm}\\
\cliout{Initial commit}\ \vspace{0.1cm} \\
\cliout{Changes to be committed:}\ \vspace{-0.1cm} \\
\cliout{(use "git rm --cached <file>..." to unstage)}\
\hspace*{0.4cm}\stagedfile{new file: README}\ \vspace{0.1cm}
\end{framed}
\vspace{-0.2cm}
Note also that the file is now in the \stagedfile{staging area}.
\end{pframe}
\subsection{Commit}
\begin{pframe}
Keep in mind that ``\lstinline{git reset}'' by default sets a staged file
back to unstaged.
\begin{lstlisting}[language=none, caption=Unstage a file.]
$ git reset README
$ git status
# Changes not staged for commit:
# modified: README
\end{lstlisting}
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{commit}\ \cmdopt{-m} \cmdarg{"First commit"}\\
\cliout{[}\branch{master}\ \commit{5466170}\cliout{] First commit}\\
\cliout{1 file changed, 1 insertion(+)}\\
\cliout{create mode 100644 README}
\end{framed}
\vspace{-0.2cm}
\pause
If the commit message is long, you can omit the \cmdopt{-m} flag and \gitcmd\
will open a text editor in which you can write your longer message.
\medskip
\pause
To discard all changes in an unstaged file, you can use
``\lstinline{git checkout}''
\begin{lstlisting}[language=none, caption=Discarding changes.]
$ git checkout -- .
$ git status
nothing to commit, working directory clean
\end{lstlisting}
We can see that the file state has changed:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{status}\\
\cliout{On branch} \branch{master}\\
\cliout{nothing to commit, working directory clean}
\end{framed}
\end{pframe}
\subsection{}
\begin{pframe}
For now, let's commit our change and move on.
\begin{lstlisting}[language=none, caption=Adding a new version of a file.]
$ echo Second version. > README
$ git commit
$ git status
nothing to commit, working directory clean
\end{lstlisting}
\vspace{-0.5cm}
Let's update the file now:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \cmd{echo}\ \cmdarg{"Second version."}\ \op{$>$}\ \cmdarg{README}
\end{framed}
Git knows that there are changes to the file:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{status}\\
\cliout{On branch} \branch{master}\\
\cliout{Changes not staged for commit:}\ \vspace{-0.1cm} \\
\cliout{(use "git add <file>..." to update what will be committed)}\ \vspace{-0.1cm} \\
\cliout{(use "git checkout -- <file>..." to discard changes}\ \vspace{-0.1cm} \\
\cliout{in working directory)}\\
\hspace*{0.4cm}\modifiedfile{modified: README}\ \vspace{0.1cm}\\
\cliout{no changes added to commit (use "git add" and/or}\ \vspace{-0.1cm} \\
\cliout{"git commit -a")}
\end{framed}
\end{pframe}
\subsection{Viewing the history}
\subsection{Check the differences}
\begin{pframe}
To see the history of your project, use ``\lstinline{git log}''.
\begin{lstlisting}[language=none, caption=The log of our project.]
$ git log
commit cc61ee7cd72590f3bebcc9e1ff3e9435c7f7dd28
Author: J.F.J. Laros <j.f.j.laros@lumc.nl>
Date: Fri Oct 11 14:18:13 2013 +0200
Second version.
commit 8e10be812fd78f69a5e3bac7670c62438161b6b0
Author: J.F.J. Laros <j.f.j.laros@lumc.nl>
Date: Fri Oct 11 14:17:51 2013 +0200
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{diff}\\
\cliout{diff --git i\/README w\/README}\ \vspace{-0.1cm}\\
\cliout{index}\ \commit{efe6f7c}\cliout{..}\commit{4fe6328}\ \cliout{100644}\ \vspace{-0.1cm}\\
\cliout{--- i\/README}\ \vspace{-0.1cm}\\
\cliout{+++ w\/README}\ \vspace{-0.1cm}\\
\diffsummary{@@ -1 +1 @@}\ \vspace{-0.1cm}\\
\diffminus{-First version.}\ \vspace{-0.1cm}\\
\diffplus{+Second version.}
\end{framed}
This compares the \modifiedfile{working directory} with the
\stagedfile{staging area for}\ \stagedfile{the next commit}.
The differences are what you could tell Git to further add to the staging area
but you still haven't.
\end{pframe}
First version.
\end{lstlisting}
\begin{pframe}
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\\
\prompt\ \gitcmd\ \cmd{diff}\\
\prompt\
\end{framed}
\end{pframe}
\subsection{Undo a commit}
\begin{pframe}
Sometimes we want to undo an entire commit. This is done with
``\lstinline{git revert}''.
\begin{lstlisting}[language=none, caption=Revert a commit.]
$ git revert cc61ee7cd7
$ cat README
First version.
\end{lstlisting}
The hash can be found with ``\lstinline{git log}''.
\bigskip
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\\
\prompt\ \gitcmd\ \cmd{diff}\\
\prompt\ \gitcmd\ \cmd{diff}\ \cmdopt{--cached}\\
\cliout{diff --git i\/README w\/README}\ \vspace{-0.1cm}\\
\cliout{index}\ \commit{efe6f7c}\cliout{..}\commit{4fe6328}\ \cliout{100644}\ \vspace{-0.1cm}\\
\cliout{--- i\/README}\ \vspace{-0.1cm}\\
\cliout{+++ w\/README}\ \vspace{-0.1cm}\\
\diffsummary{@@ -1 +1 @@}\ \vspace{-0.1cm}\\
\diffminus{-First version.}\ \vspace{-0.1cm}\\
\diffplus{+Second version.}
\end{framed}
\vspace{-0.3cm}
This displays the changes you staged for the \textbf{next commit} relative to the
\textbf{previous commit}.
\pause
You can also use a unique prefix of this hash, usually six characters is
enough.
You can also specify a specific \textbf{commit id} to which to compare the staged
files with: \gitcmd\ \cmdarg{diff}\ \cmdopt{--cached}\ \cmdarg{<commit-id>}
\end{pframe}
\subsection{Viewing changes in detail}
\subsection{}
\begin{pframe}
If you want to see what has changed, use ``\lstinline{git diff}''.
\begin{lstlisting}[language=none, caption=Difference between the working copy
and the staging area.]
$ echo Third version. > README
$ git diff
--- a/README
+++ b/README
@@ -1 +1 @@
-First version.
+Third version.
\end{lstlisting}
\vspace{-0.5cm}
To stage and commit the file again we could use:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{add}\ \cmdarg{README}\\
\prompt\ \gitcmd\ \cmd{commit}\ \cmdopt{-m}\ \cmdarg{"Second commit"}
\end{framed}
\pause
For staged files, use the ``\lstinline{--cached}'' or
``\lstinline{--staged}'' option.
\begin{lstlisting}[language=none, caption=Difference between the staging area
and the last commit.]
$ git diff --cached
\end{lstlisting}
\textbf{But}, since we have already tracked the file, you can also abbreviate
this into one \gitcmd\ \cmd{commit}\ command:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{commit}\ \cmdopt{-am} \cmdarg{"Second commit"}\\
\cliout{[}\branch{master}\ \commit{ef70f09}\cliout{] Second commit}\\
\cliout{1 file changed, 1 insertion(+), 1 deletion(-)}
\end{framed}
Notice the \cmdopt{-a} flag. This tells \gitcmd\ to automatically stage all
previously tracked files.
\end{pframe}
\subsection{Check commit history}
\begin{pframe}
You can see the differences between any two versions.
\begin{lstlisting}[language=none, caption=Difference between the working copy
and an other commit.]
$ git diff cc61ee
--- a/README
+++ b/README
@@ -1 +1 @@
-Second version.
+Third version.
\end{lstlisting}
\pause
% \vspace{-0.5cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{log}\\
\commit{commit 7a6e47cfbb38048b46937d9f8d2427a7e6e20936}\ \vspace{-0.1cm}\\
\cliout{Author: Zorro <zorro@poor.es>}\ \vspace{-0.1cm}\\
\cliout{Date: Tue Nov 24 16:13:59 2015 +0100}\ \vspace{0.1cm}\\
\hspace*{0.5cm}\cliout{Second commit}\ \vspace{0.1cm}\\
\commit{commit 54661709e859427358c97a94475643a7ccffa052}\ \vspace{-0.1cm}\\
\cliout{Author: Zorro <zorro@poor.es>}\ \vspace{-0.1cm}\\
\cliout{Date: Tue Nov 24 15:04:54 2015 +0100}\ \vspace{0.1cm}\\
\hspace*{0.5cm}\cliout{First commit}
\end{framed}
\end{pframe}
\begin{lstlisting}[language=none, caption=Difference between two committed
versions.]
$ git diff 8e10be cc61ee
\end{lstlisting}
\section{Restoring Previous Versions}
\subsection{Checkout}
\begin{pframe}
One that is used quite commonly is to discard a \modifiedfile{working directory}\
file changes and \textbf{restore its latest repository state}:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{checkout}\ \cmdopt{--}\ \cmdarg{\{filename\}}
\end{framed}
To discard all changes in the \modifiedfile{working}\
\modifiedfile{directory}:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{checkout}\ \cmdopt{--}\ \cmdarg{.}
\end{framed}
To retrieve a file from a specific commit into the \stagedfile{staging area}:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{checkout}\ \cmdarg{\{commit-id\}}\ \cmdarg{\{filename\}}
\end{framed}
\end{pframe}
% \subsection{Revert}
% \begin{pframe}
%
% \end{pframe}
\section{Extras}
\subsection{Explicit no tracking}
\subsection{Explicit not tracking}
\begin{pframe}
Sometimes you do not want to track certain files:
Certain files are not suitable for tracking by \gitcmd :
\begin{itemize}
\item Executables.
\item PDF files (if you still have the \LaTeX\ source).
\item Python bytecode (\lstinline{.pyc}) files.
\item Files containing passwords.
\item Binary files / executables;
\item PDF files / Microsoft Office files.
\end{itemize}
\medskip
\pause
Use the special ``\lstinline{.gitignore}'' file.
\begin{lstlisting}[language=none, caption=Ignoring certain files.]
$ touch notrack.txt
$ echo notrack.txt > .gitignore
$ git add .gitignore
$ git commit
$ git status
nothing to commit, working directory clean
\end{lstlisting}
Other files are also not meant to be tracked:
\begin{itemize}
\item Password-containing files;
\item Large files.
\end{itemize}
You can ignore these files by listing their names in a file called
\cmdarg{.gitignore} in your repository directory root:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \cmd{echo}\ \cmdarg{"my_password.txt"} \op{$>>$}\ \cmdarg{.gitignore}\\
\prompt\ \cmd{echo}\ \cmdarg{"*.pdf"} \op{$>>$}\ \cmdarg{.gitignore}\\
\prompt\ \gitcmd\ \cmd{add}\ \cmdarg{.gitignore}\\
\prompt\ \gitcmd\ \cmd{commit}\ \cmdopt{-m}\ \cmdarg{"Add .gitignore file"}
\end{framed}
\end{pframe}
\subsection{Help}
\subsection{Who edited what?}
\begin{pframe}
With the ``\lstinline{help}'' command get the manual of a particular
subcommand.
\begin{lstlisting}[language=none, caption=Get the full manual.]
$ git help <command>
\end{lstlisting}
\pause
\gitcmd\ \cmd{blame}\ shows you the last author of each line:
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{blame}\ \cmdarg{README}\\
\commit{a76f17de}\ \cliout{(Zorro 2015-11-24 16:13:59 +0100 1) Second version.}
\end{framed}
\end{pframe}
Example.
\begin{lstlisting}[language=none, caption=Get the manual for the diff
subcommand.]
$ git help diff
\end{lstlisting}
\subsection{Cleaning untracked files}
\begin{pframe}
% \gitcmd\ \cmd{clean}\ is your ``janitor''.
\vspace{-0.4cm}
\begin{framed}
\prompt\ \gitcmd\ \cmd{status}\\
\cliout{On branch} \branch{master}\ \vspace{0.1cm}\\
\cliout{Initial commit}\ \vspace{0.1cm} \\
\cliout{Untracked files}\ \vspace{-0.1cm} \\
\cliout{(use "git add <file>..." to include in}\
\cliout{what will be committed)}\
\hspace*{0.4cm}\untrackedfile{README.bkp}\ \vspace{0.1cm}\\
\cliout{nothing added to commit but untracked files present}\ \vspace{-0.1cm}\\
\cliout{(use "git add" to track)}\\
\prompt\ \gitcmd\ \cmd{clean}\\
\cliout{fatal: clean.requireForce defaults to true and neither}\\
\cliout{-i, -n, nor -f given; refusing to clean}\\
\prompt\ \gitcmd\ \cmd{clean}\ \cmdopt{-f}\\
\cliout{Removing README.bkp}
\end{framed}
\end{pframe}
\makeAcknowledgementsSlide{
......
......@@ -24,7 +24,7 @@
\definecolor{one-dot}{rgb}{0.9,0.2,0.0}
\definecolor{two-dots}{rgb}{0.11,0.11,0.8}
\definecolor{slash}{rgb}{0.3,0.3,0.3}
\definecolor{staged}{HTML}{2e8b57}
\definecolor{head}{HTML}{B22222}
\definecolor{branch}{HTML}{20b2aa}
\definecolor{commit-id}{HTML}{b8860b}
......@@ -36,3 +36,13 @@
\newcommand{\cmdopt}[1]{\textcolor{option-color}{\lstinline{#1}}}
\newcommand{\cliout}[1]{\textcolor{cli-text}{\lstinline{#1}}}
\newcommand{\important}[1]{\textbf{\textcolor{important-note}{#1}}}
\newcommand{\op}[1]{\textcolor{red}{#1}}
\newcommand{\branch}[1]{\textcolor{branch}{\lstinline{#1}}}
\newcommand{\commit}[1]{\textcolor{commit-id}{\lstinline{#1}}}
\newcommand{\untrackedfile}[1]{\textcolor{red}{\lstinline{#1}}}
\newcommand{\stagedfile}[1]{\textcolor{staged}{\lstinline{#1}}}
\newcommand{\modifiedfile}[1]{\textcolor{red}{\lstinline{#1}}}
\newcommand{\diffminus}[1]{\textcolor{red}{\lstinline{#1}}}
\newcommand{\diffplus}[1]{\textcolor{green}{\lstinline{#1}}}
\newcommand{\diffsummary}[1]{\textcolor{cyan}{\lstinline{#1}}}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment