Removing files in Git isn’t always as simple as it seems. You delete a file from your system, but it doesn’t mean it’s gone from the repository. It may still exist in your history, or worse, get committed by accident, like a config file or an API key.
In this guide, you’ll learn how to remove files from Git. We’ll start with basic commands like git rm, then go into more advanced methods like cleaning untracked files and editing your commit history.
Prerequisites
Before we remove files with Git, let’s make sure everything is set up.
First, check if Git is installed. Open your terminal and run:
You can also install a Git GUI like GitHub Desktop, Sourcetree, or GitKraken if you prefer a visual interface. But in this guide, I’ll use the command line (Git Bash).
Make sure you have permission to make changes in the repository, especially if you’re part of a team or working with a remote repo like GitHub or GitLab.
Note: Some file removal actions can't be undone. So, it’s a good idea to back up your project or at least double-check the state of your repo before running any commands that delete files.
Once that’s done, let’s get started.
Basic File Removal in Git
There are a few different ways to remove files in Git, depending on what you want to do. Let’s see some common options:
Using git rm
The git rm command removes files from your Git repository. When you run it, two things happen:
Git removes the file from your working directory
It stages that change for the next commit
But Git doesn’t only delete the file. It tracks the deletion in your history. Once you commit, the file is officially removed from the project.
This deletes every file inside the temp folder without deleting the folder itself.
Git also adds some safety checks. If a file has changes that aren’t committed yet, Git won’t delete it unless you force it. So use this only if you’re sure those changes can be discarded:
(Here, * means all. It matches any filename that ends in .txt.)
2. --cached
This removes the file from the repository but keeps it on your computer. Let’s say I accidentally added a sensitive file like config.env. I don’t want it in the repo, but I still need it on my machine. So I’d run:
Here, git rm will delete it from the repository and working directory. And --cached untracks it from Git (so it won't be pushed), but leaves it physically in place.
To prevent it from being added again, add it to .gitignore:
It stands for recursive. Instead of one file, this recursively deletes the directory and its contents. Without this, Git will give you an error if you try to remove a folder.
No need to run git add separately. It’s cleaner and you won’t forget to stage the deletion.
In short:
Manual deletion requires extra steps to make sure Git picks up the change.
If you forget to stage the deletion, Git still thinks the file exists, which can cause confusion in commits or merges.
git rm is cleaner and safer when you're intentionally removing tracked files.
Restore deleted files in Git
What if you delete a file and later realize you still need it? No problem. Git gives you a few ways to bring it back depending on when you notice the mistake.
If you haven’t committed yet:
Let’s say you deleted the file but haven’t committed the change. You can restore it from the last commit like this:
This shows a complete history of your Git commits, checkouts, resets, and everything.
But git reflog is local only and won’t work on remotes. If you’re working with a remote repo, it’s better to use git log -- file.txt to track the history of that file.
Once you find the point where the file still existed, restore it using either git checkout or git restore like before.
Fix Missing Files in Git
Sometimes files get deleted directly from the system—maybe through a file explorer, a terminal rm command, or even by accident. When this happens, Git doesn’t automatically clean things up. Instead, it marks those files as missing and waits for you to handle the removal properly.
Let’s see how you should deal with such a situation, step by step.
Replace <branch-name> with the working branch, like main or dev.
Step 4: Alternatively
Let’s say you’ve deleted a bunch of files manually, maybe by accident, or while cleaning up. Git still thinks those files exist until you tell it otherwise.
If you don’t want to remove each one with git rm, there’s a faster way to clean things up.
git ls-files --deleted lists all files that Git is tracking but that are now missing.
xargs -0 git rm passes those file names to git rm and removes them from Git’s index.
This is quite helpful when you’re working on a big project and want to clean things up quickly.
Note: I got this message because there are no tracked files that are currently missing from your disk.
Advanced File Removal in Git
Now that we've covered the basics, let’s look at more advanced situations where removing a file from Git isn't as simple as running git rm. These are cases where either sensitive data is involved or untracked files need to be cleaned up carefully.
Remove sensitive data from Git
Sometimes a file with private data, such as API keys or passwords, is accidentally committed. Deleting it normally won’t help. Because Git keeps a complete history, and the file still exists in earlier commits.
To erase it completely, you can use the git filter-repo command: git filter-repo (recommended)
This is a newer and better alternative to filter-branch. It’s faster, safer, and easier to use.
Then let your teammates know. They’ll need to re-clone or reset their local copies to avoid errors.
Clean untracked files with git clean
Let’s break it down:
Tracked files are in Git’s history. To remove them, use git rm, then commit.
Untracked files are not in Git at all. These can build up and clutter your project.
To delete untracked files or folders, use git clean.
Here are the most useful options:
git clean -n # Show which untracked files will be removedgit clean -nd # Show which untracked folders will be removedgit clean -fd # Delete all untracked files and folders. It is destructive.제공
Here:
-n means preview only.
-f means force delete.
-d includes folders in the cleanup.
Note: git clean won’t touch tracked files. But it will delete anything untracked, even files you meant to keep. So, always use -n first to see what it will remove.
File Removal in GitHub & GUI Tools
Most of what we’ve covered so far uses the command line. But not everyone works that way and that’s totally fine. Some developers use visual tools when working directly in GitHub.
So let’s see how deleting files works in GitHub’s web interface and other GUI tools, and how that compares to the command line.
Delete files in GitHub website
If you're working in GitHub’s browser interface, you can delete files without using the terminal.
Here’s how it works in the 2025 interface:
Open the file you want to delete.
Click the three dots in the top-right corner.
Choose the Delete file from the dropdown.
GitHub will then show a commit message box. Add a short message like:
Or create a new branch and start a pull request (recommended for team workflows)
Click Commit changes, and GitHub will remove the file and save the deletion as a commit, just like git rm does from the command line.
Delete files with Git GUI tools
If you use a Git GUI like GitHub Desktop, Sourcetree, or Tower, the process is also simple:
Delete the file in your file explorer or within the app.
The app shows the deletion under “Changes” or “File Status.”
Review, stage, and commit the change.
Each tool looks a bit different, but the workflow is similar: You track changes visually, and commit when you’re ready.
Best practices by platform
Here’s a quick look at how file deletion works across different platforms, and when each one is most useful:
GitHub (Web): Good for quick fixes and small updates. Avoid it for big cleanups or sensitive files.
Git GUI tools: Great for daily work. Easy to see changes and commit safely.
Command line: Most flexible and helpful. Best for advanced tasks like buld edits or history changes.
Best Practices for Git File Removal
Removing files in Git might look simple, but one wrong step can break your code. Here are some practical tips I use to make sure deletions are safe and controlled, especially on shared or long-term projects.
Always verify before deleting
Before removing a file, take a minute to confirm what it’s doing:
Run git status to see if it’s staged or modified.
Use git log -- <filename> to check its commit history.
This quick check saves me hours of recovery work later.
Use atomic commits
An atomic commit means one commit per logical change.
For example, if I’m deleting unused files, I commit only those deletions, separate from code edits.
Here’s why it’s quite helpful:
Debugging is easier. Tools like git bisect work better when commits are small and focused.
The commit history stays clean and clear.
Code reviews go faster.
Collaborate carefully on history rewrites
Rewriting history with tools like git filter-repo affects everyone on your team. To avoid chaos:
Communicate before rewriting shared branches.
Use git push --force carefully as it rewrites remote history.
Tell teammates to re-clone or reset their local repos afterward.
Use --dry-run before running destructive commands
Before running a command that deletes files, always test it:
This puts all files back to their last committed state.
Final Thoughts
When you’re removing files in Git, it’s easy to mess up if you’re not paying attention. One small mistake can lead to bigger problems: broken code, lost work, or confusion in your team. That’s why, take a second to double-check what you’re doing.
Use the right command for the situation. If you’re not sure what something will do, test it with a dry run first. And if you’re working with others, make sure everyone’s on the same page.
Take it one step at a time. That’s how you get good at this stuff.
Git Remove File FAQs
What does “staging the removal” mean?⌄
Staging means getting changes ready for your next commit. When you run git rm filename, Git deletes the file from your folder and prepares that deletion to be saved in the next commit. This is called staging the removal. The change isn’t permanent until you run git commit.
What’s the difference between git rm and rm?⌄
rm deletes a file from your local folder only. Git won’t track this change until you specify it.
git rm removes the file and stages the deletion for your next commit, so Git deletes it from the repo too.
When should I use git push origin vs git push?⌄
Here’s when to use each:
Use git push if your branch is already connected to a remote.
Use git push origin when you want to be clear about which remote to push to, like when you're pushing a new branch or have more than one remote.
What does git pull do?⌄
git pull updates your local branch with the latest changes from the remote. It fetches new commits from the remote and then merges them into your current branch.
I'm a content strategist who loves simplifying complex topics. I’ve helped companies like Splunk, Hackernoon, and Tiiny Host create engaging and informative content for their audiences.
Git prune is a Git command that removes objects from the repository that are no longer reachable from any commit or branch, helping to free up disk space.
Learn how to use .gitignore to keep your Git repository clean. This tutorial covers the basics, common use cases, and practical examples to help you get started!
Learn how to use the git status command to monitor your repository’s state. This guide is perfect for beginners and early‑career practitioners who want an optimized Git workflow.
Removing files in Git isn’t always as simple as it seems. You delete a file from your system, but it doesn’t mean it’s gone from the repository. It may still exist in your history, or worse, get committed by accident, like a config file or an API key.
In this guide, you’ll learn how to remove files from Git. We’ll start with basic commands like
git rm, then go into more advanced methods like cleaning untracked files and editing your commit history.Prerequisites
Before we remove files with Git, let’s make sure everything is set up.
First, check if Git is installed. Open your terminal and run:
If you see a version number, you’re good to go.
If you see a version number, you’re good to go.
You can also install a Git GUI like GitHub Desktop, Sourcetree, or GitKraken if you prefer a visual interface. But in this guide, I’ll use the command line (Git Bash).
Make sure you have permission to make changes in the repository, especially if you’re part of a team or working with a remote repo like GitHub or GitLab.
Note: Some file removal actions can't be undone. So, it’s a good idea to back up your project or at least double-check the state of your repo before running any commands that delete files.
Once that’s done, let’s get started.
Basic File Removal in Git
There are a few different ways to remove files in Git, depending on what you want to do. Let’s see some common options:
Using git rm
The
git rmcommand removes files from your Git repository. When you run it, two things happen:But Git doesn’t only delete the file. It tracks the deletion in your history. Once you commit, the file is officially removed from the project.
You can remove one file, many files, or a group of files using wildcards.
For example, to remove all log files:
This tells Git to remove every file in the folder that ends in
.log. You can confirm the change using:Delete multiple files with one git rm command. Image by Author.
Want to remove all files inside a folder but not the folder itself? Use:
This deletes every file inside the temp folder without deleting the folder itself.
Git also adds some safety checks. If a file has changes that aren’t committed yet, Git won’t delete it unless you force it. So use this only if you’re sure those changes can be discarded:
To remove a full folder and everything in it, add the
-rflag:This removes
testfolderand all files inside it.Now, to commit the change, run the following command:
Key git rm options
Here are the most common options you’ll use with
git rm:1. <filename>
This is the basic usage. Just list the files you want to remove:
You can also use a wildcard to remove multiple files of the same type. For example, to remove all
.txtfiles:(Here,
*means all. It matches any filename that ends in.txt.)2. --cached
This removes the file from the repository but keeps it on your computer. Let’s say I accidentally added a sensitive file like config.env. I don’t want it in the repo, but I still need it on my machine. So I’d run:
Here,
git rmwill delete it from the repository and working directory. And--cacheduntracks it from Git (so it won't be pushed), but leaves it physically in place.To prevent it from being added again, add it to
.gitignore:3. -r
It stands for recursive. Instead of one file, this recursively deletes the directory and its contents. Without this, Git will give you an error if you try to remove a folder.
Here, I created a folder named
testfolderand two files inside it:a.txtandb.txt.Next, I run the following command:This removed the folder along with all the contents and files inside it
4.
--dry-runIf you’re not sure what will happen if you remove a file, use
git rmwith--dry-runoption like this:This is a safe way to preview what will be removed.
Or if you’re cleaning up untracked files and want to double-check what’ll be removed, use:
This will only show what this command will delete.
Comparative Workflow Analysis
There’s more than one way to remove a file in Git. The methods might seem the same, but they don’t behave the same.
git rm vs manual deletion
Let’s take a file called
file.txt. Say I don’t need it anymore.One way to remove it is to delete it directly from the system.
On Linux, macOS, or Git Bash:
On Windows (command prompt):
This deletes the file from your folder. But Git doesn’t know that yet. So you have to manually tell Git that the file was removed:
This stages the deletion so it’s included in the next commit.
If you deleted several files and don’t remember all of them, you can run:
This stages all tracked files that were changed or deleted. It won’t include untracked files.
Now you commit the change:
If you use
git rm, it handles everything in one step. It deletes the file and stages the change right away:No need to run
git addseparately. It’s cleaner and you won’t forget to stage the deletion.In short:
git rmis cleaner and safer when you're intentionally removing tracked files.Restore deleted files in Git
What if you delete a file and later realize you still need it? No problem. Git gives you a few ways to bring it back depending on when you notice the mistake.
If you haven’t committed yet:
Let’s say you deleted the file but haven’t committed the change. You can restore it from the last commit like this:
This brings the file back into your working directory from the latest commit.
If you already committed the deletion:
That’s fine too. First, find the last commit where the file still existed:
Look through the log and copy the commit hash where the file was still there. Then run:
In older versions of Git, you could use the above command. This command restores a file from a specific commit, but could also switch branches.
Starting from Git 2.23, this was split into two clearer commands:
git switch(for switching branches)git restore(for restoring files)If you only want to restore a file from a commit, use:
This pulls the file from that commit and puts it back in your working directory.
If you’re not sure when the file was deleted:
Maybe you’ve made a bunch of changes and don’t remember when the file went missing. In that case, use git reflog or reference log:
This shows a complete history of your Git commits, checkouts, resets, and everything.
But
git reflogis local only and won’t work on remotes. If you’re working with a remote repo, it’s better to usegit log -- file.txtto track the history of that file.Once you find the point where the file still existed, restore it using either
git checkoutorgit restorelike before.Fix Missing Files in Git
Sometimes files get deleted directly from the system—maybe through a file explorer, a terminal rm command, or even by accident. When this happens, Git doesn’t automatically clean things up. Instead, it marks those files as missing and waits for you to handle the removal properly.
Let’s see how you should deal with such a situation, step by step.
Step 1: Check for missing files
Run this in your project folder:
If Git says a file is deleted, that means it’s gone from your system, but the change hasn’t been staged yet.
Step 2: Stage the deletion
To tell Git to stage all changes (including deleted files), run:
This stages all file changes: new, modified, and deleted. Then commit the update:
This is the safest way to clean up because it catches every change in one go.
Step 3: Push to the remote
If you’re working with a remote repository, push the commit:
Replace
<branch-name>with the working branch, likemainordev.Step 4: Alternatively
Let’s say you’ve deleted a bunch of files manually, maybe by accident, or while cleaning up. Git still thinks those files exist until you tell it otherwise.
If you don’t want to remove each one with
git rm, there’s a faster way to clean things up.Run this command in your terminal:
Here’s what it does:
git ls-files --deletedlists all files that Git is tracking but that are now missing.xargs -0 git rmpasses those file names togit rmand removes them from Git’s index.This is quite helpful when you’re working on a big project and want to clean things up quickly.
Note: I got this message because there are no tracked files that are currently missing from your disk.
Advanced File Removal in Git
Now that we've covered the basics, let’s look at more advanced situations where removing a file from Git isn't as simple as running
git rm. These are cases where either sensitive data is involved or untracked files need to be cleaned up carefully.Remove sensitive data from Git
Sometimes a file with private data, such as API keys or passwords, is accidentally committed. Deleting it normally won’t help. Because Git keeps a complete history, and the file still exists in earlier commits.
To erase it completely, you can use the
git filter-repocommand: git filter-repo (recommended)This is a newer and better alternative to
filter-branch. It’s faster, safer, and easier to use.First, install it (you’ll need Python and pip):
Then run:
This removes the
hide.txtfile from every commit in your repo’s history.To confirm, run:
If the file is gone, add it to
.gitignoreso it doesn’t get tracked again:If you're working with a team, clean the rewrite history and force-push the changes:
Then let your teammates know. They’ll need to re-clone or reset their local copies to avoid errors.
Clean untracked files with git clean
Let’s break it down:
git rm, then commit.To delete untracked files or folders, use
git clean.Here are the most useful options:
Here:
-nmeans preview only.-fmeans force delete.-dincludes folders in the cleanup.Note:
git cleanwon’t touch tracked files. But it will delete anything untracked, even files you meant to keep. So, always use-nfirst to see what it will remove.File Removal in GitHub & GUI Tools
Most of what we’ve covered so far uses the command line. But not everyone works that way and that’s totally fine. Some developers use visual tools when working directly in GitHub.
So let’s see how deleting files works in GitHub’s web interface and other GUI tools, and how that compares to the command line.
Delete files in GitHub website
If you're working in GitHub’s browser interface, you can delete files without using the terminal.
Here’s how it works in the 2025 interface:
GitHub will then show a commit message box. Add a short message like:
You can also write an optional description.
Next, choose how to save the change:
mainbranchClick Commit changes, and GitHub will remove the file and save the deletion as a commit, just like
git rmdoes from the command line.Delete files with Git GUI tools
If you use a Git GUI like GitHub Desktop, Sourcetree, or Tower, the process is also simple:
Each tool looks a bit different, but the workflow is similar: You track changes visually, and commit when you’re ready.
Best practices by platform
Here’s a quick look at how file deletion works across different platforms, and when each one is most useful:
Best Practices for Git File Removal
Removing files in Git might look simple, but one wrong step can break your code. Here are some practical tips I use to make sure deletions are safe and controlled, especially on shared or long-term projects.
Always verify before deleting
Before removing a file, take a minute to confirm what it’s doing:
git statusto see if it’s staged or modified.git log -- <filename>to check its commit history.This quick check saves me hours of recovery work later.
Use atomic commits
An atomic commit means one commit per logical change.
For example, if I’m deleting unused files, I commit only those deletions, separate from code edits.
Here’s why it’s quite helpful:
git bisectwork better when commits are small and focused.Collaborate carefully on history rewrites
Rewriting history with tools like
git filter-repoaffects everyone on your team. To avoid chaos:git push --forcecarefully as it rewrites remote history.Use --dry-run before running destructive commands
Before running a command that deletes files, always test it:
This shows what would be deleted without deleting anything. Here’s why I think you should always do this:
Combine git rm --cached with .gitignore
If you accidentally commit something that shouldn’t be in the repo (like a config file or logs), fix it in two steps:
This tells Git to stop tracking the file but leaves it on your computer.
gitignore:Here’s why it’s helpful:
.env,.log, ornode_modules) out of the repo.Git Edge Cases: Staging & Empty Folders
Sometimes file removal doesn’t go as expected. You may stage the wrong file or run into issues with empty folders.
Here’s how I handle common edge cases.
Unstage a file without removing your changes
Let’s say I run
git add .and accidentally stage a file I didn’t mean to. I don’t want to lose my changes—I just want to unstage it.To fix that, I run:
This unstages the file but keeps the edits in place.
To confirm, I use:
Now the file should show as "modified" instead of "staged."
Keeping empty folders in the repo
By default, Git ignores empty folders. If I try to commit a folder with no files inside, Git skips it.
But sometimes I want to keep that folder in the repo either for future logs or uploads. The usual fix is to add a placeholder file called
.gitkeep.Here’s how:
This makes the folder "non-empty," so Git tracks it.
Note:
.gitkeepisn’t built into Git. It’s just a convention. You can name the file anything, but.gitkeeptells others what it’s for.Git File Removal Troubleshooting
Here are a few common problems I’ve run into when removing files in Git and how to fix them.
The file still appears in the remote repository
You deleted the file, committed the change, but it’s still on the remote repo. Maybe you forgot to push.
So make sure to run:
Also check:
Problems with submodules
Submodules are Git repositories inside other Git repositories. They don’t behave like normal folders.
If you're deleting files inside a submodule:
If you're removing the submodule entirely:
.gitmodulesand.git/configDetached HEAD state
In Git,
HEADusually points to the current branch. But sometimes it points to a specific commit instead, that’s called a detached HEAD.This happens if you:
Check your status:
If you see:
You’re not on a branch, and any new changes may be lost when you switch away.
Here’s how you can fix it:
If you’ve made changes and want to keep them, run this:
This puts your work on a new branch and saves it.
If you’ve already committed the changes and want to move them to another branch later, do this:
If you didn’t make any changes and just want to get out of detached HEAD, run this:
That’s it. Your HEAD is now reattached, and everything is back to normal.
Permission or state errors
Sometimes Git won’t delete a file usually on Windows. This can happen if:
Here’s how to fix this:
Merge or rebase issues
If you see errors like:
You’re likely in the middle of a merge or rebase.
To finish a rebase after resolving conflicts:
To cancel the rebase or merge entirely:
This restores your repo to how it was before the merge started.
Want to throw away all local changes and reset your working directory?
Try this:
This puts all files back to their last committed state.
Final Thoughts
When you’re removing files in Git, it’s easy to mess up if you’re not paying attention. One small mistake can lead to bigger problems: broken code, lost work, or confusion in your team. That’s why, take a second to double-check what you’re doing.
Use the right command for the situation. If you’re not sure what something will do, test it with a dry run first. And if you’re working with others, make sure everyone’s on the same page.
There’s a lot more Git can do beyond deleting files. You can delete branches, undo mistakes using reset and revert, or see exactly what changed with git diff. The more you learn, the smoother your workflow gets.
Take it one step at a time. That’s how you get good at this stuff.