Deleting a file in Git using the different Git commands and Committing the changes to the local repository.
Deleting a file contains different stages like deleting the untracked file, deleting a tracked file and also backing out the staged deletion and also deleting a file which has been tracking by Git itself but without using Git to delete that file.
Let's start with a Demo as shown below with examples:
Open the Git bash terminal and check whether you are in your project folders directory or not. You can see a starter-web-1 folder,
cd into it and do an
ls, you can see all your files inside the Git repository. Check for
git status, Git tells you that, you have a clean working directory.
notepad++ simple.txtand press enter add some dummy text then save and exit, here I added as This file won't last long.
ls, you can see a simple.txt file in Git repository and if you do
git status, we can see that we have an untracked file.
So, here am going to show you how to delete a file which has not been tracked by Git. So we use Git rm command
git rm simple.txt, we will use this command for removing the file and specify simple.txt.Git will throw an error that the file simple.txt doesn't match any file, this is because this file has not tracked by Git.
Hence, we will use an operating system facility to delete the file directly. Here am using bash command rm with the file name as
ls, our file simple.txt has been deleted and check
git status, it shows that your working directory is clean.
git ls-filesto list all the files.
lsyou can see newfile.txt in our directory.
git rm newfile.txtand press enter. Git replies that it issued rm command.
lscommand, you can see that the newfile.txt is no longer listed in the directory.
git status, it tells, Git has staged the fact that the deletion is going to take place.The deletion is not permanent yet but we need to move forward by committing a change that is been staged.
git commit -m "deleting the new file"and press enter so that the deletion has recorded in the Git repository.
git status, you will see a clean working directory.
lsyou can see the hipster.txt file.
git ls-filescommand, it will list the hipster.txt file, so the git is tracking this file.
git rm hipdster.txtGit returns that the file has been removed and if we do an
lsthe file hipster.txt has no longer listed here
git statusshows that the deletion is currently staged, that means the file has not been deleted permanently.
git reset HEAD hipster.txt
lsthe hipster.txt file is still in our working directory.
git status, it tells us that, the file has been deleted and its currently being tracked by Git.
The command "reset HEAD" specifying the file hipster.txt is missing from the working directory. just unstaging the file doesn't mean that it will restore the file back to file system.
And hence we need to discard the changes we have made to the working directory by using git checkout command as
git checkout -- hipster.txt and press enter.
To confirm that, let's do as
ls, Git will show you that the hipster.txt file is back in our working directory. If you do
git status, it tells us, we are on the clear working directory.
In this example, you can delete a file using a command line or by using a file manager, I will use bash command instead of git command to delete the file.
So let's do that
rm hipster.txt and press enter and do an
ls, the hipster.txt file has no longer listed in the working directory.
git status, it says that the hipster.txt file, which is tracking by Git is missing from the working directory.
And hence Git will replay as not staged for commit, We can fix this by using git add -A command, where -A will add and update any changes to the working directory including rename and deletion staged.
git commit -m "deleted hipster.txt file"
git status, you can see that your working directory is clean.
Let's do an
ls, you can see that there is a level1 directory. Level1 contains several files and folders recursively, so in this case, I want to remove entire file structure, so am going to use the bash command rm -rf.
rm -rf level1 where -r recursively walk into the folder structure and -f will force the deletion, Now do an
ls, you can see that level1 is no longer listed here
git status, notes all the file we have deleted.
git add -A
git status, the deletion of all these files have been staged.so let's do commit message,
git commit -m "deleting level1 and all his children"
git status, it tells us that we are in a clean working directory.