Javaexercise.com

How to Undo a Git Add before a Commit?

Git contains a staging area or an index where we can add files. The files in the staging area will be part of the next commit.

The git add command adds or stages files on the staging area. Next, we use the git commit command to commit the changes added to the staged files.

In some cases, we may end up staging the wrong files, Or we may not want some of the files in the staging area to be part of the next commit.

Let's learn how to undo the effects of a git add before a commit. Or, in other words, how to remove some files from the staging area(unstage).

Undo git add before commit using git reset command

The git reset command is used to undo changes. We can use this command to unstage or remove files from our index. We just need to pass the file name with the git reset command to unstage that file.

git reset <file-name>

This command can also unstage all the files. It will clear the entire staging area or index if we don't mention a file name.

git reset

The Git Reset commands works in three modes. The three modes are soft, mixed, and hard. We are using the --mixed option to undo a Git Add. It is also the default mode.

git reset --mixed <file-name>   
git reset <file-name>   # equivalent to the above command

Undo git add before commit using git restore command

Version 2.23 of Git brings a new command called Git Restore. The git restore with --staged option can undo a git add command effects.

git restore --staged <file-name>

Note that we cannot use the git restore command if we don't have any commits in our repo. The git restore command will not undo a git add operation if there are no commits in our repository.

For example, consider that we just initialized a new Git repository(using git Init). We created a new file demo.txt and added it to the staging area. Now, if we try to unstage it using git restore command, we will get the following error.

git init                        # initializing or creating a new Git Repository
touch demo.txt                  # creating the demo.txt file 
git add demo.txt                # staging demo.txt
git restore --staged demo.txt    # trying to undo the Git Add command
fatal: could not resolve HEAD

The next section discusses this case.

Undo git add before commit using git rm command(Only for repositories with no commits)

The git rm command removes or deletes files from the repository. In addition, it will stage the removal operation. But in some cases, we can use this command to undo the Git Add operation.

Consider that we have no commits in our repository and we create a new file and add it to the staging area using Git Add. Now, to unstage this file, we need to use the Git Rm command. We need to use the --cached option with the command.

git rm --cached <file-name>

It is not recommended to use Git Rm --cached command in any other case to undo the Git Add operation.

Conclusion

The Git Add command adds files or changes to the staging area. The staging area is used to sort and segregate the files we want to add to the next commit. There are two commands for undoing a Git Add operation. We can either use the Git Reset command or the Git Restore command. Git Restore will only work on Git version 2.23 and later. Use the git --version command to check your Git version before using the restore command. The Git Rm --cached command is used when there are no commits in our repository.

To summarize:

  • Use git restore --staged file to undo the Git Add operation if using version 2.23 or above.

  • Otherwise, use git reset file to undo the Git Add operation.

  • Use git rm --cached file to undo the Git Add operation if the repo does not contain any commits. Git Reset will also work in this case, if you are using version 1.8.2 or above.