Skip to content

GeneralGit from scratch

2. Change history

Guide · git, version control · updated on 21/07/2026

Author: Ángel Fernández Caravaca
Date: July 21, 2026

In the previous chapter we established what a commit is and how it moves through Git's three areas. That gave us a way to record history, but not yet to read it or undo it. Right now, every commit you have made lives inside .git/objects, but we have no way of inspecting it. In this chapter we will learn how to trace what we did, compare two points in time and undo a change without losing our work.

Reading the history

The most direct way to trace what you have done in the repository is git log, which lists commits from newest to oldest, walking backwards along the chain of parents we described in Chapter 1. The command is:

git log

Git will immediately reply with a list of blocks like this one:

1
2
3
4
5
commit <hash - identifier>
Author: user.name <user.email>
Date:   full date

    commit message

The default view of this command, while useful, is fairly basic and only shows the commits' metadata. You can get more information with flags. These flags don't change what git log does, only how much of each commit is shown and in what format. Some examples are:

1
2
3
4
git log --oneline           # one line per commit: short hash + subject
git log --oneline --graph   # adds a text graph of branches and merges
git log -p                  # shows the full diff introduced by each commit
git log --stat              # shows only which files changed, and by how much

Note

VSCode offers extensions (such as Git Graph or GitLens) that show this same information graphically instead of as text in the terminal. For example: the commit graph you get with git log --oneline --graph is shown visually in the Graph view of the Source Control panel. There is a later chapter devoted to these extensions, but it is worth knowing these commands exist before moving on to the prettier versions built on top of them.

As well as controlling the format and how much of each commit is shown, you can also narrow the history down to the commits you are interested in instead of going through all of it. Some useful flags are:

1
2
3
4
5
git log -n 5                     # only the last 5 commits
git log --author="Ángel"         # commits by a specific author
git log --since="2 weeks ago"    # commits after a date
git log -- <file>                # only the commits that touched this file
git log --grep="fix"             # commits whose message contains this text

Comparing two states

If git log tells you what you did, git diff tells you what you changed, expressed as added and removed lines.

To understand how to use this command, remember the three areas from Chapter 1: working directory, staging area and repository. git diff always compares two of them, and which ones depends on the flags you pass it:

Command Compares Intuition
git diff Working area vs staging area What you have edited but not staged yet
git diff --staged Staging area vs HEAD What you are about to commit, compared with your last commit on the current branch
git diff HEAD Working area vs HEAD Everything you have changed since your last commit on the current branch, staged or not

Important

This is one of the most common sources of confusion among beginners: plain git diff does not show you what has changed since your last commit, but what is not staged. If you have already added everything with git add, plain git diff prints nothing, even though your working directory is clearly different from the last commit.

Three things to keep in mind when running this command in any of its variants:

  • File header: each file in the diff starts with the two versions being compared. Both paths are usually identical, since it is the same file at two different moments.
--- a/File   # the old version
+++ b/File   # the new version
  • Line prefixes: each line carries a one-character prefix that indicates what happened to it. They are also coloured to make them easier to read. Notice too that there is no prefix for modified, since editing a line is recorded as a removal followed by an addition.
1
2
3
+> this line was added
-> this line was removed
>   this line has not changed; it is context shown only to help you find your way
  • Hunk header: each block of changes starts with a line such as @@ -7,3 +7,4 @@. Read it as coordinates: the hunk spans 3 lines starting at line 7 of the old version, and 4 lines starting at line 7 of the new one.

Once you feel comfortable using git diff to compare your current work, it is worth knowing that the command is far more powerful than that. You can compare any two points in history, not just the three areas: two arbitrary commits, a commit against your working directory or the tips of two branches.

1
2
3
git diff <hash1> <hash2>       # between two specific commits
git diff HEAD~2 HEAD           # between two commits ago and now (see HEAD~n below)
git diff <branch1> <branch2>   # between the latest commits of two branches

To find the hashes you need here, run git log, which is the command that tells you what you did; the short hash printed by git log --oneline is enough to identify a commit.

Tip

When you run git log or git diff, keep in mind that:

  • Press q to quit.
  • Press Space to move forward a page.
  • Press b to move back a page.
  • ↑ / ↓ to move line by line.
  • g / G to jump to the beginning or the end.
  • /text to search for a specific word or text. In this mode, press n / N to go to the next or previous match. (/New function)

Inspecting a single commit

Whereas git diff compares two points, git show looks at just one. It prints a commit's metadata followed by the diff it introduced relative to its parent, so it behaves like a "combination" of git log and git diff. It also accepts the formatting flags of both:

1
2
3
git show                  # the commit HEAD points to, i.e. the latest one
git show <hash>           # a specific commit: metadata + the diff it introduced
git show <hash> --stat    # the same, but only which files changed and by how much

In particular, I would like to mention a second use of this command, which is where it really shines. You can ask for the content of a single file as it existed in that commit:

git show <hash>:path/to/file   # the file as it was in that commit
git show HEAD:path/to/file     # the file as it was in your last commit

For example: if you want to see what README.md looked like ten commits ago, you don't need to move anywhere: git show HEAD~10:README.md prints it straight to the terminal, leaving your working directory exactly as it is.

HEAD: your position in the history

In Chapter 1 we introduced HEAD as a file that tells Git which branch you are on. It is worth expanding that definition now, because it has already appeared in earlier commands, and the ones coming next are defined entirely in terms of it.

In reality, HEAD is a pointer to a branch, which in turn points to that branch's latest commit. You could say it is a pointer to a pointer. In short, HEAD contains the path to a file in refs/heads/, which is your current branch. Inside that branch file there is a hash related to a file in objects/, that is, that branch's latest commit.

Understanding this is quite useful, since it gives you an intuition for how the previous commands work. The image below shows how Git resolves HEAD when you use it in the git diff HEAD command.

As mentioned earlier, you can also control how far back you go from HEAD, that is, how many commits you want to travel back in time:

Reference Means
HEAD the current commit
HEAD~1 (or HEAD^) the parent of the current commit
HEAD~2 the grandparent: two steps back
HEAD~n n steps back, following the first parent each time

Undoing changes: restore, reset and revert

Git provides three different commands to undo something, and the reason there are three and not one is that undoing can mean different things depending on where the change lives and whether it has already been shared with others. Mixing them up is the most common way to lose work by accident, so the aim of this section is to make the boundaries between them clear.

The question to ask yourself before typing any of these commands is always: which area am I trying to undo, and has this already been pushed?

git restore: undoing in the working directory or the staging area

This is the narrowest and safest of the three, and it acts before history comes into play, that is, only on the working directory and the staging area.

Command Effect Intuition
git restore <file> Discards the changes in the working directory, going back to the last commit. It is like undoing every change with ctrl + z, only faster
git restore --staged <file> Unstages a file while keeping the edits in the working directory It simply undoes the git add <file>

Something important to keep in mind is that git restore <file> is destructive for the working directory, since the edits you made are overwritten with the version from the last commit, without asking for confirmation. In contrast, git restore --staged is harmless, since it only moves the file from the staging area back to the working directory, so your edits are not touched at all; they are simply no longer marked as ready for the commit.

The following image gives an intuitive outline of what git restore does. In particular:

  • git restore <file>: in the diagram it is represented by the folder. Notice that it appears blue in the last commit but yellow in the working area, which means you have modified it since the commit. If you run git restore <folder>, it turns blue again, that is, back to the state Git had recorded. I focus on the folder only to make it easier to follow, but keep in mind that git restore only touches the paths you pass it: running it on a single file leaves everything else in the working area exactly as it is. It feels like going back to the last commit, undoing every change you made to that file.

  • git restore --staged: in the diagram it is represented by the Excel file. Notice that the Excel file is in the staging area with a red cross. This is what you get when you run git restore --staged <excel>: the Excel file, which was staged and ready for the commit, is no longer there. Bear in mind that the file itself is not touched in your working area. The only thing you undo is the git add.

Note

I hope these diagrams clarify the concepts rather than confuse. I came up with them myself, so feel free to suggest a better representation.

git reset: moving the branch pointer

git reset acts one level higher: it moves what the current branch points to, that is, it rewrites which commit HEAD (and therefore your branch) considers "the latest". Everything after the target commit is left behind, unreachable from the branch, though not necessarily deleted yet.

To put it simply, you travel back in time to an earlier commit, and your branch travels with you. Everything you did between that commit and where you were is left behind: it is not deleted, the branch simply forgets it and stops counting those commits as part of its history. From there you keep working, and your next commits grow from that point, opening the line called New timeline in the diagram.

The following image gives an intuitive outline of what git reset does. In particular:

You created the repository and started making changes and commits. That is the whole horizontal line. At some point you decided to undo a lot of work by going back to the fourth commit. From there your branch carries on, opening the new timeline drawn in red. Notice that all the commits between where you were and the commit you went back to are "forgotten": they are not deleted, but your branch no longer counts them as part of its history. This is shown by the dashed grey line.

Note

I hope these diagrams clarify the concepts rather than confuse. I came up with them myself, so feel free to suggest a better representation.

Like every command we have seen so far, git reset accepts flags. Here they are not merely cosmetic, since the command takes a target commit and a mode that decides which of the three areas are dragged back along with the branch. The branch pointer always moves; what changes is whether the staging area and the working directory are rewritten to match.

Mode Branch pointer Staging area Working directory
git reset --soft moves unchanged (keeps the previous staging) unchanged
git reset (--mixed) moves reset to match <commit> unchanged. This is the default mode.
git reset --hard moves reset to match <commit> reset to match <commit>

Read this table as a gradient of how much each mode touches: --soft moves the least and --hard the most. For example: if you have just made a commit and realise it should have been two commits instead of one, git reset --soft HEAD~1 undoes the commit while leaving all the changes neatly staged, ready to split and commit again. git reset --hard HEAD~1, on the other hand, undoes the commit and rewinds your working directory to the previous state, so those changes disappear from disk too.

The commit itself does not disappear: it is still in objects/ and the reflog remembers where HEAD was, so git reset --hard ORIG_HEAD takes you straight back. What you will never get back is whatever was not committed at that moment, staged or not, since that content never reached the repository.

To make it clear:

Branch pointer Index (staging) Working directory Content of your files
--soft Goes to commit i Stays at commit i+n Stays at commit i+n that of i+n
--mixed Goes to commit i Goes to commit i Stays at commit i+n that of i+n
--hard Goes to commit i Goes to commit i Goes to commit i that of i

git revert: undoing with a new commit

Both restore and reset work by moving or rewriting pointers and content, which is fine as long as nobody else has seen the affected commits. As soon as you have pushed a commit and someone may have built work on top of it, rewriting history underneath them is exactly what you must avoid, since their copy and yours would no longer agree on what the history is.

git revert solves this without ever touching the past. Instead, it looks at what a specific commit changed, computes exactly the opposite of that change and applies it as a new commit on top of the current history:

git revert <commit>

For example: if commit a3f9c21 added a line, git revert a3f9c21 creates a new commit that removes that same line. The original commit is untouched and remains in the history exactly as it was; the repository now simply contains a commit that undoes another, both visible, both permanent, both honest about what happened.

The following image gives an intuitive outline of what git revert does. In particular:

Important

This is the rule of thumb this course will repeat again and again: use reset (or the interactive rewriting tools from a later chapter) only on commits that are still local and have not been pushed; once a commit is public, prefer revert. The former rewrites history, which is fine as long as it is yours alone; the latter extends it, which is the only safe option when others depend on what is already there.

Summing up the three, ordered by how much history they are willing to touch:

Command Undoes Rewrites history? Safe after push?
git restore working directory / staging area no (nothing committed yet) —
git reset which commit the branch points to yes no
git revert the effect of a past commit no, it adds a new commit yes

gitignore: keeping noise out of the history

Not everything in your working directory belongs in the repository: build artefacts, dependency folders, editor configuration, credentials. Chapter 1 already mentioned info/exclude as the private, unshared way to ignore files. .gitignore is its public counterpart: a plain text file, committed alongside your project, that lists patterns Git must never treat as untracked files, no matter how many times you run git add ..

1
2
3
4
5
6
# .gitignore
node_modules/       # ignore this whole folder
*.log                # ignore every file ending in .log
.env                 # ignore this specific file
build/               # ignore this whole folder
!build/keep-this.txt # exception: keep tracking this file despite the previous rule

Each line is a pattern: a trailing / restricts the rule to directories, a leading * matches any characters before the rest of the pattern, and a leading ! negates an earlier rule, creating an exception.

Warning

.gitignore only stops Git from picking up files it has never tracked before. If a file was committed at some point in the past, adding it to .gitignore afterwards does nothing: Git keeps tracking it, since the rule only affects untracked files. To stop tracking it from now on while keeping it on disk, you need git rm --cached <file>, commit that removal, and only then does the .gitignore rule take effect.

Tip

GitHub maintains a collection of ready-made .gitignore templates by language and framework, available when you create a repository, and they are a good starting point instead of writing one from scratch every time.