Skip to content

Commit

Permalink
Finished text based single line commits
Browse files Browse the repository at this point in the history
  • Loading branch information
cbx33 committed Apr 13, 2011
1 parent 03b5567 commit f67ec36
Showing 1 changed file with 260 additions and 1 deletion.
261 changes: 260 additions & 1 deletion afterhours5.tex
Expand Up @@ -49,4 +49,263 @@ \subsection{Taking commits that little bit further}
john@satsuki:~/coderepo$
\end{Verbatim}

Now, we could just do \texttt{git commit -a} and be done with it, but what if we really wanted to split this information up into four commits? We will introduce a new parameter to our \texttt{git add} tool from before. We are going to use the \texttt{git add -p} or \texttt{git add --patch}
Now, we could just do \texttt{git commit -a} and be done with it, but what if we really wanted to split this information up into four commits? We will introduce a new parameter to our \texttt{git add} tool from before. We are going to use the \texttt{git add -p} or \texttt{git add --patch}. This will allow us to interactively edit the hunks before they are committed. To begin with, let us run \texttt{git add -p} and see what it is we need to do.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
john@satsuki:~/coderepo$ git add -p
diff --git a/newfile1 b/newfile1
index 44640b2..0eccf1a 100644
--- a/newfile1
+++ b/newfile1
@@ -1,2 +1,4 @@
-A new file
-and some more awesome changes
+This is line 1
+This is line 2
+This is line 3
+This is line 4
Stage this hunk [y,n,q,a,d,/,e,?]?
\end{Verbatim}

We are given the options of \texttt{y,n,q,a,d,/,e,?}. At first glance, this may seem rather daunting. Let us choose the \texttt{?} and see what help is presented to us.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
Stage this hunk [y,n,q,a,d,/,e,?]? ?
y - stage this hunk
n - do not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
@@ -1,2 +1,4 @@
-A new file
-and some more awesome changes
+This is line 1
+This is line 2
+This is line 3
+This is line 4
Stage this hunk [y,n,q,a,d,/,e,?]?
\end{Verbatim}

So it appears that Git is offering us the opportunity to either
\begin{itemize}
\item Stage it
\item Do not stage it
\item Quit,
\item Stage it and all remaining hunks
\item Do not stage it or any of the remaining ones
\item Search for a regex
\item Edit the hunk
\end{itemize}

In fact, though the help mentioned a \textbf{split} command, we do not have this option available to us, due to the nature of our hunk. Instead, if we wish to split this hunk, we are going to have to edit it manually. To do this we will choose the \texttt{e} option.

Here we are left in our chosen text editor, to either add, modify or remove lines from the hunk. In our case, we are going to delete a few lines. We only want to leave the first line of additions. Just because we delete the others does not mean they are deleted from the working copy. Remember, we are not editing the actual files here. Just the hunks that are going to be staged.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
# Manual hunk edit mode -- see bottom for a quick guide
@@ -1,2 +1,4 @@
-A new file
-and some more awesome changes
+This is line 1
+This is line 2
+This is line 3
+This is line 4
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.
\end{Verbatim}

After the deletes, the editors file should look like this.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
# Manual hunk edit mode -- see bottom for a quick guide
@@ -1,2 +1,4 @@
-A new file
-and some more awesome changes
+This is line 1
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging. If it does not apply cleanly, you will be given
# an opportunity to edit again. If all lines of the hunk are removed,
# then the edit is aborted and the hunk is left unchanged.
\end{Verbatim}

Once we quit our editor, we are then asked about the next hunk. In our case, we are going to apply all of the changes to our second file during this commit. To do this we are going to use the \texttt{a} option.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
diff --git a/newfile2 b/newfile2
index 3545c1d..40efcce 100644
--- a/newfile2
+++ b/newfile2
@@ -1,2 +1,4 @@
Another new file
and a new awesome feature
+This is a new line
+This is another new line
Stage this hunk [y,n,q,a,d,/,e,?]? a
john@satsuki:~/coderepo$
\end{Verbatim}

At first glance, we do not appear to have been left with any indication that anything has taken place. In order to perform a check we shall run out obligatory \texttt{git diff}, both between the working copy and the index, and between the index and the last commit.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
john@satsuki:~/coderepo$ git diff
diff --git a/newfile1 b/newfile1
index f702b65..0eccf1a 100644
--- a/newfile1
+++ b/newfile1
@@ -1 +1,4 @@
This is line 1
+This is line 2
+This is line 3
+This is line 4
john@satsuki:~/coderepo$
\end{Verbatim}

So above, we can see that the difference between the working copy and the index, or staging area, is the last three lines that we are not ready to commit yet. Below we can see the difference between the staging area and the last commit to the repository. This includes the three lines that we included during our interactive commit preparation.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
john@satsuki:~/coderepo$ git diff --cached
diff --git a/newfile1 b/newfile1
index 44640b2..f702b65 100644
--- a/newfile1
+++ b/newfile1
@@ -1,2 +1 @@
-A new file
-and some more awesome changes
+This is line 1
diff --git a/newfile2 b/newfile2
index 3545c1d..40efcce 100644
--- a/newfile2
+++ b/newfile2
@@ -1,2 +1,4 @@
Another new file
and a new awesome feature
+This is a new line
+This is another new line
john@satsuki:~/coderepo$
\end{Verbatim}

We can now commit in the normal way, and continue to edit the working copy to stage the sections we require. The following output is shortened for brevity.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
john@satsuki:~/coderepo$ git commit -m 'Added first line'
[fantasy 03bd20c] Added first line
2 files changed, 3 insertions(+), 2 deletions(-)
john@satsuki:~/coderepo$ git add -p
diff --git a/newfile1 b/newfile1
index f702b65..0eccf1a 100644
--- a/newfile1
+++ b/newfile1
@@ -1 +1,4 @@
This is line 1
+This is line 2
+This is line 3
+This is line 4
Stage this hunk [y,n,q,a,d,/,e,?]? e
john@satsuki:~/coderepo$ git commit -m 'Added second line'
[fantasy 302e3fa] Added second line
1 files changed, 1 insertions(+), 0 deletions(-)
john@satsuki:~/coderepo$ git add -p
...
...
...
\end{Verbatim}

We have gone through the process of editing each hunk for each commit and have performed the commits.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
john@satsuki:~/coderepo$ git log
commit a59e73b1dc571318a1154aa4c2fc591ab6f1f395
Author: John Haskins <john.haskins@tamagoyakiinc.koala>
Date: Wed Apr 13 23:56:39 2011 +0100
Added fourth line
commit 3ca3d627a54418be4c2e9d9196db6ce62e2b93ff
Author: John Haskins <john.haskins@tamagoyakiinc.koala>
Date: Wed Apr 13 23:56:13 2011 +0100
Added third line
commit 302e3fa5f880a2a503235667b4c96d4dcdaa11be
Author: John Haskins <john.haskins@tamagoyakiinc.koala>
Date: Wed Apr 13 23:55:57 2011 +0100
Added second line
commit 03bd20cb8a78a28f003ab402492cf7055f21bb2e
Author: John Haskins <john.haskins@tamagoyakiinc.koala>
Date: Wed Apr 13 23:55:32 2011 +0100
Added first line
\end{Verbatim}

Next we are going to see how to perform exactly the same procedure using \texttt{git gui}. For a start, we are going to reset our branch HEAD and our index back to their state before we made the last four commits, however we are going to leave the working copy files in the state they were after these last four commits. This is called a \textbf{mixed} reset, as it modifies the staging area and the HEAD, but does not touch the working files.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
john@satsuki:~/coderepo$ git log --oneline
a59e73b Added fourth line
3ca3d62 Added third line
302e3fa Added second line
03bd20c Added first line
d50ffb2 Merged in zaney
ed2301b Removed third file
...
...
john@satsuki:~/coderepo$ git reset --mixed d50ffb2
Unstaged changes after reset:
M newfile1
M newfile2
john@satsuki:~/coderepo$
\end{Verbatim}

Now we will run a diff, just to be sure.

\begin{Verbatim}[frame=leftline,framerule=1mm,fontsize=\relsize{-3}]
john@satsuki:~/coderepo$ git diff
diff --git a/newfile1 b/newfile1
index 44640b2..0eccf1a 100644
--- a/newfile1
+++ b/newfile1
@@ -1,2 +1,4 @@
-A new file
-and some more awesome changes
+This is line 1
+This is line 2
+This is line 3
+This is line 4
diff --git a/newfile2 b/newfile2
index 3545c1d..40efcce 100644
--- a/newfile2
+++ b/newfile2
@@ -1,2 +1,4 @@
Another new file
and a new awesome feature
+This is a new line
+This is another new line
john@satsuki:~/coderepo$
\end{Verbatim}

If we now run our \texttt{git gui} command, we will see the changes that we have once we click on one of the files in the left hand portion of the screen.

0 comments on commit f67ec36

Please sign in to comment.