How to remove a committed file from the Git repository?

GitLinuxOpen SourceSoftware & Coding

Git allows you to delete a file from the repository using any of the following methods −

  • Using the Linux rm command

  • Using the git rm command

Scenario 1 − Use the Linux rm command

The syntax of the Linux rm command is −

$ git rm <file_name>

Let us assume that a file “file1.python” exists in the repository. Use the Linux rm command to delete the file − “file1.python”.

$ git rm file1.python

Let us first verify if the file has been removed from the working directory. This can be verified by using the Linux ls command.

$ ls

The output suggests that the file has been removed from the working directory.

dell@DESKTOP−N961NR5 MINGW64 /e/tut_repo (master)
$ Is

Let us now verify if the file has also been removed from the staging area.

Use the git command ls−files to verify this.

$ git ls−files

The output of the above code is −

file1.python

It is clear from the output that the file has not been removed from the staging area. This means changes like adding, renaming or deleting a file performed using Linux commands will not be automatically staged. Such changes should be explicitly staged using the git add command and then committed.

$ git add file1.python

Execute the ls−files command. The status of the operation can also be verified using the git status command.

$ git ls−files
$ git status

The output of the above commands is shown in the screenshot.

On branch master
Changes to be committed:
   (use “git restore --staged <file>...” to unstage)
      deleted:file1.python

The file is removed from the staging area. Let us now finally commit the change.

$ git commit -m ‘removed unused python file’

The file is permanently deleted from the repository. Refer the screenshot below for output.

[master 81e32c4] removed unused python file
1 file changed, 2 deletions (-)
delete mode 100644 file1.python

Scenario 2 − Use the git rm command

The syntax of the git rm command is

git rm <file_name>

Assuming that a file “file1.js” exists in the repository, let us use the git rm command to delete the file.

$ git rm file1.js

Let us use the git ls−files command to verify if the file has been removed from the staging area.

$ git ls−files

We can also use the git status command to verify if the file has been removed from the staging area.

$ git status

It is clear from the output that files removed using the git rm command are deleted from the working area and also from the staging area. Refer screenshot below for the output.

On branch master
Changes to be committed:
   (use “git restore −−staged <file>...” to unstage)
      deleted:file1.js

Let us now commit the change and verify the output.

$ git commit −m ‘remove unused js file’

The output suggests that the file has been permanently deleted from the repository.

[master dc7861e] removed unused js file
1 file changed, 1 deletion(−)
delete mode 100644 file1.js
raja
Published on 20-Feb-2021 08:33:56
Advertisements