Signup/Sign In

How to Remove Files from Git Commit

A commit is a snapshot of our project at an instant in time and it is used to maintain different versions of our project. It captures the state of all files added to the commit. Sometimes we may not want a file to be present in a commit or we may want to make modifications to that file before adding that commit. Git provides us with a few different commands to achieve this. Let's learn how to remove files from a commit.

Removing Files from a Git Commit

Removing Files from a Commit

Removing files from a commit is a two-step process. We are essentially rewriting the history of our project so we have to go back in time. To do this we use the Git Reset command in the --soft mode. The --soft mode will reset the commit but will keep the staging area and the working directory as it is. Next, we will remove the file from the staging area or the working directory according to our requirements and then commit these new changes. Let's take an in-depth look at how it is done.

Removing Files using Git Reset

Let's say, we have added a file to the last commit by mistake. We will first reset to the second last commit using the following Git Reset command.

$ git reset --soft HEAD~1

Now our HEAD points to the second last commit and we need to remove some files before adding the next commit. The changes made to this file will still be present in the staging area. If you just wish to remove the file from the staging area(so that it is not included in the commit) then use the following Git Reset command. The file will become an unstaged file and this file can be used in some other future commits.

$ git reset HEAD <file-name>

However, if you want to delete the file from the staging area(to make it an untracked file) use the Git Rm command with the --cached option. If you also want to remove it from the working directory then omit the --cached option.

$ git rm --cached <file-name>

We are almost done. The unwanted file has been removed from the staging area and all the other changes are staged. All we have to do is commit these changes using the Git Commit command. We now have our commit back but this no longer includes the files that are not needed.

$ git commit -m "commit message"

Consider the following scenario to better understand the above-mentioned process.

Suppose, we have a Git repository with three commits(A, B, and C), and commit C has an unwanted file that we want to remove.

Viewing the existing commits and the files involved in them

We will first use the Git Reset command to reset our repository to commit B. All the changes that were added to commit C will be visible in the staging area.

Resetting the repository to the second last commit

Now check the status of commit.

Viewing the changes that were staged for commit C

Now we remove the unwanted file from the staging area using the Git Rm --cached command. The rest of the changes will still be staged and the deletion will also be staged.

Removing the unwanted file from the staging area

Commit the rest of the changes by using the Git Commit command.

Commit the new changes

The changes of the removed file will still be present in the working directory, and we can use them in future commits.

Removing Files using Git Commit --Amend

There is another way of removing files from the most recent commit. In this method we use the --amend option with the Git Commit command. The --amend option is used to make changes to the most recent commit. We can add, modify or remove files from the last commit using this option. Follow the steps shown below to remove files from the most recent commit.

  • First, we will use the Git Rm command to remove the file staging area. If we use the --cached option with it then the file will become untracked but will still be present in the working directory. Omit this option if you wish to remove it even from the working directory.
$ git rm --cached <file-name>
  • Git will add this removal to the staging area and now we can add this change to the most recent commit using the Git Commit command with the --amend option. A thing to note here is that even though we are modifying the previous commit but Git will create an entirely new commit with all these changes. This can be verified by checking the hash of the commit before and after using the --amend option.
$ git commit --amend

Summary

We will often add files to the commits and later realize that they should not be present there. We can rewrite the commit history and remove these files by using the Git Reset command. After going back to a previous commit we can remove a file from the staging area or even from the working directory and then add a new commit without the unwanted file. We also learned how to use the --amend option with the Git Commit command. The --amend option is capable of modifying the last commit and we can remove the unwanted file using this option.