The short answer
You can undo a prior git add operation with either of these commands:
$ git reset <file/path>
# or
$ git restore --staged <file/path>
Run in Warp
But let’s walk through the full flow…
Starting from git adding a file (or files)
$ git add <file/path>
Run in Warp
The command above adds files from the working directory to the staging area to get them ready to be committed.
- git add . to add all the changed files
- git add <file> to add a specific file
- git add <path> to add all the changes under the path
After running the above command you can run git status and verify what has been added to the staging area and what is still on the working directory.
Your options for undoing the addition
There are two popular ways to unstage changes done by a git add. Some developers tend to use the git reset command due to its powerful and versatile nature. However, I find the git restore --staged option preferable because the naming of the command offers clarity and is a recommended command whenever I type git status.
But because both commands ultimately achieve the same result, the choice between using one or the other depends on the individual developer's preference.
Undoing with git reset
$ git reset <file/path>
Run in Warp
The command above unstages the changes made in the staging area. It moves the HEAD and branch pointers to a previous commit thus, undoing adding your changes. This is a powerful command and is used typically to undo git commits. We’ve written a blog post that goes more in-depth on undoing git commits.
- git reset README.md to put the changes done on the README.md file back to the working directory
- git reset *.md to remove all the files ending in .md from the staging area
- git reset to unstage all changes
I would highly recommend using Warp's AI Command Search to access these commands in the future. It's a fantastic way to reduce context switching and stay within the terminal when debugging.
In the gif below, I pressed the # key to trigger the A.I. Command Search and typed what command I was looking for. Warp recommended git reset which is used to reverse the effects of a git add.
Undoing with git restore
$ git restore --staged <file/path>
Run in Warp
The command above is used mainly to revert changes in a workflow. The --staged option will 'restore' your files from staging back to the working directory.
- git restore —staged README.md to undo the git add of the README.md file
- git restore —staged *.md to remove all files ending in .md from the staging area
- git restore —staged . to unstage all the changes
Similarly, if you wanted to put the files back in the working directory to their original state, you can use the command without the flag: git restore. Please be sure that you want to undo these changes as this is not reversible.
Written by
Glory Kim
Software Engineer, Loom
Filed Under
Related Articles
Undo A Git Pull
How to effectively remove the commits introduced by a pull in Git using git-reset and preserve your local changes using git-stash. Also, how to cancel an unmerged pull request on GitHub.
Undo a Git Merge
How to rollback the changes introduced by a merge in Git by adding new opposite commits using git-revert and effectively removing commits using git-reset.
Prompt Show Git Branch In Prompt
Enhance your terminal with a custom Git prompt. Learn different ways to integrate this contextual info, from custom shell functions to Warp context chips and toolkits like Starship and P10K.
How To Remove Secrets From The Git History Remove Secrets From The Git History
Learn how to remove secrets from the Git history using the BFG and git-filter-repo command-line tools.
Adding a Submodule in Git
This post will show you how to simply add a submodule to a local repository, clone a repository with a submodule, and work within a repository that has a submodule.
Undo a git push
This post will show you had to simply undo a git push three different ways.
Undo a Git Rebase
This post will show you how to undo a rebase using git reset, git rebase and git revert
Git Push Origin
A breakdown of git push origin
Create Folder In GitHub Repository
Learn how to create and push one or more empty directories in a Git repository using `.placeholder` and `README.md` files using both the CLI and the GitHub interface.
Git Push Tags
This post will show you how to push a single tag, multiple tags, all tags, and tags with commits.
Undoing Git Commits
Explore ways to undo a commit, including git reset, git checkout, and git revert with git while preserving commit history.
Delete Local Git Branch
Learn how to delete local branches from your git repository, including ones with unmerged changes, as well as local remote-tracking branches.