Read Anthropic’s case study about Graphite Reviewer

How to add deleted files in git

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


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.

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).

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2

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.

Graphite's CLI simplifies Git workflows, including managing deleted files. With the gt restore command, you can quickly recover deleted files directly in your working directory. Here's how you can use the Graphite CLI for this process:

If you delete a file but haven't committed the deletion yet, you can restore it using:

Terminal
gt restore <file-name>

This restores the file to its last committed state, just like git restore.

If you've already committed the deletion but need the file back, use:

Terminal
gt checkout HEAD^ <file-name>

Alternatively, for stack-specific workflows, Graphite CLI allows you to recover files while keeping your stack structure intact:

Terminal
gt restore <file-name>

This works seamlessly in scenarios where multiple pull requests are stacked, ensuring the recovered file doesn't disrupt your stack hierarchy.

  • Integrated recovery: The gt restore command is part of the CLI's larger suite, ensuring smooth recovery even in stacked or complex branch setups.
  • Simplified workflows: Designed to minimize Git's complexities, the CLI eliminates manual overhead in restoring files.

For more details about managing and restoring files in your project, explore Graphite CLI's documentation.

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

Built for the world's fastest engineering teams, now available for everyone