The quickest way to delete a file from GitHub

You can delete a file directly from GitHub's web interface without touching your computer. Go to the file you want to remove, click the three dots menu in the top right corner of the file view, and select Delete this file. GitHub will create a new commit with the deletion, and you'll write a commit message explaining why. Hit Commit changes and it's gone.

If you're working on a branch rather than the main branch, the deletion happens on that branch first. You'll then need to merge the branch back into main through a pull request, the same way you'd merge any other change.

This method works for any file type — code, images, documents, anything. The file stays in your repository's history (you can recover it if needed), but it's no longer in the current version.

Key Takeaways

  • The web interface delete button is the fastest route: open the file, click the three-dot menu, and select Delete this file.
  • Deleting through the web interface creates a commit automatically, so you don't need to use the command line.
  • If you're deleting from a branch, you'll merge that branch back to main through a pull request like any other change.
  • Deleted files remain in your repository history and can be recovered by viewing an earlier commit or using git revert.

Deleting a file from the command line

If you work on your computer with Git, you can delete a file locally and then push the change to GitHub. Use git rm filename.txt to remove the file and stage the deletion in one step. Then commit with git commit -m "Remove filename.txt" and push with git push.

The git rm command removes the file from your working directory and tells Git to track that removal. If you just delete the file manually from your folder, Git sees it as a change but doesn't automatically stage it — you'd have to run git add afterward. Using git rm is cleaner because it does both at once.

If you want to delete the file from GitHub but keep it on your computer (useful for local configuration files or secrets you don't want tracked), use git rm --cached filename.txt instead. This removes it from the repository without deleting your local copy.

Deleting multiple files at once

If you need to remove several files, the command line is faster than clicking through the web interface one file at a time. Use git rm file1.txt file2.txt file3.txt to remove multiple files in a single command, then commit and push as usual.

You can also use patterns to delete groups of files. For example, git rm *.log removes all files ending in .log, or git rm folder/* removes everything inside a specific folder. Be careful with patterns — double-check what you're about to delete before you commit.

Recovering a deleted file

Deleted files don't disappear forever. Every commit in your repository's history is stored, including the version before the deletion. To see what a file looked like before it was deleted, go to your repository on GitHub, click the Commits tab, and scroll back to find the commit before the deletion. Click that commit and you can view the file as it was then.

If you want to restore the file completely, you have two options. The simplest is to use git revert with the commit hash of the deletion: git revert [commit-hash]. This creates a new commit that undoes the deletion, bringing the file back. Alternatively, use git checkout [commit-hash] -- filename.txt to restore just that file from a specific point in history, then commit the restored version.

Deleting folders and their contents

Deleting a folder works the same way as deleting a file. In the web interface, navigate into the folder, click the three-dot menu next to any file inside it, and look for the option to delete the folder. On the command line, use git rm -r foldername to remove the folder and everything inside it recursively.

The -r flag tells Git to delete the folder and all its contents in one step. Without it, Git will refuse to delete a folder that contains files. After running git rm -r, commit and push the change as you would for a single file.

When to use branches before deleting

If you're unsure about a deletion or working with others, make the change on a separate branch first. Create a new branch with git checkout -b remove-old-files, delete what you need to delete, commit, and push. Then open a pull request so your team can review the deletion before it goes into the main branch.

This approach is especially useful in shared repositories where multiple people work on the same code. A pull request gives others a chance to object or suggest keeping something you were about to remove. Once the pull request is approved and merged, the deletion becomes permanent in the main branch.

Frequently Asked Questions

Can I undo a deletion after I've pushed it to GitHub?

Yes. Use git revert with the commit hash of the deletion to create a new commit that restores the file. You can find the commit hash in your repository's commit history on GitHub. This is safer than trying to force-push, especially in shared repositories.

What's the difference between deleting and archiving a repository?

Deleting a file removes it from your current code. Archiving a repository makes the entire project read-only but keeps it visible in your GitHub account. If you want to remove a whole project, archive it first unless you're certain you'll never need it again.

Does deleting a file free up storage space on GitHub?

Not immediately. The file remains in your repository's history, so it still counts toward your storage limit. If you need to permanently remove a file from history (for example, if you accidentally committed a large binary file), you'll need to use git filter-branch or the BFG Repo-Cleaner tool, which are more advanced operations.

Can I delete a file if I don't have write access to the repository?

No. You need write permissions on the repository or the branch to delete files. If you don't have access, you can fork the repository, make the deletion in your fork, and then open a pull request asking the owner to merge your changes.