Data report"State of code review 2024" is now liveRead the full report

How to add deleted files in git

Greg Foster
Greg Foster
Graphite software engineer


Note

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


In Git, deleting a file from your working directory (the files stored locally on your machine), and committing that change to the remote repository (the files stored in the remote server) are two separate actions. Git tracks deletions as part of its version control process, which means that even when files are deleted, their history remains in the repository for potential recovery or analysis.

This guide will show you how to manage deleted files using Git, covering everything from committing deleted files, to reverting accidental deletions.

Stop wrestling with Git commands

The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop Googling Git commands.

Learn more

You can delete files manually using your operating system's file management commands, or directly through Git:

Terminal
git rm <file-name>

The git rm command deletes the file from your working directory and stages the deletion for your next commit. This is the recommended method if you are sure that you want to remove the file from the repository as it streamlines the process.

If you've manually deleted a file, you need to tell Git about this deletion:

Terminal
git add <deleted-file-name>

In Git, this command stages the deletion as if you used git rm. If you're not sure which files you have deleted, you can use:

Terminal
git add -u

This command stages all modifications, including deletions, for all tracked files in the current directory and subdirectories, but does not add new (untracked) files.

Once you have staged the deletions, commit them to your repository:

Terminal
git commit -m "Remove specified files"

This saves the changes to your repository, effectively recording the deletions in the project history.

If you decide that you need a file back after deleting it, but before committing the change, you can restore it using:

Terminal
git restore <deleted-file-name>

This will restore the file to your working directory, essentially undoing the file deletion.

If you have already committed the deletion, you can still bring the file back using:

Terminal
git checkout HEAD^ <deleted-file-name>

This command restores the deleted file to its state before the last commit, (ie. when you deleted it).

The best engineers use Graphite to simplify Git

Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.

Git started for free

If you have deleted multiple files and want to stage all these deletions at once, you can use:

Terminal
git add -A

This command stages all changes, including new files, modifications, and deletions, across your entire project, regardless of where you are in the repository.

If a commit was made that deleted files and you want to undo this action, use:

Terminal
git revert <commit-hash>

Replace <commit-hash> with the hash of the commit that deleted the files. This command creates a new commit that undoes the changes made by the previous commit, effectively restoring the deleted files.

For further reading on file deletions in Git, see the official Git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2