Git Delete File or Directory (git rm, git clean) with Examples

Git Delete File or Directory (git rm, git clean) with Examples

Git provides multiple ways to delete files and directories depending on whether they are tracked, untracked, or ignored. Understanding when to use git rm, git clean, or other methods helps avoid accidental data loss and keeps your repository clean as part of an efficient Git workflow.. This guide covers all common deletion scenarios with clear examples and best practices.


Git Delete File or Directory - Quick Cheat Sheet

TaskCommand
Delete tracked filegit rm file.txt
Delete tracked directorygit rm -r folder/
Remove file from Git but keep locallygit rm --cached file.txt
Remove directory from Git but keep locallygit rm -r --cached folder/
Delete untracked filesgit clean -f
Delete untracked files and directoriesgit clean -fd
Preview files to be deleted (safe mode)git clean -n
Delete ignored files onlygit clean -fX
Delete ignored + untracked filesgit clean -fx
Force delete file using system commandrm file.txt
Force delete directory using system commandrm -rf folder/
Delete file from last commitgit reset --hard HEAD~1
Remove file from entire historygit filter-branch ...
Delete multiple filesgit rm file1 file2
Delete files by patterngit rm *.log
Clean specific directory onlygit clean -f folder/

When to Use git rm vs git clean

Delete tracked files (git rm explained)

Use git rm when the file is already tracked by Git (i.e., added and committed earlier). This command removes the file from both the working directory and the repository (index), and the deletion is recorded in the next commit.

Delete untracked files (git clean explained)

Use git clean when you want to remove files that are not tracked by Git (e.g., temporary files, build artifacts). These files are not part of commits, so git clean helps keep your working directory clean without affecting repository history.

Common confusion between rm, git rm, and git clean

  • rm: Deletes files only from the filesystem (Git still sees them as deleted but not staged).
  • git rm: Deletes tracked files and stages the deletion automatically.
  • git clean: Deletes untracked (and optionally ignored) files only.

Delete File in Git (Step-by-Step)

Check file status before deletion

Run the following command to confirm whether the file is tracked or untracked using git status:

text
git status

Delete file using git rm

Delete the file and stage the removal in one step:

text
git rm file.txt

Commit deletion properly

Record the deletion in Git history uusing git commit:

text
git commit -m "Delete file.txt"

Verify file removal

Confirm the file is removed:

text
ls
git status

Delete Directory in Git

Remove folder recursively using git rm -r

Delete a directory and all its tracked contents:

text
git rm -r folder/

Delete nested directories safely

Check what will be deleted before running the command:

text
git status

Verify directory deletion

Ensure the directory is removed from tracking using git status:

text
git status
git ls-files

Remove File from Repository but Keep Locally

Use git rm --cached explained

Remove file from Git but keep it locally using git rm --cached:

text
git rm --cached file.txt

Stop tracking files (like logs, config)

Example:

text
git rm --cached .env
git rm --cached logs/app.log

Update .gitignore after removal

Prevent Git from tracking the file again:

text
echo "file.txt" >> .gitignore
git add .gitignore
git commit -m "Ignore file.txt"

Delete Untracked Files in Git

Remove untracked files using git clean -f

Use git clean -f to delete untracked files (files not added to Git):

text
git clean -f

This removes only files, not directories.

Remove directories using git clean -fd

To delete both untracked files and directories:

text
git clean -fd

This is useful for cleaning build folders or temporary directories.

Preview deletion using git clean -n (safe mode)

Before deleting, always preview using git clean:

text
git clean -n

This shows what will be removed without actually deleting anything.


Delete Ignored Files in Git

Remove ignored files using git clean -fX

To delete only files listed in .gitignore using git clean:

text
git clean -fX

Useful for removing build artifacts or ignored logs.

Delete all (ignored + untracked) using git clean -fx

To delete everything not tracked by Git:

text
git clean -fx

⚠️ This removes both ignored and untracked files permanently.

When to use each option

  • -f → Remove untracked files
  • -fd → Remove files + directories
  • -fX → Remove only ignored files
  • -fx → Remove everything untracked

Delete File from Git History (Advanced)

Remove file using git reset (recent commits)

To undo recent commits and remove files using git reset:

text
git reset --hard HEAD~1

This deletes the last commit and its changes.

Remove file permanently using filter-branch / filter-repo

To remove a file from entire Git history using advanced tools like git filter-branch:

text
git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch file.txt' --prune-empty --tag-name-filter cat -- --all

This rewrites history and removes the file completely, similar to advanced history operations using git reset examples.

When NOT to rewrite history

  • Avoid in shared repositories
  • Avoid after pushing to remote
  • Coordinate with team before using

git rm vs git clean vs rm Command

CommandUse CaseAffects Git Tracking
rmDeletes file from systemNo
git rmDeletes tracked filesYes
git cleanDeletes untracked filesNo

When to use each command

  • Use git rm → For tracked files
  • Use git clean → For untracked files
  • Use rm → For manual deletion (requires staging later)

Impact on repository and history

  • git rm → Changes are tracked and committed
  • git clean → No impact on history
  • git reset / filter-branch → Can rewrite history (use carefully)

Common Mistakes While Deleting Files in Git

File deleted but still tracked

Deleting a file using rm file.txt only removes it from the filesystem. Git still tracks it as a deleted file, which you can verify using git status or inspect changes using git diff examples.

Fix:

text
git rm file.txt

Or stage the deletion:

text
git add -u

Accidentally deleting important files

Using force commands like git clean -fx or git rm -r can remove important files permanently.

Fix:

  • Always run preview first (git clean -n)
  • Keep backups for critical files

git clean deleting too many files

Running git clean -fx removes both ignored and untracked files.

Fix:

  • Use git clean -f or -fd instead
  • Avoid -x unless absolutely required

Confusion between working directory and repository

Users often mix:

  • Working directory (actual files)
  • Index (staging area)
  • Repository (commits)

Fix:

  • Use git status to understand file state
  • Choose correct command (git rm, git clean, or rm)

Best Practices for File Deletion in Git

Always check git status before deletion

text
git status

This helps confirm whether files are tracked or untracked.

Use dry-run before git clean

text
git clean -n

Preview prevents accidental deletion of important files.

Keep .gitignore updated

Add unnecessary files (logs, temp files, builds) to .gitignore to avoid repeated cleanup.

Avoid rewriting history in shared repos

Commands like:

text
git reset --hard
git filter-branch

can break collaboration if used after pushing to remote.


Frequently Asked Questions

1. How do I delete a file in Git?

You can delete a tracked file using git rm filename and then commit the change. This removes the file from both the working directory and repository.

2. How do I remove a directory in Git?

Use git rm -r foldername to recursively delete a directory from Git and commit the changes.

3. What is the difference between git rm and git clean?

git rm removes tracked files from Git, while git clean removes untracked files and directories from the working directory.

4. How do I delete untracked files in Git?

Use git clean -f to remove untracked files. To delete directories as well, use git clean -fd.

5. How do I remove a file from Git but keep it locally?

Use git rm --cached filename to remove the file from the repository while keeping it in your local working directory.

6. How do I delete ignored files in Git?

Use git clean -fX to delete only ignored files or git clean -fx to delete both ignored and untracked files.

7. Can I delete a file from Git history?

Yes, you can remove a file from history using advanced commands like git filter-branch or tools like git filter-repo, but this rewrites history and should be used carefully.

Summary

In this guide, you learned how to delete files and directories in Git using the right commands like git rm and git clean for each scenario. We covered git rm for tracked files, git clean for untracked files, and advanced methods for removing files from history.

You also explored common mistakes, how to fix them, and best practices to safely manage file deletion in Git repositories.

Using the correct approach ensures clean repositories, avoids data loss, and improves your overall Git workflow.


Official Documentation

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.