Exploring Git History

So Git stores away all this history, what can we do with it?

git log

Let’s check how far we have come! Take a look at your repository’s history of commits:

git log

git log --oneline

When a long text display fills the terminal window, navigate up and down using the arrow keys or Page Down key. To exit the display, type q.

Compare this display to the “Commits” page on your GitHub repository. Using git log can be handy for a quick reminder of what you were working on, but it is often easier to review your history on GitHub visually!

git diff

We can compare current files with the history. Modify one of the files in your repository using a text editor or the command line.

echo "more important notes!" >> notes.txt

git status

git diff

git diff notes.txt

git add notes.txt

git diff

git commit -m "testing diff"

git diff

The diff command allows us to see what changes were made to the currently unstaged files. It’s good practice to check this before you commit to ensure you know what you are changing.

Many text editors have a diff visualization built in, which is very handy. Viewing the diff can help you understand exactly what is changed and debug problems in your work.

git checkout

Wait that last commit was no good! How can we undo?

Let’s get the earlier version back with checkout.

You can track individual commits using their id given by git log. Or we can refer to them sequentially. The most recent commit is called HEAD. One back is called HEAD~1 (“head minus one”), and so on. If you git checkout a specific file, it will reset it to the specified commit in your working directory.

git checkout HEAD~1 notes.txt

git status

git diff HEAD notes.txt

git checkout HEAD notes.txt

git status

Those two checkouts undid each other!

If you wanted to save the undo (the version from checkout HEAD~1), remember to add and commit the checked out version:

git checkout HEAD~1 notes.txt

git status

git add notes.txt

git commit -m "undo notes changes"

Checking it out is simply introducing a change, just like editing the file in your working directory. This keeps your history linear, ensuring that Git has a record of everything, just in case you want to undo your undo.

If you get ‘detached HEAD’ warning, type git checkout main to get back to normal.

amend

If you just made a commit, and haven’t pushed it yet, but suddenly realize you forgot to add something, need to change the message, or tweak a file, use git commit --amend:

  1. Make the changes you need (such as editing one more file, deleting something, fixing a spelling error, what ever).
  2. git add any changes.
  3. git commit --amend -m "new commit message that replaces the old one"

This commit will be combined with the last one into a single new commit as if the first one never happened. If you just want to change the commit message, you can just use the command in #3 to update the message alone.

Only use --amend while working locally–you can mess up the history if you have already pushed your changes.

git revert

Using git revert will undo the indicated commit by creating a new commit exactly reversing those changes. It doesn’t go back to that point in the history or undo other commits along the way, it just does exactly the opposite of that one commit–> so very carefully consider if that is what you need in your situation!

Get the hash id for the commit from the GitHub commits page or git log --oneline and add it to the end of git revert <somehash>.

gitignore

Sometimes there are files that you do not want version controlled or sent to your remote repository.

For example, your code might be used to generate visualization images or a website. Those derivative outputs do not need to be version controlled, since they can be regenerated from the project code, so usually would be gitignored.

To tell Git what to ignore, create a .gitignore file in the repository.

Additionally, global ignores related to your operating system are generally added by the Git installer, and local ones are often automatically generated by project templates or your IDE (see some example gitignore templates).

For details, check this gitignore tutorial or the documentation.