Read Anthropic’s case study about Graphite Reviewer

How to undo the command "git rm"

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


The git rm command is used to remove files from your working directory and staging area in Git. However, if you accidentally remove the wrong file or decide you need a file back after using git rm, there are several ways to undo this action. This guide will explore how to reverse a git rm operation, whether you've removed the file just from the staging area or also from your working directory.

The git rm command can be used in several ways:

  • git rm <file>: Removes the file from both the working directory and the staging area.
  • git rm --cached <file>: Removes the file only from the staging area but leaves it in the working directory.

Scenario 1: File removed from both working directory and staging area

If you've removed a file from both your working directory and the staging area, and haven't committed the change yet, you can restore the file using the following steps:

  1. Check the status of your repository:

    Terminal
    git status

    This will confirm that the file has been deleted.

  2. Restore the deleted file: Use the git checkout command to restore the deleted file from the most recent commit (HEAD):

    Terminal
    git checkout HEAD -- <file>

Scenario 2: File removed only from the staging area (--cached)

If you used git rm --cached and the file is still in your working directory but not in the staging area:

  1. Re-add the file to the staging area:
    Terminal
    git add <file>

If you have committed the changes:

If you accidentally committed the deletion, you have a few options depending on your situation:

  1. Revert the commit: You can revert the commit where the deletion took place. This will create a new commit that undoes the changes made:

    Terminal
    git revert <commit_hash>
  2. Reset to a previous commit: If it's a recent commit and you haven't pushed to a remote repository, you can reset to the commit before the deletion:

    Terminal
    git reset --hard <commit_before_deletion>

    Caution: git reset --hard will discard all changes in the working directory and index since the commit you reset to. Make sure this is what you want before using it.

Using reflog to recover lost commits:

If you've lost track of where the file was last intact due to various operations, git reflog can help you find the lost commit:

  1. Check your reflog:

    Terminal
    git reflog

    Look for an entry before the deletion occurred.

  2. Reset to the appropriate entry: Once you find the correct entry:

    Terminal
    git reset --hard <entry_id>

For further reading on git rm see the official Git documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2