Undo Git mistakes

Add a forgotten file to the last commit

Forgot a file in your last commit? Add it and fold it into that commit with git commit --amend.

Fill these in once
  1. Add the forgotten file
    Terminal
    git add src/app.js
  2. Put it into the last commit
    Terminal
    git commit --amend --no-edit

    This keeps the same commit message. Leave out --no-edit if you also want to change the message.

  3. If you already pushed the commit
    Terminal
    git push --force-with-lease

    Only do this on your own branch. It replaces the commit on GitHub and anyone who pulled the old one will need to pull again.

Check it worked

  1. Check the file is in the last commit
    Terminal
    git show --stat HEAD

    The file should be in the list of changed files. Press q to leave.

Undo it
  1. Go back to the commit before you added the file
    Terminal
    git reset --soft 'HEAD@{1}'

    The file stays added, ready for its own commit.

  1. Add the forgotten file
    Terminal · Ubuntu / Debian
    git add src/app.js
  2. Put it into the last commit
    Terminal · Ubuntu / Debian
    git commit --amend --no-edit

    This keeps the same commit message. Leave out --no-edit if you also want to change the message.

  3. If you already pushed the commit
    Terminal · Ubuntu / Debian
    git push --force-with-lease

    Only do this on your own branch. It replaces the commit on GitHub and anyone who pulled the old one will need to pull again.

Check it worked

  1. Check the file is in the last commit
    Terminal · Ubuntu / Debian
    git show --stat HEAD

    The file should be in the list of changed files. Press q to leave.

Undo it
  1. Go back to the commit before you added the file
    Terminal · Ubuntu / Debian
    git reset --soft 'HEAD@{1}'

    The file stays added, ready for its own commit.

  1. Add the forgotten file
    PowerShell
    git add src/app.js
  2. Put it into the last commit
    PowerShell
    git commit --amend --no-edit

    This keeps the same commit message. Leave out --no-edit if you also want to change the message.

  3. If you already pushed the commit
    PowerShell
    git push --force-with-lease

    Only do this on your own branch. It replaces the commit on GitHub and anyone who pulled the old one will need to pull again.

Check it worked

  1. Check the file is in the last commit
    PowerShell
    git show --stat HEAD

    The file should be in the list of changed files. Press q to leave.

Undo it
  1. Go back to the commit before you added the file
    PowerShell
    git reset --soft 'HEAD@{1}'

    The file stays added, ready for its own commit.

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