How to Remove Changes From Staging Area in Git

Azhar Bashir Khan Feb 02, 2024
How to Remove Changes From Staging Area in Git

This tutorial is about removing changes from the staging area in Git.

Sometimes, we may wish to remove the files or remove the changes of the files from the staging area. We can achieve this using the git restore command.

Use the git restore Command to Remove Changes From Staging Area in Git

When we are finished with the changes to files or when we want to add new files to the project directory, tracked in a Git repository, we add those to the repository.

We use the git add command to add the files or files’ changes to the staging area or index of the repository in Git. We can then use the git commit command to create a commit and commit the changes to the Git repository.

Sometimes, before committing the changes, we may find that the new changes added to the staging area are no longer valid. We wish to remove those changes from the staging area instead of committing those changes.

For example, we have a file named README.md in our project directory in the Git repository. We have now done some changes to the file README.md.

We can run the git status command to check the status of the repository as follows.

$ git status .
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md

We can see above that the git status command shows that the file README.md is modified.

We can now add the README.md file modifications to the staging area. We can do so using the git add command as follows.

$ git add .

We can again check the status of the repository as follows.

$ git status .
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   README.md

We can now see that the modifications of the file README.md are now in the staging area or the index. The file status is now in Changes to be committed.

We can now decide to un-stage the changes (i.e.) remove the modifications from the staging area using the git restore command with the --staged option. This information is shown above in the git status command.

Thus, we now run the git restore command as follows.

$ git restore --staged README.md

We can now run the git status command again as follows.

$ git status .
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md

We can see above that the modifications of the file README.md are no longer in the staging area (i.e.); changes are not staged for commit.

Thus, we have learned how to remove changes from the staging area in Git.

For more information, please visit these links.

  1. git restore
  2. Undo staged local changes

Related Article - Git Restore