Added information on viewing commits

This commit is contained in:
Kenneth John Odle 2025-01-25 15:51:44 -05:00
parent 162bfc0fcb
commit d80ee23ae1
5 changed files with 76 additions and 1 deletions

View File

@ -335,7 +335,70 @@ If you're happy with the files you've staged, we can now commit them by using \t
\section{How to View Commits}
\todo[inline]{Add information about viewing commits; include shortcut for --rev-list}
The entire point of Git is that any work you commit is never lost, so that if you delete something that you later decide you want back, it's just a matter of going back to one of those earlier commits, getting the information that you need, copying it, returning to your current version, and pasting the information back in. Fortunately, Git has plenty of ways of viewing your earlier commits.
The most straightforward way (and probably the least useful, as well) is to simply view the log:
\input{include/gitlog}
which will give you output similar to this:
\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{Output of git log}]
commit 162bfc0fcb2f8399985e6f7eab8503a2c227f2a3 (HEAD -> main, ogit/main)
Author: Kenneth Odle <email@redacted.net>
Date: Sat Jan 25 14:56:02 2025 -0500
Removed section on bash aliases (beyond the scope)
commit 0d9e1fee1de24951cabd928a3c2bc85aee355f02
Author: Kenneth Odle <email@redacted.net>
Date: Fri Jan 24 14:28:07 2025 -0500
Added information about workflows
\end{Verbatim}
In this example, each commit is identified by a unique SHA-1 hash (the forty characters appearing after the word ``commit'', followed by the author, their email, the date and time the commit was made, and finally, the commit message. Notice that the first commit is also labeled with \texttt{(HEAD -> main, ogit/main)}, showing that this commit is the latest commit.
Git will show you a screenful of information at a time. You can use the down arrow on your keyboard to view more, and press the ``q'' key on your keyboard to break out of the command.
I said that this is probably the least useful way of viewing commits because if your project has a lot of commits, you will have to scroll through a lot of information to find what you are looking for, unless it was in a very recent commit.
If you want to see how many commits you have, you can use the \texttt{rev-list} command:
\input{include/revlist}
which will simply return a number indicating the number of commits, and thus how many screenfuls of information you may need to scroll through to find data from an early commit.
However, you can use some options with \texttt{git log} to get just the information you need. For example, you the \texttt{--pretty=oneline} option to show only the SHA hash and the description for each commit on a single line.
\input{include/gitlogoneline}
which will produce output like this:
\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{Output of git log with pretty=oneline option}]
162bfc0fcb2f8399985e6f7eab8503a2c227f2a3 (HEAD -> main, ogit/main) Removed section on bash aliases (beyond the scope)
0d9e1fee1de24951cabd928a3c2bc85aee355f02 Added information about workflows
d37d470c95a7d02a76a4cabea22960a74cf8a2cc Added inline to-do items
02d40255da4acde637c953fda7727f09ba8ca091 Updated changelog, version number
43e59f77dee0b2ee42a44838ab561236273383a9 Significant addtions to branching chapter
\end{Verbatim}
The \texttt{--pretty} option has a lot of configuration options that you can experiment with to get just the information you need. But if you have a lot of commits, the simplest option is just to send all that output to a text file, which you can open with a text editor and then search. For example, this command:
\input{include/gitprettyoptions}
sends the output of that command to a file (\texttt{log.txt}), the first few lines of which look like this:
\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{}]
2025-01-25 162bfc0 Kenneth Odle Removed section on bash aliases (beyond the scope)
2025-01-24 0d9e1fe Kenneth Odle Added information about workflows
2025-01-22 d37d470 Kenneth Odle Added inline to-do items
2025-01-22 02d4025 Kenneth Odle Updated changelog, version number
2025-01-22 43e59f7 Kenneth Odle Significant addtions to branching chapter
\end{Verbatim}
Here we get the date of the commit (in YYYY-MM-DD format), followed by an abbreviated commit hash, the name of the comitter (important if you have more than one person working on this project), and finally the commit description.
\chapter{Remote Repositories}

3
include/gitlog.tex Normal file
View File

@ -0,0 +1,3 @@
\begin{Verbatim}[]
$ git log
\end{Verbatim}

View File

@ -0,0 +1,3 @@
\begin{Verbatim}[]
$ git log --pretty=oneline
\end{Verbatim}

View File

@ -0,0 +1,3 @@
\begin{Verbatim}[]
$ git log --pretty=format:"%as %h %an %s" > log.txt
\end{Verbatim}

3
include/revlist.tex Normal file
View File

@ -0,0 +1,3 @@
\begin{Verbatim}[]
$ git rev-list --count HEAD
\end{Verbatim}