In some working situations, developers add files or stages for commit on git repo. For better performance, you will need to remove files from Git commit to do more changes.
Git runs with three main spaces, they are the workspace (where you edit your files), the index (where you stage your files for commits), and the repository.
Do Check: GIT Commands
Let’s check this short & ultimate tutorial, to understand and learn how effectively & easily you can remove files from your Git commits, without losing your modifications.
- Remove Files From Git Commit or Staging Area
- Remove Single File from Committed Area
- Remove File From Commit using Git Restore
- Remove File from Git Repository
- Remove Specific File from Git Commit
Remove Files From Git Commit or Staging Area
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>
- How To Unstage Files on Git | Different Ways to Unstage Files in Git
- How To Git Reset to HEAD | What is Git HEAD? | How to Reset Git Head to Latest Commit
- How To Delete File on Git | Removing Files from Git Repository using git rm Command
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 Single File from Committed Area
Removing file from the committed area needs 3 commands to be run, they are as follows-
git reset --soft HEAD^1
This command will undo the latest commit. Once you do git status, you will see files in the staging area. Now, we can easily remove it from the staging area, as mentioned in the previous point.
git rm --cached <file-name>
By performing the above command, the file will seem in the untracked file section. Now, we removed the single file, also you can commit back those remaining files by running the below command. So, let’s do it:
git commit -m "<your-message>"
Do Check: How To Delete File on Git
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 in 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 the 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 learned how you can easily remove files from commit using the “git reset” command. You also learned 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).