Undo Git mistakes

Recover a deleted branch or lost commit

Find a commit you thought was gone with git reflog, then bring it back on a branch.

Fill these in once
  1. List where your branches have been
    Terminal
    git reflog

    Each line starts with a short commit ID. Find the line from just before things went wrong. Press q to leave the list.

  2. Bring that commit back on a new branch
    Terminal
    git switch -c recovered-work a1b2c3d

    This is the safe way: it makes a new branch and leaves everything else alone.

Check it worked

  1. Check your work is back
    Terminal
    git log --oneline -5

    The commit you wanted should be at the top.

  1. List where your branches have been
    Terminal · Ubuntu / Debian
    git reflog

    Each line starts with a short commit ID. Find the line from just before things went wrong. Press q to leave the list.

  2. Bring that commit back on a new branch
    Terminal · Ubuntu / Debian
    git switch -c recovered-work a1b2c3d

    This is the safe way: it makes a new branch and leaves everything else alone.

Check it worked

  1. Check your work is back
    Terminal · Ubuntu / Debian
    git log --oneline -5

    The commit you wanted should be at the top.

  1. List where your branches have been
    PowerShell
    git reflog

    Each line starts with a short commit ID. Find the line from just before things went wrong. Press q to leave the list.

  2. Bring that commit back on a new branch
    PowerShell
    git switch -c recovered-work a1b2c3d

    This is the safe way: it makes a new branch and leaves everything else alone.

Check it worked

  1. Check your work is back
    PowerShell
    git log --oneline -5

    The commit you wanted should be at the top.

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