In this post, you’ll see how to remove file from git in different contexts. If you committed a file by mistake and pushed it to the remote origin, there are commands to delete the file.
Also, if you want to keep a file locally on your machine but delete it only from the remote origin, there is a command for it as well.
Last but not least, you also have a way to ignore the deleted file from the future commit & push.
Let’s explore them one by one.
How to delete files from Git
To delete a file locally, use the following command in your terminal:
git rm path_of_the_file
Replace the path_of_the_file with the actual path including the file name & extension such as
A few examples:
git rm vendor/app.php
git rm script.js
This will remove the file from your local repo and also stage it for the next commit as you see in the screenshot below.
Commit the deletion
After you delete the file locally, you must commit the changes as you see below.
git commit -m "deleted a file named app.php"
The commit message could be anything that is meaningful to you.
Push the deletion to the remote origin (repository)
If you want to remove the file from the remote origin as well, you have to push the changes.
git push origin master
Here I pushed the changes to the master branch and most of you will need to do the same. However, you also have the option to push the changes to a specific feature branch. For example
git push origin another_branch_you_like
Anyways, after I push the changes to the remote origin, the file was also deleted from my GitHub repository.
That means you can also remove a file/folder from your remote origin such as GitHub, Bitbucket, etc.
Summary of the commands that you used in this section:
git rm file_name
git commit -m "your custom message"
git push origin master
Sidenote: If you added all files to the stage by mistake, you can undo the
git add -A. From there you can also unstage one or more specific files. There are a couple of handy options. If this is your case, you can check the detail in this post.
How to remove a folder from Git?
To remove a folder and its content, use the following command:
git rm -r folder_name
If I want to delete a folder named “assets”, the command will be the following:
git rm -r assets
Note that it will also delete all the other files & folders that live inside the folder (as you see in the screenshot below).
This is how you can delete folders on Git.
Just like the
git rm command,
git rm -r also stage the changes which are ready to commit.
So the next commands will be as follows (to push the changes to the remote origin):
git commit -m "Your custom message"
git push origin master
Sidenote: There is a way to remove or unstage a file after commit. If you made a commit by mistake and even if you pushed the commit, this is still possible to remove a file from the commit & also from the remote origin. For more details, please see this post.
How to remove a file from the Git repository but keep it in the working directory?
Let’s say you want to keep a file on your local machine (computer) but you want to delete it from the remote origin such as the GitHub repository. There is a command for it:
git rm --cached file_name
For example, I generally like to keep a file named “information.txt” on my project where I store personal information such as conversations with clients, to-do lists, etc. If I accidentally pushed this file and want to delete it from the remote origin but keep it locally, I can use the following command:
git rm --cached information.txt
How to untrack the deleted file in the future push?
But there is a catch!
If you add all files in the future using the
git add -A, it will again add the “information.txt” file to the stage and you will find it in your repository as well (after pushing).
To get around this situation, you need to create a filed name “.gitignore” in the root directory. And also you have to include the file names that you want to ignore.
In my example, the “.gitignore” file will look like the following:
How to create the “.gitignore” file?
If you try to create the “.gitignore” as you normally do while creating other files, you won’t be able to do it. And it will show you an error “You must type a file name.”
To get around this problem, you can create the file using your code editor (file – new). Also, you can create the file using the following command:
This will create the file named “.gitignore”
Why you may need to remove a file from Git?
There are a couple of real-world examples and use cases where you may also need to remove certain files & folders from Git. And these are just not limited to the following:
- Sensitive information: If you entered your payment processor or gateway-related information in a file, you must have to delete that file. And you have to reorganize it differently. Passwords, Database credentials, etc fall into this same category. For example, a “.env” (dot env) and “.gitignore” files can help you to solve this kind of issue.
- Large files: If you pushed or tracked large files and folders which are unnecessary to keep track of, you can remove them from git. For example, you don’t need to keep track of WordPress system folders/files such as wp-admin, wp-content, wp-config.php, plugins, etc.
Learn more about Git
- What is Git and why it is used?
- How to install Git on Windows & Mac?
- How to use Git with Visual Studio Code?
- How to use Git and GitHub?
- What is the difference between Git and GitHub?
- How to connect local Git to a remote server?
- How to change Git remote origin URL?
- How to create a Git repository on GitHub?
- Git add all (stage all changes)
- How to undo the git add -A command?
- How to undo the last Git commit?
- How to change the git commit message?
- How to remove or unstage a file from Git commit?
- How to git push after rebase?
- Git switch branches: How to checkout the git branch?
- How to merge a git branch to master?
- Git branch: create, checkout, merge, list, command, delete & push
- How to clone a Git repository?
- How to git pull to override the local project?
- How to remove file from Git?
- When should you use git rebase?
- Git commands & explanation (downloadable cheatsheet included)
- How to create GitHub Pages?
When I first created my portfolio website using NodeJS, MongoDB, EJS, and after pushing it to live (Heroku), I discovered that I added my gateway information to a file. Also, the remote origin was a public repository. However, I was able to remove the file both from production & remote origin before happening anything bad with me. I was lucky enough!
Later I created a “.env” file to store such credentials and other environment variables. Also, I added this “.env” file in the “.gitignore.” This way, the website worked and I was on the safe side.
In this post, I showed how you can remove files from git (both locally & remote origin). Also, I showed you how to keep a file locally but delete it only on the GitHub repository. Additionally, I shared all the real-life experiences I came across which is related to this post and how you can be benefited from my fault & experience.