Introduction
The git rm
command is used in Git to remove files from the Git repository. It is short for "Git remove." When you use this command, Git removes the specified files from the working directory and the index.
Create a Working Directory “project5”, and enter that folder. We'll use the command mkdir
command to create a directory. And cd
to enter into that directory.
Now, Initialize the Workspace, and it’ll create an Empty Repository in the Workspace. So, for this, we’ll use the command git init
Create three files “a.txt”, “b.txt” and “c.txt” into the Workspace, and write something in it. We’ll use the command cat
to create the file.
So, we have created 3 files in the Workspace, and these all files are available in the Working Directory.
If we’ll check the Status, using the command git status then it tells us all three files are untracked. And no commits have been made.
So, now we’ll add all the files to the Staging Area. So, for this, we’ll use the command git add .
If we’ll again check the status then it’ll tell all the files are being Tracked and added to the Staging Area.
Now we’ll commit the files present in the Staging Area to the Local Repository. So, for that, we’ll use the command
git commit -m <Commit Message>
In our case, the command will be git commit -m "3 Files added"
And now, we'll check the log of Git. So, to check the log of Git we’ll use the command git log
It’ll show, three files have been committed to the Local Repository.
Remove Files From Both Staging Area And The Workspace
If we want to remove a file from both Staging Area and the Workspace, we’ll use the command git rm <filename>
Example:
In our case, First, we’ll check the files present in both Areas i.e the Staging Area and the Workspace.
So, if we want to check the files present in the Workspace we’ll use the command ls, it’ll lists out all the files present in the Workspace.
And, if we want to check the files present in the Staging Area then, we’’ have to list all the files that have been tracked by Git. So, for that, we’ll use the command git ls-files
it’ll list all the files present in the Staging Area.
After that check the Status of Git again using the command git status
, It’ll tell you everything is clean, and there is nothing to Commit.
In our case, three files are there in both Areas. So, now If we want to delete a file “c.txt” from both Staging Area and the Workspace, we’ll use the command git rm <filename>
In our case, it’ll be git rm c.txt
Then it’ll get removed from both the Staging Area and the Workspace.
Now, if we check the files present in both the Areas i.e the Staging Area and the Workspace. Now, the file “c.txt” will be removed from both Areas.
ls
If we’ll check the status of Git now, using the command git status
then it’ll tell one file “c.txt” has been deleted from the Staging Area.
So, the changes made in the Workspace and the Staging Area should be Committed to the Local Repository. So, we’ll commit again; this time 2 files “a.txt” and “b.txt”.
We’ll use the command
git commit -m "File "c.txt" has been deleted from both Workspace & the Staging Area"
Now, we'll check the status of Git again. It’ll tell you everything is clean, there is nothing to Commit.
git status
Remove All Files From Both Staging Area And The Workspace
If we want to remove all the files from both Staging Area and the Workspace, we’ll use the command git rm -r .
Example:
In our case, First, we’ll check the files present in both Areas i.e the Staging Area and the Workspace. So, if we want to check the files present in the Workspace we’ll use the command ls, it’ll lists out all the files present in the Workspace.
And, if we want to check the files present in the Staging Area then, we’’ have to list all the files that have been tracked by Git. So, for that, we’ll use the command git ls-files
it’ll list all the files present in the Staging Area.
After that check the Status of Git again using the command git status
It’ll tell you everything is clean, there is nothing to Commit.
In our case, two files are there in both Areas. So, now If we want to delete both the files “a.txt” &“b.txt” from both Staging Area and the Workspace, we’ll use the command git rm -r .
Then all the files will get removed from both the Staging Area and the Workspace.
Now, we’ll check the files present in both Areas i.e the Staging Area and the Workspace.
We'll use the commands:
ls
git ls-files
Now, all the files will be removed from both Areas.
If we’ll check the status of Git now, using the command git status
Then it’ll tell us two files “a.txt” and “b.txt” have been deleted from the Staging Area.
So, the changes made in the Workspace and the Staging Area should be Committed to the Local Repository. So, we’ll commit again.
We’ll use the command git commit -m "Messege"
In our case, it’ll be
git commit -m "Removed all files from Workspace and Staging Area"
The changes will be committed.
Note: If we have to delete all the files from a Workspace or the Staging Area then we should use the option “-r”. Otherwise, it’ll throw an error:
"fatal: not removing '.' recursively without -r"
Remove Files From Only Staging Area Not From The Workspace
Now, Again we’ll create two files “d.txt” and “e.txt” into the Workspace, and write something in it. We’ll use the command cat
to create and add something to the file.
So, we have created 2 files in the Workspace, and all the files are available in the Working Directory.
If we’ll check the Status, using the command git status
Then it tells us all three files are untracked. And no commits have been made.
So, now we’ll add all the files to the Staging Area. So, for this, we’ll use the command git add .
If we’ll again check the status using the command git status
Then it’ll tell all the files are being Tracked and added to the Staging Area.
Now we’ll commit the files present in the Staging Area to the Local Repository. So, for that, we’ll use the command:
git commit -m <Commit Message>
In our case, the command will be
git commit -m "Files "d.txt" and "e.txt" added"
If we want to delete a file from the Staging Area only, not from the Workspace, we’ll use the command git rm --cached <filename>
Then the file will get removed from the Staging Area only.
In our case, two files are there in both Areas. So, now If we want to delete a file “d.txt” from the Staging Area only not from the Workspace, we’ll use the command git rm --cached d.txt
Then it’ll get removed from the Staging Area only.
Now, if we verify the total number of files in the Workspace using the command ls
It’ll display two files “d.txt” and “e.txt”.
But if we verify the total number of files in the Staging Area using the command git ls-files
it’ll display only one file “d.txt”.
Now, if we’ll check the status of git, it’ll tell file “d.txt” has been deleted and now the file “d.txt” is untracked as it’s still present in the Workspace.
Remove Files From Only Workspace Not From The Staging Area
So, in this case, there is no Git command for this as a working Directory means a simple folder in the local machine. So, if we have to delete a file from the local machine then we’ll use the normal Linux command rm
for it.
Before this operation first add the files to the Staging Area (Ex: After any changes) using the command git add .
Now, 2 Files will be displayed as we added again the files from the Workspace without committing the previous changes.
Now, commit all the changes to the Local Repository using the command git commit -m <Commit Message>
In our case, the command will be
git commit -m "Files “d.txt” got deleted"
Now, to delete the file from the Workspace we’ll use the command
rm <filename>
In our case, if we want to delete the file “d.txt” then the command will be rm d.txt
Now, if we verify the total number of files in the Workspace using the command ls
, it’ll display only one file “e.txt”.
But if we verify the total number of files in the Staging Area using the command git ls-files
, it’ll display only one file “d.txt”.
So, again if we’ll check the status of the git using the command
git status
It’ll tell that the file “d.txt” has been deleted.
Now we’ll add the changes to the staging area. We’ll use the command git add .
Now, if we’ll list all the tracked files using the command git ls-files
Then the changes will be overridden to the staging area, i.e previously there were, two files “d.txt” & “e.txt”, but now there will be only one file i.e “e.txt”.