diff --git a/git-primer.tex b/git-primer.tex index 6dcb12a..aae2bee 100644 --- a/git-primer.tex +++ b/git-primer.tex @@ -469,10 +469,128 @@ Let's go ahead and commit those using the following set of commands: \input{include/branch001} -Notice that we don't have a remote repository at this point, so all we are doing is making commits. +Notice that we don't have a remote repository at this point, so all we are doing is making commits. Running \texttt{git status} gives us this output: + +\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{}] +On branch main +nothing to commit, working tree clean +\end{Verbatim} + +Let's suppose we want to make some edits in this file, but we're not sure if we're going to keep them—we're just testing. To do that, we'll create a new branch called ``testbranch'' with this command: + +\input{include/branch002} + +Git immediately gives us this message: + +\begin{Verbatim}[] +Switched to a new branch 'testbranch' +\end{Verbatim} + +Now we're going to add a new line to \texttt{doc\a.txt} so it looks like this: + +\label{doc-aC2} +\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{doc\_a.txt New Contents}] +This is line A1. +This is line A2. +This is line B1. +\end{Verbatim} + +Now if we run \texttt{git status}, we'll see this: + +\begin{Verbatim}[] +On branch testbranch +Changes not staged for commit: + (use "git add ..." to update what will be committed) + (use "git restore ..." to discard changes in working directory) + modified: doc_a.txt + +no changes added to commit (use "git add" and/or "git commit -a") +\end{Verbatim} + +Again, Git provides us with helpful information here. It's telling us that we are on ``testbranch'' and not our main branch, it tells us that we have changes in a file, and that we have yet not committed those changes. What it's not telling us is what those changes are. To do that, we use the \texttt{git diff} command: + +\input{include/gitdiff} + +Which gives us this output: + +\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{Output of git diff}] +diff --git a/doc_a.txt b/doc_a.txt +index ea9c135..01b52bf 100644 +--- a/doc_a.txt ++++ b/doc_a.txt +@@ -1,2 +1,3 @@ + This is line A1. + This is line A2. ++This is line A3. +\end{Verbatim} + +In most terminals, this output will be color-coded, which is helpful on the fly, but we have line numbers here to help us. Let's take this line by line to understand what Git is telling us. + + +\begin{itemize} +\item Line 1 (\texttt{diff --git a/doc\_a.txt b/doc\_a.txt}) shows us that we are examining two different versions of the same file. Git is calling the original version ``a'' and the new version ``b'' +\item Line 2 (starting with ``\texttt{index}'') is meta data about the files, and is not terribly useful. \texttt{100644} shows that this is a normal file, rather than an executable one (which would be \texttt{100755}) or a symbolic link (which would be \texttt{120000}). +\item Lines 3 and 4 show that Git is assigning a minus sign (-) to the ``a'' version of the file and a plus sing (+) to the ``b'' version of the file, which it will use starting in line 6. +\item Line 5 is a \textbf{chunk header}. Git is not going to show you the entire contents of your file, but rather just the chunks that have differences. (Except in our example, which is a very small file.) + \begin{itemize} + \item The chunk header starts and ends with \texttt{@@}. + \item ``\texttt{-1, 2}'' is actually three separate pieces of information. Git is telling us that it is going to show us 2 lines, starting at line 1, from the ``a'' version of our file (because of the minus sign at the beginning). + \item ``\texttt{+1, 3}'' is again three separate pieces of information. Git is telling us that it is going to show us 3 lines, starting at line 1, from the ``b'' version of our file (because of the plus sign at the beginning). + \end{itemize} +\item Lines 6-8 are the lines that are different. Notice that line 8 starts with a plus sign, meaning that this line appears in version ``b'' only. +\end{itemize} + +Let's make a couple of other changes, and see how this output changes. + +First, let's \textit{delete} the first line (``This is line A1.''). + +Second, let's create a new file (which we'll call \texttt{doc\_b.txt}) which will contain a single line of text: ``This is line B1.'' + +Running \texttt{git diff} now gives us slightly different output: + +\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{Output of git diff}] +diff --git a/doc_a.txt b/doc_a.txt +index 01b52bf..505be23 100644 +--- a/doc_a.txt ++++ b/doc_a.txt +@@ -1,3 +1,2 @@ +-This is line A1. + This is line A2. + This is line A3. +\end{Verbatim} + +This is similar to before, but now we are seeing that line 6 now has a minus sign in front of it, because that line only exists in the original version of that file. But it doesn't show us any information about \texttt{doc\_b.txt} because file is completely new. + +How does this test branch compare to our main branch? First, let's add both files (\texttt{git add -A}) and make a commit (\texttt{git commit -m "commit message"}). Now we can ask Git to tell us the difference between these two branches using this command: + +\input{include/gitdiffbranch} + +\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{Output of git diff main testbranch}] +diff --git a/doc_a.txt b/doc_a.txt +index ea9c135..505be23 100644 +--- a/doc_a.txt ++++ b/doc_a.txt +@@ -1,2 +1,2 @@ +-This is line A1. + This is line A2. ++This is line A3. +diff --git a/doc_b.txt b/doc_b.txt +new file mode 100644 +index 0000000..3fc1659 +--- /dev/null ++++ b/doc_b.txt +@@ -0,0 +1 @@ ++This is line B1. +\end{Verbatim} + +Lines 2-8 tell us about ``doc\_a.txt'' and lines 9-15 tells us about ``doc\_b.txt''. + +Notice that line 12 is telling us that the ``a'' version of ``\texttt{doc\_b.txt}'' (that is, the version that exists in the main branch) is ``\texttt{/dev/null}'' which is Git's way of telling us that this file does not exist at all in the main branch. In fact, line 14 shows us \texttt{-0,0} which means that Git is showing us zero lines starting at line zero from the ``a'' version of this file. + +\section{Uses of Branching} + + -\todo[inline]{Talk about git \texttt{diff here}.} - \chapter{Collaborating With Yourself — A Guide for Creatives} diff --git a/include/branch002.tex b/include/branch002.tex new file mode 100644 index 0000000..b1093ca --- /dev/null +++ b/include/branch002.tex @@ -0,0 +1,3 @@ +\begin{Verbatim}[] +$ git checkout -b testbranch +\end{Verbatim} diff --git a/include/gitdiff.tex b/include/gitdiff.tex new file mode 100644 index 0000000..c4f26a0 --- /dev/null +++ b/include/gitdiff.tex @@ -0,0 +1,3 @@ +\begin{Verbatim}[] +$ git diff +\end{Verbatim} diff --git a/include/gitdiffbranch.tex b/include/gitdiffbranch.tex new file mode 100644 index 0000000..916b31e --- /dev/null +++ b/include/gitdiffbranch.tex @@ -0,0 +1,3 @@ +\begin{Verbatim}[] +$ git diff main testbranch +\end{Verbatim}