- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove a committed file from the Git repository?
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.
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 −
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
- How to ignore a previously committed file in Git repository?
- How to Remove Newline Characters from a text File?
- C program to remove a line from the file
- How to Remove Empty Lines from a File on ubuntu
- What are the steps to rename a file in Git?
- How to remove a file using Python?
- How to create a GitHub repository
- How to clone a GitHub repository?
- How to copy Docker images from one host to another without using a repository?
- How to add collaborators to a repository in GitHub?
- How to tag a commit in git?
- Java Program to remove file information from a filename returning only its path component
- Java Program to remove path information from a filename returning only its file component
- How to abort a merge conflict in Git?
- How to Remove Scratches from a Car?