How to Undo Git Add

  1. How to undo git add before commit
  2. How to undo git add with git amend
  3. The git commit Command
  4. The git reset Command

At any stage of your work, you may like to undo something. You have that opportunity with Git. Here, we will show you the basic techniques of undoing an unwanted add in Git.

How to undo git add before commit

One of the most common questions while working with Git is “How to undo git add before commit?”. If you also wonder how to undo git add before commit, then follow the commands below (these commands are used since Git 1.8.2 version):

In case you want to undo a single file, act like this:

git reset <file>

For undoing all your changes run the following:

git reset

Anyway, before following the above-mentioned command, note that it is an unrestorable operation.

As you see, git reset is used for undoing git. Running only git reset without specifying the file name is especially useful when you are unable to list all the existing files one by one.

In the previous Git versions, there were different commands to undo git add. For undoing one file use the following command:

git reset HEAD <file>

For undoing all files act as follows:

git reset HEAD

Anyway, before following the above-mentioned command, note that it is an unrestorable operation.

How to undo git add with git amend

Another common undo happens in case you commit too early forgetting to add several files. Whenever it happens you can make additional changes. To make the changes, stage and commit them, you need to use the amend command:

git commit --amend

In another case, when you commit but then remember that you have not staged the changes, you may act like this:

git commit -m 'initial commit'
git add forgotten_file
git commit --amend

The git commit Command

The git commit command can be related to the most important git commands. It is used for saving overall currently staged changes. You can create commits for capturing the current position of your project. Git always asks before changing the committed snapshots, hence they can be considered safe versions of the project. The git add command is used before git commit for making changes to the project which is going to be stocked in a commit.

The git reset Command

Whenever you need to undo changes, the git reset command will come to help you. It runs on the so-called “ The Three Trees of Git”. It involves the commit history (HEAD), the working directory and the staging index. This tool is useful when you have not pushed your commit to the remote repository yet.


Do you find this helpful?

Related articles