W3docs

How to Remove Files from Git Commit

This tutorial will help you get the answer to the question of removing files from git commit based on different scenarios. Also, read about used commands.

Sometimes you stage your files for commit and want to remove files from your commit to make more modifications. In this tutorial, we will show how you can effectively remove files from your Git commits discussing some scenarios.

Removing file from previous commit

Let’s discuss our first scenario. Here is what you need to do:

Removing file and keeping it on disk

If you only want to remove a file from the previous commit and keep it on disk, note that git reset --soft affects all staged changes. For precise removal of a single file, use interactive rebase:

remove a file and keep it on disk git

git rebase -i HEAD~1

In the editor, change pick to edit for the commit, save, and exit. Then remove the file and amend the commit:

remove the file and amend the commit git

git rm <file>
git commit --amend --no-edit
git rebase --continue

Resetting files

If you prefer using git reset, you can move HEAD back one commit, unstage the specific file, and commit again:

reset to previous commit git

git reset --soft HEAD~1

Then, reset the files you no longer need to remove them from the commit:

reset the files, remove files from the commit git

git reset HEAD path/to/unwanted_file

Committing

You can git commit again and even use the same commit message:

git commit

git commit -c ORIG_HEAD

Deleting file from last commit

The second scenario discusses the situation when it is your last commit, and you want to delete the file.

Deleting file from index

You can use the git rm command in order to delete the file from the staging area. The <kbd class="highlighted">--cached</kbd> option indicates the file to be removed from the cached area:

delete the file from the cached area git

git rm --cached <file>

Committing changes

When you finish the modifications, you can commit your changes again with the <kbd class="highlighted">--amend</kbd> option:

git commit changes

git commit --amend

Checking files

If done with the work, you can check whether the files are removed from the repository and the file does not appear in the new commit:

check out the files are removed from the repository git

git status
deleted: <file>

The git reset Command

The <kbd class="highlighted">git reset</kbd> command is used to undo changes. It moves the <kbd class="highlighted">HEAD</kbd> reference pointer and the current branch reference pointer.

The <kbd class="highlighted">--soft</kbd> argument updates reference pointers and stops the reset. However, it doesn’t affect the staging index and the working directory. A soft reset resets only the commit history. By default, it is invoked with <kbd class="highlighted">HEAD</kbd> as the target commit.

The git rm Command

The <kbd class="highlighted">git rm</kbd> command removes specific files or a collection of files from a git repository. The primary function of <kbd class="highlighted">git rm</kbd> is removing tracked files from the staging area (also called index). The git rm is also used for deleting files from both the staging index and the working directory. But a file can not be removed only from the working directory. However, <kbd class="highlighted">git rm</kbd> does not remove branches.

The git commit Command

The <kbd class="highlighted">git commit</kbd> command keeps all the currently staged changes. Commits are for capturing the current state of the project. Committed snapshots are regarded as secure versions of a project because Git asks before changing them. The git add command promotes changes to the project that will be then stored in a commit before executing the git commit command. The <kbd class="highlighted">--amend</kbd> option changes the last commit. Staged modifications are added to the previous commit. This argument opens the system's configured text editor and changes the previously specified commit message.