Remove file from git commit

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.”

identified a file to remove from git 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

Removed a file from git commit

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).

Remote origin URL (GitHub Repository) after removing a file from last commit

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

Removed a file permanently from computer that was committed

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.”

git rm fatal error because of trying to  remove a file before staging

That means rm only works with commit files.

Learn more about Git

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.