Deleting a file from GitHub removes it from your repository's history going forward
You can delete a file from GitHub in three ways: through the web interface (the easiest), through Git commands on your computer (the most common), or by deleting an entire folder. The method you choose depends on whether you're working alone, collaborating with others, and whether you want to keep a record that the file existed. Deleting through the web interface takes seconds and requires no command-line knowledge. Deleting through Git commands gives you more control and is what most developers do when working on their own machine.
Important: deleting a file from GitHub does not erase it from the repository's past. Anyone with access to the repository can still see the file in older versions. If you need to remove sensitive information (passwords, API keys, personal data), deleting the file is not enough — you need to rewrite the repository history or contact GitHub support.
Key Takeaways
- The web interface method works in your browser: open the file, click Delete, write a commit message, and confirm.
- The Git command method on your computer uses git rm filename followed by git commit and git push.
- Deleted files remain visible in the repository's history unless you rewrite that history with git filter-branch or BFG Repo-Cleaner.
- If you delete a file by mistake, you can recover it by checking out an older version or using git reflog to find the commit before deletion.
Deleting a file through GitHub's web interface
Open your repository on GitHub.com, navigate to the file you want to delete, and click the three-dot menu button in the top right corner of the file view. Select "Delete this file" from the menu. GitHub will open a delete confirmation screen showing you the file path and asking you to write a commit message.
Type a commit message describing why you're deleting the file — something like "Remove old config template" or "Delete unused stylesheet". This message becomes part of your repository history so collaborators understand what happened. Click "Commit changes" and the file is deleted. If you're working on a branch other than main, GitHub will ask whether to commit directly to that branch or create a new branch and open a pull request. For solo work, committing directly is fine; for team projects, a pull request lets others review the deletion first.
Deleting a file using Git commands on your computer
Open your terminal or command prompt, navigate to your repository folder, and run git rm filename (replace "filename" with the actual file path). This stages the deletion. If the file is in a subfolder, use the full path: git rm src/components/old-button.js. To delete multiple files at once, list them separated by spaces: git rm file1.js file2.js file3.js.
Next, commit the deletion with git commit -m "Remove old button component". The message should explain why you're deleting it. Finally, push the change to GitHub with git push. The file is now deleted from your main branch on GitHub. If you're on a different branch, push that branch instead: git push origin branch-name.
If you want to delete a file from your computer but keep it in the repository, use git rm --cached filename instead of git rm. This removes it from version control without deleting the actual file on your machine — useful when you've accidentally committed something you shouldn't have tracked.
Deleting an entire folder
To delete a folder and everything inside it, use git rm -r foldername. The -r flag means "recursive," telling Git to delete the folder and all files within it. Commit and push as usual: git commit -m "Remove old templates folder" followed by git push.
Through the web interface, navigate into the folder, delete each file individually using the three-dot menu, or delete the folder by going to the repository root, finding the folder in the file list, clicking the three dots next to it, and selecting delete. The web interface approach is slower for large folders, so command-line deletion is usually faster.
Recovering a deleted file
If you delete a file by mistake, you can recover it. The simplest method is to use git log --oneline to find the commit where the file still existed, then run git checkout commit-hash -- filename. This restores the file to your working directory. You can then commit this restoration as a new change.
If you've already pushed the deletion to GitHub, you can still recover the file the same way — Git keeps the history on GitHub too. If you deleted the file locally but haven't pushed yet, use git reflog to see all recent actions, find the commit before deletion, and check out the file from that commit.
Removing sensitive information from repository history
straightforward deleting a file does not remove it from the repository's past. If you committed a password, API key, or personal information, anyone who can access the repository can find it by looking at older commits. You need to rewrite the repository history to truly remove it.
The safest approach is to use BFG Repo-Cleaner, a tool designed for this purpose. read it, run bfg --delete-files filename on your repository, and then force-push the cleaned history back to GitHub. Alternatively, use git filter-branch, though it is more complex. After rewriting history, notify anyone who has cloned the repository that they need to re-clone it, because their local copies will be out of sync.
If the sensitive information is very recent and only a few people have access, you can also rotate the credentials when ready (change the password, regenerate the API key) and then delete the file normally. The old credentials in the history become useless.
Deleting files when working with collaborators
If you're working on a shared repository, delete files on a branch and open a pull request rather than deleting directly on main. This lets your teammates review the deletion and discuss whether it's necessary. They can see exactly what's being removed and why before the change is merged.
If a collaborator deletes a file on main and you want to keep it, you can restore it from an older commit on your own branch, then open a pull request to add it back. This creates a clear record of the disagreement. Alternatively, if the deletion was a mistake, ask the person who deleted it to revert the commit — this is cleaner than manually restoring the file.
Frequently Asked Questions
Can I undo a file deletion after I've pushed it to GitHub?
Yes. Use git log --oneline to find the commit before deletion, then run git checkout commit-hash -- filename to restore the file. Commit and push the restoration. The file reappears in your repository, and the deletion is now part of the history.
What's the difference between git rm and deleting the file manually?
git rm both deletes the file and stages the deletion for commit. If you delete the file manually (by dragging it to trash), Git sees it as a change but doesn't stage it automatically. You'd need to run git add filename or git add . to stage the deletion. Using git rm is faster and clearer.
If I delete a file, can someone else still see it in the repository?
Yes, if they check out an older commit or look at the commit history. The file remains in the repository's past. Only you and others with access to the full history can see it — the general public cannot if the repository is private. If you need to hide sensitive data, you must rewrite the history using BFG or git filter-branch.
Does deleting a file from GitHub delete it from my computer?
No. Deleting through the web interface only affects the repository on GitHub. If you delete using git rm on your computer and then push, the file is deleted from both your machine and GitHub. Use git rm --cached if you want to remove it from version control but keep the file on your computer.
What happens if I delete a file that other people are still working on?
If someone else has the file open in their local copy, they can still work on it. When they try to push their changes, Git will warn them that the file no longer exists on the main branch. They'll need to either delete their local copy to match the main branch or restore the file by checking out an older commit. Open a pull request for deletions so collaborators can discuss before the change is merged.