Terminus
Undo Git Add

Undo Git Add

The short answer

You can undo a prior [.inline-code]git add[.inline-code] operation with either of these commands:

$ git reset <file/path> 
# or
$ git restore --staged <file/path> 

But let’s walk through the full flow…

[#git-add]Starting from git adding a file (or files) [#git-add]

$ git add <file/path> 

The command above adds files from the working directory to the staging area to get them ready to be committed.

  • [.inline-code]git add . [.inline-code]   to add all the changed files
  • [.inline-code]git add <file>[.inline-code]  to add a specific file
  • [.inline-code]git add <path>[.inline-code]  to add all the changes under the path

After running the above command you can run [.inline-code]git status[.inline-code] 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 [.inline-code]git reset[.inline-code] command due to its powerful and versatile nature. However, I find the [.inline-code]git restore --staged[.inline-code] option preferable because the naming of the command offers clarity and is a recommended command whenever I type [.inline-code]git status[.inline-code].

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]Undoing with git reset[#undoing-with-git-reset]

$ git reset <file/path> 

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

  • [.inline-code]git reset README.md[.inline-code] to put the changes done on the README.md file back to the working directory
  • [.inline-code]git reset *.md[.inline-code]  to remove all the files ending in .md from the staging area
  • [.inline-code]git reset [.inline-code] 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 [.inline-code]#[.inline-code] key to trigger the A.I. Command Search and typed what command I was looking for. Warp recommended [.inline-code]git reset[.inline-code] which is used to reverse the effects of a git add. 

[#undoing-with-git-restore]Undoing with git restore[#undoing-with-git-restore]

$ git restore --staged <file/path> 

The command above is used mainly to revert changes in a workflow. The [.inline-code]--staged[.inline-code] option will 'restore' your files from staging back to the working directory. 

  • [.inline-code]git restore —staged README.md[.inline-code]  to undo the git add of the [.inline-code]README.md[.inline-code] file
  • [.inline-code]git restore —staged *.md[.inline-code]  to remove all files ending in [.inline-code].md[.inline-code] from the staging area
  • [.inline-code]git restore —staged . [.inline-code] 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: [.inline-code]git restore[.inline-code]. Please be sure that you want to undo these changes as this is not reversible.