Undo Git mistakes

Get one file back from an older commit

Bring back an older version of a single file with git restore, without undoing anything else in your project.

Fill these in once
  1. Find the commits that changed the file
    Terminal
    git log --oneline -- src/app.js

    Each line is one commit that touched this file, newest first. Copy the short ID of the version you want.

  2. Look at the file as it was in that commit
    Terminal
    git show a1b2c3d:src/app.js

    Only shows it. Nothing changes yet. Press q to leave.

  3. Bring that version back
    Terminal
    git restore --source a1b2c3d -- src/app.js

    Only this one file changes. Everything else stays as it is now.

  4. Commit the old version
    Terminal
    git commit -am "Restore src/app.js from a1b2c3d"

Check it worked

  1. Check what changed in the file
    Terminal
    git diff HEAD~1 -- src/app.js
Undo it
  1. Before you commit: go back to the latest version
    Terminal
    git restore --source HEAD -- src/app.js

    After you commit, undo the last commit instead.

  1. Find the commits that changed the file
    Terminal · Ubuntu / Debian
    git log --oneline -- src/app.js

    Each line is one commit that touched this file, newest first. Copy the short ID of the version you want.

  2. Look at the file as it was in that commit
    Terminal · Ubuntu / Debian
    git show a1b2c3d:src/app.js

    Only shows it. Nothing changes yet. Press q to leave.

  3. Bring that version back
    Terminal · Ubuntu / Debian
    git restore --source a1b2c3d -- src/app.js

    Only this one file changes. Everything else stays as it is now.

  4. Commit the old version
    Terminal · Ubuntu / Debian
    git commit -am "Restore src/app.js from a1b2c3d"

Check it worked

  1. Check what changed in the file
    Terminal · Ubuntu / Debian
    git diff HEAD~1 -- src/app.js
Undo it
  1. Before you commit: go back to the latest version
    Terminal · Ubuntu / Debian
    git restore --source HEAD -- src/app.js

    After you commit, undo the last commit instead.

  1. Find the commits that changed the file
    PowerShell
    git log --oneline -- src/app.js

    Each line is one commit that touched this file, newest first. Copy the short ID of the version you want.

  2. Look at the file as it was in that commit
    PowerShell
    git show a1b2c3d:src/app.js

    Only shows it. Nothing changes yet. Press q to leave.

  3. Bring that version back
    PowerShell
    git restore --source a1b2c3d -- src/app.js

    Only this one file changes. Everything else stays as it is now.

  4. Commit the old version
    PowerShell
    git commit -am "Restore src/app.js from a1b2c3d"

Check it worked

  1. Check what changed in the file
    PowerShell
    git diff HEAD~1 -- src/app.js
Undo it
  1. Before you commit: go back to the latest version
    PowerShell
    git restore --source HEAD -- src/app.js

    After you commit, undo the last commit instead.