Home > Article > Development Tools > How to undo add in git
To undo git add, you can use the following method: git reset HEAD
: remove the file from the staging area and restore the state before modification. git rm --cached : Delete the file from the staging area but keep it in the working directory. git restore : Delete files from the staging area and working directory simultaneously.
Undo git add
When using git, the add
command adds files to Staging area, ready for submission. However, if you add a file by mistake or change your mind, you can undo add
using:
Use <code>git reset HEAD <filename></code>
This is one of the easiest ways to undo add
. This command removes the file from the staging area and restores its status to before modification:
<code>git reset HEAD <filename></code>
For example:
<code>git reset HEAD readme.txt</code>
Use <code>git rm --cached <filename></code>
This command deletes files from the staging area but does not delete files from the working directory. This means you can continue editing the file, but it will not be included in the next commit:
<code>git rm --cached <filename></code>
For example:
<code>git rm --cached readme.txt</code>
Use <code>git restore <filename></code>
This command deletes files from both the staging area and the working directory. This is equivalent to using git reset HEAD
and then using git checkout -- <filename>
:
<code>git restore <filename></code>
For example:
<code>git restore readme.txt</code>
Note:
git revert
command to undo them. add
will not affect submitted files. The above is the detailed content of How to undo add in git. For more information, please follow other related articles on the PHP Chinese website!