How do I undo ‘git add’ before commit?
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Undo Git Add using restore
The easiest way to undo your git add command is to use the “git restore” command with the “–staged” option and specify the file you want to unadd.
As an example, let’s say that you are on a branch named “feature” and that you have recently added a new file to your staging area.
In order to bring back the file to the working directory, we can execute the “git restore” command with the “–staged” option.
you have successfully undone your “git add” command using the restore one!
Now you can modify your file, add it again and commit it if you want.
You can undo
git add
before commit withwhich will remove it from the current index (the “about to be committed” list) without changing anything else. You can use
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 <file>
andgit reset HEAD
respectively, and will fail ifHEAD
is undefined (because you haven’t yet made any commits in your repository) or ambiguous (because you created a branch calledHEAD
, 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: