Jose Robledo
04 March 2025
You can refer to the most recent commit of the working
directory by using the identifier HEAD
.
Before we start, let’s make a change to guacamole.md
,
adding yet another line. Lets say we want to add a change we don’t want,
for example, let’s add in the instructions a phrase “Do nothing.”
Note that HEAD
is the default option for
git diff
, so omitting it will not change the command’s
output at all (give it a try). However, the real power of
git diff
lies in its ability to compare with previous
commits. For example, by adding ~1
(where “~” is “tilde”,
pronounced [til-duh]), we can look at the
commit before HEAD
.
If we want to see the differences between older commits we can use
git diff
again, but with the notation HEAD~1
,
HEAD~2
, and so on, to refer to them:
We could also use git show
which shows us what changes
we made at an older commit as well as the commit message, rather than
the differences between a commit and our working directory that
we see by using git diff
.
We can also refer to commits using those long strings of digits and
letters that both git log
and git show
display. These are unique IDs for the changes, and “unique” really does
mean unique: every change to any set of files on any computer has a
unique 40-character identifier.
Typing out random 40-character strings is annoying, so Git lets us use just the first few characters (typically seven for normal size projects)
Let’s suppose we change our mind about the last update to
guacamole.md
(the “ill-considered change” “Do
nothing”).
git status
now tells us that the file has been changed,
but those changes haven’t been staged:
We can put things back the way they were by using
git restore
:
By default, it recovers the version of the file recorded in
HEAD
, which is the last saved commit.
If we want to go back even further, we can use a commit identifier
instead, using -s
option:
Notice that the changes are not currently in the staging area, and
have not been committed. If we wished, we can put things back the way
they were at the last commit by using git restore
to
overwrite the working copy with the last committed version. We use the
.
to mean all files.
data_cruncher.py
(more than one can be correct)?$ git restore
$ git restore data_cruncher.py
$ git restore -s HEAD~1 data_cruncher.py
$ git restore -s <unique ID of last commit> data_cruncher.py
The command git revert
is different from
git restore -s [commit ID] .
because
git restore
returns the files not yet committed within the
local repository to a previous state, whereas git revert
reverses changes committed to the local and project repositories.
Content