Undo Git Add

Glory Kim
Glory KimSoftware Engineer, Loom
Published: January 31, 2024

The short answer

You can undo a prior git add operation with either of these commands:

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

But let’s walk through the full flow…

Starting from git adding a file (or files)

Bash
$ git add <file/path>

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

Bash
$ 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.

  • 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

Bash
$ git restore --staged <file/path>

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
Glory KimSoftware Engineer, Loom
Filed under

Related articles


Git Clone, Push, And Pull Over SSH

Learn how to set up an SSH key to clone, push, and pull a Git repository over the SSH protocol.

Undo a Git Rebase

This post will show you how to undo a rebase using git reset, git rebase and git revert

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.

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.

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.

Git Push Origin

A breakdown of git push origin

Undo a git push

This post will show you had to simply undo a git push three different ways.

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.

Git Commit History

Different ways to use Git to understand how to navigate the commit history of a repository or branch

Change Git Origin Remote URL

Learn how to change the remote url of a local git-enabled directory using the git-remote command and Warp's workflow feature.

Amend a Git Commit

Making changes to a previous commit

How To Create a Git Repository

Creating repos in various scenarios with git