How to Remove Committed Files in Git

Abdul Jabbar Feb 02, 2024
How to Remove Committed Files in Git

Sometimes we realize that we need to manually figure out which files are important for the current commit and remove the unwanted ones from the working directory. It can be difficult for manual processes, especially if we build a large project with many developers in a team.

Suppose we have committed the wrong file in the repository and want to delete it from the branch. The git reset command is a useful tool when dealing with Git. This command is used to remove a specific file from a specific Git commit or the working directory in the branch. For example, you can run the following command if you want to remove the file "README.md" from the commit that HEAD points to.

git reset -soft HEAD@{1}

We can remove the file from a specific commit while keeping the changes in the working directory by running the following command.

git reset HEAD@{1}

As we know that the command used to remove files from a commit is git reset. This command can be used with-p and -u options. The option -p allows us to keep the changes in the working directory but reset the index. The option -u resets the index and the working directory.

Let’s practice removing files from a commit with the following example. Suppose we have created a new commit and added a file named file1. Now, we want to delete this file from the commit. We can commence this using the following command.

git reset -p HEAD~1

This command orders Git to reset the index and the working directory back to the commit HEAD~1. Before we created the commit, we add the file2 to the working directory. Now, we want to delete this file from the working directory but keep it in the index. It can be commenced using the following command:

git reset -u HEAD~1

This command orders Git to reset the index and the working directory to the commit HEAD~1. Now, let’s suppose we have added three files to the index, and we want to delete all the files from the index except for file3. We can commence this using the following command:

git reset -- '-p -- file3'

This command orders Git to keep file3 and delete the other files from the index within the same branch. The command git reset can be used to remove files from any commit. The command can be used with the -p option to keep the changes in the index and the working directory and remove the file from the commit.

The command can also be used with the -u option to reset the index and the working directory but keep the file in the commit.

If we want to delete a file from a specific commit, we can use the git reset command with the -hard option and specify the commit after HEAD. For example, we can run the following command if we want to remove the file "README.md" from the commit test.

git reset -hard test

Keep in mind that the command git reset --hard discards any changes you have made to your working directory. If you have edited some files and removed others, the last command would remove any changes you have made to files still present in your staging area.

However, suppose we don’t want the file and want to delete it from the staging area and make it an untracked file. In that case, we will use the git rm command with the --cached option. If we also want to remove it from the working directory, we will skip the --cached option from that command.

$ git rm --cached <file-name>

Here we are almost done with the file recovering process. The irrelevant file has been deleted from the staging area, and all the other changes are staged. Now, we have to commit these changes using the command git commit to the branch. We now have got our commit back, but this doesn’t include the files that are not needed.

$ git commit -m "commit message"
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Commit