The three ways to remove a file from GitHub

You can delete a file from GitHub in three ways: through the web interface, using the command line with Git, or by deleting the entire folder that contains it. The method you choose depends on whether you want to keep the file on your computer, whether you need to delete it from your repository's history, and how comfortable you are with command-line tools.

The web interface is the fastest for a single file. The command line gives you more control and is necessary if you want to remove the file from your repository's past commits. Deleting a folder works the same way as deleting individual files — GitHub has no folder-level deletion, only file-level.

Key Takeaways

  • Deleting a file through GitHub's website removes it from the main branch immediately, but the file remains in your repository's history unless you use a special tool.
  • Using git rm from the command line deletes the file and stages the deletion, which you then commit and push to update GitHub.
  • If you want to erase a file from your entire repository history, you need git filter-branch or a tool like BFG Repo-Cleaner, not a simple delete.
  • Deleting a file does not delete the folder it was in — empty folders disappear from GitHub automatically, but your local copy keeps them until you manually remove them.

Deleting a file through the GitHub website

Open your repository on GitHub, navigate to the file you want to delete, and click the three-dot menu in the top right of the file view. Select "Delete this file". GitHub will ask you to confirm the deletion and create a commit message — you can use the default message or write your own. Click "Commit changes" and the file is removed from the main branch.

This method works well for single files and requires no setup. The deletion is immediate on GitHub, and anyone who pulls the repository will no longer see the file. However, the file remains in your repository's commit history — someone with access to an older commit can still see it. If you need to erase it completely, you will need the command line.

Deleting a file using the command line

Open your terminal, navigate to your repository folder, and run git rm filename.txt (replace filename.txt with the actual name). This removes the file from your working directory and stages the deletion. Run git commit -m "Remove filename.txt" to create a commit that records the deletion. Then run git push to send the change to GitHub.

The command line method is useful when you are deleting multiple files or when you want to delete a file and commit other changes at the same time. You can also use git rm -r foldername to delete an entire folder and everything in it. Like the web interface, this removes the file from the current state of your repository but leaves it in the history.

Removing a file from your entire repository history

If you need to erase a file completely — so it does not appear in any commit, even old ones — you cannot use a simple delete. You need git filter-branch or a tool like BFG Repo-Cleaner. These tools rewrite your repository's history, which is powerful but also risky if other people are working from the same repository.

BFG Repo-Cleaner is simpler than git filter-branch for this task. Download it, run bfg --delete-files filename.txt in your repository folder, then run git reflog expire --expire=now --all and git gc --prune=now --aggressive. Finally, run git push --force to overwrite the history on GitHub. This is a last resort — only use it if the file contains sensitive information like passwords or API keys, and warn your team before you do it.

What happens to empty folders after you delete files

GitHub does not store empty folders. When you delete the last file in a folder, the folder disappears from GitHub automatically on the next push. Your local copy of the repository will still have the empty folder until you manually delete it with rm -r foldername on Mac or Linux, or rmdir foldername on Windows.

If you want to keep a folder but have it appear empty on GitHub, you can create a hidden file called .gitkeep inside it. This file does nothing except prevent Git from ignoring the folder. Add it with git add foldername/.gitkeep and commit it.

Recovering a deleted file

If you delete a file by mistake, you can recover it as long as the deletion has been committed and pushed to GitHub. Go to your repository, click the "Commits" link, and find the commit that deleted the file. Click on it to view the changes, then click the three dots next to the deleted file and select "View file". Click the "Raw" button, copy the content, and save it locally. You can also check out an older commit with git checkout commit-hash -- filename.txt to restore the file to your working directory.

If you have not yet pushed the deletion, you can undo it locally with git restore filename.txt or git checkout -- filename.txt (depending on your Git version). This brings the file back to your working directory without creating a new commit.

Deleting files from a pull request before merging

If a pull request includes files you do not want to merge, you cannot delete them directly from the pull request interface. You have two options: ask the person who opened the pull request to delete the files and push the change, or merge the pull request and then delete the files in a separate commit on the main branch.

If you have permission to edit the branch, you can also push new commits to the same branch that the pull request came from. Delete the files locally, commit, and push — the pull request will update automatically to show the new state of the branch.

Frequently Asked Questions

Can I delete a file without deleting it from my computer?

Yes. When you use git rm, the file is removed from Git's tracking but stays on your computer. If you want to keep the file locally but remove it from the repository, use git rm --cached filename.txt instead. This tells Git to stop tracking the file without deleting it from your working directory.

What is the difference between deleting a file and removing it from Git tracking?

git rm deletes the file and removes it from tracking in one step. git rm --cached removes it from tracking only, leaving the file on your computer. Use the cached version if you want to keep the file locally but stop uploading it to GitHub — often used for configuration files or API keys that should not be in the repository.

If I delete a file, can someone else still see it in the repository history?

Yes, unless you rewrite the history with git filter-branch or BFG Repo-Cleaner. A simple delete removes the file from the current state but leaves it in older commits. Anyone who checks out an earlier commit or views the commit history can still see the file. This is usually fine, but if the file contains passwords or sensitive data, you should use a history-rewriting tool.

How do I delete multiple files at once?

Use git rm filename1.txt filename2.txt filename3.txt to delete several files in one command, or git rm *.txt to delete all files with a certain extension. Stage and commit the changes as usual with git commit and git push.