This post will show you how to remove or unstage a file from the git commit. Note that it’s different than removing a file from git. Anyways, if you make a commit & even if you pushed the changes to the remote origin, this is still possible to remove a file from the commit and the remote git repository.
Let’s see how you can do it.
How to remove a file from git commit?
I will walk you through the step-by-step process but I suggest you take a backup of your repository or download a zip. This is just for safety precautions if there anything goes wrong or if you misunderstood my guideline.
Step 1: First check the status of your repository using the following command: git status
This will show you the list of files that are currently staged or committed.
Step 2: Identify the file that you want to unstage or delete from the commit.
In my example, I want to remove the “style.css.map” from my “third commit.”
Please note that this file is already committed and even pushed to the GitHub repository. So you will need to create a new commit to remove it from the commit history.
Step 3: Remove the file using the following command:
git reset HEAD^ name_of_the_file
Replace the “name_of_the_file” with the actual file name that you want to unstage from the commit.
In my case, the command will be as follows: git reset HEAD^ style.css.map
This will create a new commit that removes the file from the commit history.
Step 4: You’re almost done! Now you have to make another commit to override the last commit. If you type the first command & hit enter, you’ll see that the desired file has been removed from the staging area (as you see in the above screenshot).
To make a new commit, type git commit -m "another commit or whatever"
and hit enter.
And push the changes by git push origin master
After you push the commit to the remote origin (repository), you’ll see that the file no longer exists. And after pushing my changes to the remote origin, I saw that the file is no more there (see the screenshot below).
But when I checked my last commit (third commit), the file was there.
That’s it!
In a nutshell, you need to use the following command to remote or unstage a file from the last commit & push: git reset HEAD^ name_of_the_file
How to completely remove a file from the local repository?
If you created a bogus file and already made a commit, you can remove the file from your local machine.
Not to mention, you can delete a file by hitting the “Delete” button on your keyboard but this is not the end. Because Git keeps track of the changes and can restore the deleted file.
To delete a file permanently from your local copy, use the following command:
git rm name_of_the_file
This will remove the file from your computer and also from future commits.
Note that if you try to use the rm
command without unstaging the file, it will not work and trigger a fatal error “did not match any files.”
That means rm
only works with commit files.
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?
Conclusion
Before you pretend to remove a file or uncommit changes, be sure to take a backup of your repository. Because it can change the commit history and even you can lose data.
But if you’re sure about what you’re doing, there is no problem with proceeding.
In this post, I showed you how to unstage or remove a file from the git commit & remote origin. Also, I showed you how to remove a file from your computer and future commits. If you have any questions, let me know.