Skip to content

Instantly share code, notes, and snippets.

@windsting
Last active July 15, 2017 06:25
Show Gist options
  • Save windsting/0088d36d29c03eb4042c32ac857229a7 to your computer and use it in GitHub Desktop.
Save windsting/0088d36d29c03eb4042c32ac857229a7 to your computer and use it in GitHub Desktop.
Undo Add Files in Git
$ git commit -m "Something terribly misguided"              #(1)
$ git reset HEAD~                                           #(2)
#< edit files as necessary >>                               #(3)
$ git add ...                                               #(4)
$ git commit -c ORIG_HEAD                                   #(5)
  1. This is what you want to undo
  2. This leaves your working tree (the state of your files on disk) unchanged but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in git status and you'll need to add them again before committing). If you only want to add more changes to the previous commit, or change the commit message1, you could use git reset --soft HEAD~ instead, which is like git reset HEAD~ but leaves your existing changes staged.
  3. Make corrections to working tree files.
  4. git add anything that you want to include in your new commit.
  5. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

You can undo git add before commit with

git reset <file>

which will remove it from the current index (the "about to be committed" list) without changing anything else.

You can use

git reset

without any file name to unstage all due changes. This can come in handy when there are too many files to be listed one by one in a reasonable amount of time.

In old versions of Git, the above commands are equivalent to git reset HEAD and git reset HEAD respectively, and will fail if HEAD is undefined (because you haven't yet made any commits in your repo) or ambiguous (because you created a branch called HEAD, which is a stupid thing that you shouldn't do). This was changed in Git 1.8.2, though, so in modern versions of Git you can use the commands above even prior to making your first commit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment