How to git remove file from commit after push or staging

Wondering how to git remove file from commit from staging, tracking, or after push ? Well, it is even possible to remove files from repository so let’s check.

There are times when I myself commit or push some file and then feel the need to remove specific files from the commit.

But then I don’t want to revert the full commit. Instead just want to remove a file.

To understand what problem we are trying to solve here, let us first understand how Git works.

There are 3 stages of a file in git lifecycle :

  1. Untracked when you just created a file
  2. Workspace or local file edits when the file was already present.
  3. Indexed where the files have been staged for commits. Mostly git addcommand is used to do so.
  4. When we actually commit and push the changes to the repository.

Based on these 3 stages, in this tutorial, we will see how to remove files from git based on the status and different ways to do so.

1. Remove file from staging

To remove the file from the local staged changes list and cache we can use a simple command.

git rm --cached <FILE>

Here --cachedremoves the changes from local cache also.

If the above command doesn’t look good enough to you, here is another approach. Alternatively, run this command and you should be good to go.

git reset HEAD -- <file>

[su_label type=”success”]Suggested read[/su_label] How to clone a branch in git with just 2 commands

2. Remove file from local commit

This method is applicable when the file you committed is only locally and is part of the latest commit. It has not been pushed to the repo yet. Then you can git remove file from commit with these steps.

1. Delete the file from local

It will need a few commands to be executed one by one. Here are commands and their explanations.

git reset --soft HEAD^1

The above command will revert your last commit whereas changes will still be in an indexed state. You can use gst or git status command to see the changes.

Then we will remove the file from staged changes by using the command.

git rm --cached <FILE>

This will move the corresponding file to untracked changes state.

[su_note note_color=”#FFF9C4″]Removing the file using rm will delete it from local also ![/su_note]

Since we are done now, therefore we can make a commit now which doesn’t have the removed file. The below command will help you to do so.

git commit -m "<your-message>"

3. Do not delete the file and changes from local

This method applies if you want to revert mistakenly committed files back to the staging area from the last commit, without canceling the changes done.

Here are a few steps to help you with it.

git reset --soft HEAD~1

Then reset the unwanted files so that we leave them out from next commit, we will be making.

git reset HEAD path/to/file_to_remove

Now use the previous commit message and make a new commit again. Basically re-use the same commit message.

git commit -c ORIG_HEAD

or

git commit --amend

[su_label type=”success”]Suggested read[/su_label] Best NoSQL databases list

3. Remove file from commit after push

Here as well we will be removing file using git rm.

Here are some simple steps to remove a file from local as well as from the original repo.

git rm FILE.txt

This will remove the git file from the local.

git commit -m "removes FILE.txt"

If you do not want to remove from local but just from the repo, use the following steps.

git rm --cached FILE.txt

Then use the above commit command and then push the changes with below command.

git push origin name_of_branch

[su_note note_color=”#FFF9C4″] Note: this will not help you get rid of file from others commits and might push the file back to repo if somebody else commits and pushes the file again.[/su_note]

Additional tip :

  •  To remove the directory and all its content recursively, use : git rm -r directory

[su_label type=”success”]Suggested read[/su_label] Stop and remove all docker containers with easy commands !

4. Remove file from tracking

If you want to remove a file from being tracked, you can remove it from the index. This can be achieved with a single command.

git rm --cached <FILE>

If the use case is to remove a whole folder, and all the files within it, use the below command.

git rm -r --cached <FOLDER>

Then just make a commit and it should work.

[su_note note_color=”#FFF9C4″] Note: This will not remove the file from your local machine, but the next time other developers take a pull of changes, it will delete the file/folder from their machine.[/su_note]

If you are interested in more similar git related tutorials, our website has a few of them. Do check out and let us know if you like this git remove file from commit guide.

Leave a Comment