Find when (and why) something changed

safe intermediate #blame#log#search#history

When to use this: A line, file or word changed and you want to know which commit did it.

Before you start

Steps

  1. History of one file

    terminal
    git log --oneline --follow -- src/pages/index.astro

    Lists the commits that touched that file. --follow keeps tracking it across renames.

  2. See the actual edits to that file

    terminal
    git log -p -- src/pages/index.astro

    Adds the diff of each commit. Scroll with space, quit with q.

  3. Which commit added or removed a piece of text?

    terminal
    git log -S"heroTitle" --oneline

    -S (the 'pickaxe') finds commits where the number of occurrences of that text changed.

  4. Who last changed each line?

    terminal
    git blame src/pages/index.astro

    Prefixes every line with the commit and author that last modified it. Then git show <id> to read the whole commit.

  5. What did I do in a time window?

    terminal
    git log --since="2 weeks ago" --author="Your Name" --oneline

    Handy for a weekly summary. Dates like yesterday or 2025-01-01 also work.

How to verify it worked

  • You have a commit ID, and git show <id> explains the change.

Q&A

How do I get back a file as it was in an old commit?

git restore --source=<commit> -- path/to/file copies that version into your working tree. Then review with git diff and commit if you want to keep it.

Practice these as flashcards →

Terms in this scenario

Related scenarios

My notes

Saved in this browser only.