Git basics

Find a commit by its message or by code it changed

Search your Git history by commit message, by a piece of code or by file, then open the commit you need.

Fill these in once
  1. Find commits by words in their message
    Terminal
    git log --oneline -i --grep="login fix"

    Shows every commit whose message mentions those words, newest first. -i ignores capital letters.

  2. Find commits that added or removed some code
    Terminal
    git log --oneline -S "maxRetries"

    Shows the commits where that exact text appeared or disappeared in any file. Handy for "when did this line come in?"

  3. Find commits that changed one file
    Terminal
    git log --oneline -- src/app.js
  4. See who last changed each line of a file
    Terminal
    git blame src/app.js

    Each line shows the commit, the author and the date. Press q to leave.

  5. Look at one commit in full
    Terminal
    git show a1b2c3d

    Copy the short ID from any of the lists above.

Check it worked

  1. Search the code as it is now
    Terminal
    git grep -n "maxRetries"

    Shows where that text appears in the current files, with line numbers.

Undo it
  1. Nothing to undo
    Terminal
    git status

    Searching only reads the history. It never changes your files or commits.

  1. Find commits by words in their message
    Terminal · Ubuntu / Debian
    git log --oneline -i --grep="login fix"

    Shows every commit whose message mentions those words, newest first. -i ignores capital letters.

  2. Find commits that added or removed some code
    Terminal · Ubuntu / Debian
    git log --oneline -S "maxRetries"

    Shows the commits where that exact text appeared or disappeared in any file. Handy for "when did this line come in?"

  3. Find commits that changed one file
    Terminal · Ubuntu / Debian
    git log --oneline -- src/app.js
  4. See who last changed each line of a file
    Terminal · Ubuntu / Debian
    git blame src/app.js

    Each line shows the commit, the author and the date. Press q to leave.

  5. Look at one commit in full
    Terminal · Ubuntu / Debian
    git show a1b2c3d

    Copy the short ID from any of the lists above.

Check it worked

  1. Search the code as it is now
    Terminal · Ubuntu / Debian
    git grep -n "maxRetries"

    Shows where that text appears in the current files, with line numbers.

Undo it
  1. Nothing to undo
    Terminal · Ubuntu / Debian
    git status

    Searching only reads the history. It never changes your files or commits.

  1. Find commits by words in their message
    PowerShell
    git log --oneline -i --grep="login fix"

    Shows every commit whose message mentions those words, newest first. -i ignores capital letters.

  2. Find commits that added or removed some code
    PowerShell
    git log --oneline -S "maxRetries"

    Shows the commits where that exact text appeared or disappeared in any file. Handy for "when did this line come in?"

  3. Find commits that changed one file
    PowerShell
    git log --oneline -- src/app.js
  4. See who last changed each line of a file
    PowerShell
    git blame src/app.js

    Each line shows the commit, the author and the date. Press q to leave.

  5. Look at one commit in full
    PowerShell
    git show a1b2c3d

    Copy the short ID from any of the lists above.

Check it worked

  1. Search the code as it is now
    PowerShell
    git grep -n "maxRetries"

    Shows where that text appears in the current files, with line numbers.

Undo it
  1. Nothing to undo
    PowerShell
    git status

    Searching only reads the history. It never changes your files or commits.

New to Commit? I explain it in plain English on Layman Terms.