To delete a file from a Git repository, you can follow these steps. There are a couple of methods depending on whether you want to just remove it locally or completely from the Git history as well.
1. Delete a File from the Working Directory and Stage the Removal
This is the most common way to remove a file from the repository.
Steps:
- Remove the file using
git rm
:git rm <file_name>
For example, to remove a file called
file.txt
, you would run:git rm file.txt
- Commit the change to record the removal:
git commit -m "Remove file.txt from the repository"
- Push the changes to the remote repository:
git push
Now, the file is removed from both your local working directory and the Git repository, and the change has been pushed to the remote.
2. Remove a File from the Repository but Keep It Locally
If you want to delete a file from the repository, but keep it in your local working directory (for example, if you want to stop tracking it but still need it locally), you can use the --cached
option:
Steps:
- Remove the file from the repository but keep it locally:
git rm --cached <file_name>
For example:
git rm --cached file.txt
- Commit the change to reflect that the file has been removed from the repository:
git commit -m "Stop tracking file.txt"
- Push the changes to the remote repository:
git push
After these steps, the file will still exist in your local directory but will no longer be tracked by Git or included in the repository.
3. Delete a File from Git History (Remove a File Completely)
If you need to completely remove a file from the Git history (i.e., it should be deleted even from previous commits), you can use the git filter-branch
command or BFG Repo-Cleaner. This is more advanced and is typically used when a file contains sensitive information like passwords or API keys.
Using git filter-branch
:
- Run the following command to remove the file from all previous commits:
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch <file_name>" --prune-empty --tag-name-filter cat -- --all
For example, to remove
file.txt
:git filter-branch --force --index-filter "git rm --cached --ignore-unmatch file.txt" --prune-empty --tag-name-filter cat -- --all
- Push the changes with the
--force
option (since you’re rewriting history):git push origin --force --all git push origin --force --tags
Using BFG Repo-Cleaner:
BFG Repo-Cleaner is a simpler and faster alternative for cleaning Git history.
- Download and install BFG Repo-Cleaner
- Run BFG to delete the file from history:
bfg --delete-files <file_name> <repository_name>.git
For example:
bfg --delete-files file.txt myrepo.git
- Clean up the repository and force-push the changes:
cd myrepo git reflog expire --expire=now --all=once git gc --prune=now --aggressive git push --force
Summary
- Basic Removal: Use
git rm <file_name>
to delete a file and commit the change. - Keep Locally: Use
git rm --cached <file_name>
to stop tracking a file but keep it locally. - Delete from History: Use
git filter-branch
or BFG Repo-Cleaner for permanently removing a file from all Git history.