The two ways to delete a file from GitHub
You can delete a file from GitHub in two ways: through the web interface on GitHub.com itself, or through your local computer using Git commands. The web interface is faster if you only need to remove one or two files and do not need to delete them from your own computer. Using Git commands on your computer gives you more control and lets you delete multiple files at once, then review the changes before pushing them back to GitHub.
Both methods accomplish the same thing: the file disappears from the repository history going forward, though GitHub keeps a record that it existed. This matters if you accidentally delete something — you can recover it by looking at an earlier version of the repository.
Key Takeaways
- Deleting through GitHub.com takes three clicks and works when ready, but only removes the file from the repository, not from your local computer.
- Deleting through Git commands on your computer lets you delete multiple files, review changes, and keep your local copy in sync with GitHub.
- After deletion, the file is gone from the current version but still visible in the repository history if you need to recover it.
- If you delete a file by accident, you can restore it by checking out an earlier commit or by undoing the deletion before you push to GitHub.
Deleting a file through the GitHub website
Open GitHub.com, navigate to the repository, and find the file you want to delete. Click on the file name to open it. At the top right of the file view, you will see a trash icon or a menu button (three dots). Click the trash icon, or click the menu and select "Delete this file".
GitHub will ask you to confirm the deletion and write a commit message — a short note explaining why you deleted it. You can leave the default message or write your own. Click "Commit changes" and the file is gone from the repository. This method works best when you are deleting one or two files and do not need to make other changes at the same time.
Deleting files using Git on your computer
If you have the repository cloned to your computer, open your terminal or command prompt and navigate to the repository folder. Use the command git rm filename.txt (replace "filename.txt" with the actual file name). This removes the file from your computer and stages it for deletion in Git.
To delete multiple files at once, you can use git rm file1.txt file2.txt file3.txt or use a pattern like git rm *.log to delete all files ending in .log. After you run git rm, the deletions are staged but not yet committed. You can see what you are about to delete by running git status, which shows all staged changes in red.
Once you are sure the right files are marked for deletion, commit the changes with git commit -m "Remove old log files" (or whatever message describes why you are deleting them). Then push the changes to GitHub with git push. The files are now deleted from both your computer and the repository.
Deleting a file without deleting your local copy
Sometimes you want to remove a file from GitHub but keep it on your own computer — for example, if you accidentally committed a configuration file with passwords in it. Use git rm --cached filename.txt instead of the regular git rm command. This removes the file from GitHub but leaves it on your computer.
After running git rm --cached, commit and push as usual. The file will no longer be tracked by Git, so future changes to it will not be pushed to GitHub. This is useful for files you want to keep private or files that should not be in the repository.
Recovering a deleted file
If you delete a file by mistake, you can recover it as long as you have not yet pushed the deletion to GitHub. Run git reset HEAD filename.txt to unstage the deletion, then git checkout filename.txt to restore the file to your computer. After that, you can push normally and the file stays in the repository.
If you have already pushed the deletion to GitHub, you can still recover the file by checking out an earlier version. Run git log --oneline to see the commit history, find the commit before the file was deleted, and run git checkout [commit-hash] -- filename.txt (replace [commit-hash] with the actual hash). This brings the file back. Then commit and push the recovery.
What happens to deleted files in GitHub history
When you delete a file and push to GitHub, the file disappears from the current version of the repository. However, it remains visible in the repository history. Anyone with access to the repository can see that the file existed by looking at older commits. This is by design — version control systems keep a complete record of changes so you can always go back.
If the file contained sensitive information like passwords or API keys, deleting it from the current version is not enough. You would need to use a tool like git filter-branch or BFG Repo-Cleaner to remove it from the entire history. This is more complex and should only be done if the file truly contained secrets. For most files, a straightforward deletion is sufficient.
Frequently Asked Questions
Does deleting a file on GitHub delete it from my computer?
No. If you delete through the GitHub website, the file stays on your computer. If you use git rm on your computer, the file is deleted locally and from GitHub. Use git rm --cached if you want to remove it from GitHub but keep it on your computer.
Can I delete a file if I do not have write permission to the repository?
No. You need write access to the repository to delete files. If you do not own the repository, you would need to ask the owner to add you as a collaborator, or you could submit a pull request with the deletion and let the owner review it.
What if I delete a file by accident before pushing to GitHub?
Run git reset HEAD filename.txt to unstage the deletion, then git checkout filename.txt to restore it. The file comes back to your computer and you can push normally without the deletion.
Can I delete a folder instead of a single file?
Yes. Use git rm -r foldername to delete a folder and all files inside it. The -r flag tells Git to delete recursively. You can also delete a folder through the GitHub website by navigating into it and deleting the files one by one, though this is slower.
Will other people see that I deleted a file?
Yes. The deletion shows up in the commit history and anyone with access to the repository can see it. If you want to hide the deletion, you would need to rewrite the repository history, which is not recommended unless the file contained secrets.