Home Software Engineering How To Remove Files From Git Commit

How To Remove Files From Git Commit

by schkn

As a developer, you often stage files for commit just to realize that you may need to remove them later on.

Git works with three main spaces : the workspace (where you edit your files), the index (where you stage your files for commits) and the repository.

Now, if you staged your files for commit, you may want to remove files from your commit in order to perform more modifications.

In this tutorial, we are going to learn how you can effectively remove files from your Git commits easily, without losing your modifications.

Remove Files From Git Commit

In order to remove some files from a Git commit, use the “git reset” command with the “–soft” option and specify the commit before HEAD.

$ git reset --soft HEAD~1

When running this command, you will be presented with the files from the most recent commit (HEAD) and you will be able to commit them.

Now that your files are in the staging area, you can remove them (or unstage them) using the “git reset” command again.

$ git reset HEAD <file>

Note : this time, you are resetting from HEAD as you simply want to exclude files from your staging area

If you are simply not interested in this file anymore, you can use the “git rm” command in order to delete the file from the index (also called the staging area).

$ git rm --cached <file>

When you are done with the modifications, you can simply commit your changes again with the “–amend” option.

$ git commit --amend

To verify that the files were correctly removed from the repository, you can run the “git ls-files” command and check that the file does not appear in the file (if it was a new one of course)

$ git ls-files

<file1>
<file2>

Remove File From Commit using Git Restore

Since Git 2.23, there is a new way to remove files from commit, but you will have to make sure that you are using a Git version greater or equal than 2.23.

$ git --version

git version 2.24.1

Note : Git 2.23 was released in August 2019 and you may not have this version already available on your computer.

To install newer versions of Git, you can check this tutorial.

To remove files from commits, use the “git restore” command, specify the source using the “–source” option and the file to be removed from the repository.

For example, in order to remove the file named “myfile” from the HEAD, you would write the following command

$ git restore --source=HEAD^ --staged  -- <file>

As an example, let’s pretend that you edited a file in your most recent commit on your “master” branch.

The file is correctly committed but you want to remove it from your Git repository.

To remove your file from the Git repository, you want first to restore it.

$ git restore --source=HEAD^ --staged  -- newfile

$ git status

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   newfile

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   newfile

As you can see, your file is back to the staging area.

From there, you have two choices, you can choose to edit your file in order to re-commit it again, or to simply delete it from your Git repository.

Remove File from Git Repository

In this section, we are going to describe the steps in order to remove the file from your Git repository.

First, you need to unstage your file as you won’t be able to remove it if it is staged.

To unstage a file, use the “git reset” command and specify the HEAD as source.

$ git reset HEAD newfile

When your file is correctly unstaged, use the “git rm” command with the “–cached” option in order to remove this file from the Git index (this won’t delete the file on disk)

$ git rm --cached newfile

rm 'newfile'

Now if you check the repository status, you will be able to see that Git staged a deletion commit.

$ git status

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    newfile

Now that your file is staged, simply use the “git commit” with the “–amend” option in order to amend the most recent commit from your repository.

$ git commit --amend

[master 90f8bb1] Commit from HEAD
 Date: Fri Dec 20 03:29:50 2019 -0500
 1 file changed, 2 deletions(-)
 delete mode 100644 newfile

As you can see, this won’t create a new commit but it will essentially modify the most recent commit in order to include your changes.

Remove Specific File from Git Commit

In some cases, you don’t want all the files to be staged again : you only one to modify one very specific file of your repository.

In order to remove a specific file from a Git commit, use the “git reset” command with the “–soft” option, specify the commit before HEAD and the file that you want to remove.

$ git reset HEAD^ -- <file>

When you are done with the modifications, your file will be back in the staging area.

First, you can choose to remove the file from the staging area by using the “git reset” command and specify that you want to reset from the HEAD.

$ git reset HEAD <file>

Note : it does not mean that you will lose the changes on this file, just that the file will be removed from the staging area.

If you want to completely remove the file from the index, you will have to use the “git rm” command with the “–cached” option.

$ git rm --cached <file>

In order to make sure that your file was correctly removed from the staging area, use the “git ls-files” command to list files that belong to the index.

$ git ls-files

When you are completely done with your modifications, you can amend the commit you removed the files from by using the “git commit” command with the “–amend” option.

$ git commit --amend

Conclusion

In this tutorial, you learnt how you can easily remove files from commit using the “git reset” command.

You also learnt that it is possible to remove files on newer versions of Git using the “git restore” command but you will have to make sure that you have the most recent Git version (>= 2.23)

If you are interested in Git or in software engineering, we have a complete section dedicated to it on the website, so make sure to check it out!

git remove files from commit

You may also like

7 comments

Kani December 13, 2021 - 1:23 pm

Hey!
Thanks a lot for your explanation! I just need to point out a slight amendment to the process described in “Remove Files From Git Commit”: After reset –soft and removing the files, you recommend using git commit –amend. But this will add the remaining changes to the PREVIOUS commit (since we used reset –soft earlier), NOT amend the latest commit we intended to amend.
I solved the problem by following your recommendations up to this step, then

* stashing the result (git stash) (meaning the changes I want to keep without the unwanted files), next
* git reset –hard to the same commit we used for reset –soft, and finally
* git stash –apply and git commit.

So I created a new commit instead of the old one including the unwanted files. This is obviously only applicable, if the faulty commit was the latest one.

Hope this helps!
Greetings, Kani

Reply
Kani December 13, 2021 - 1:46 pm

Here is a simplified version for my recommendation above:
Follow the instructions for “How To Remove Files From Git Commit” until you reach “git commit –amend”. Instead of doing that (it would as mentioned add the new changes to the originally second to last commit, instead of amending the latest commit we intend to fix). Then just use “git commit” as usual and create a new commit.
Kani

Reply
ct February 17, 2022 - 5:16 pm

Need git push as well?

Reply
FiruzzzZ August 24, 2022 - 1:39 pm

the step from git commit –amend is wrong, if you do this, you will be amending to the current “commit” which is not the one that you undid with “reset –soft HEAD~1”. To re-create the commit with the previous message, you must use “git commit -c ORIG_HEAD”

Reply
Alex August 24, 2022 - 2:29 pm

Thank you, this was useful, the git reset –soft HEAD~1 helped me.

Reply
Aida January 6, 2023 - 7:34 pm

Thanks that was awesome

Reply
Anonymous April 4, 2023 - 1:07 pm

Thanks a lot for this helpful information.

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.