Table of content

Deleting a File in Git

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:

  • Deleting a file that is not yet being tracked by Git :

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.DF-cd-ls-git-status

  • Now let's create a .txt file named as a simple.txt file by using the command notepad++ simple.txt and press enter add some dummy text then save and exit, here I added as This file won't last long.
  • Now let's do an 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 commandgit 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 rm simple.txt

  • If you do an ls, our file simple.txt has been deleted and check git status, it shows that your working directory is clean.
  • Since the file has not been tracked and we need not worry about committing any changes to the working directory.
  • Deleting File that is being tracked by Git :
  • First list out all the files which has been tracked by Git by using git ls-files to list all the files.DF-git-ls-files


  • So here we have a newfile.txt file in the list and do an ls you can see newfile.txt in our directory.
  • So the only way to delete this file, which is being tracked by Git is to use Gits rm command asgit rm newfile.txtand press enter. Git replies that it issued rm command.
  • If you do an ls command, you can see that the newfile.txt is no longer listed in the directory.
  • If you do 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.
  • Let's commit inline, git commit -m "deleting the new file" and press enter so that the deletion has recorded in the Git repository.
  • If you do a git status, you will see a clean working directory.
  • Backing out a staged deletion:
  • Let's do an ls you can see the hipster.txt file.
  • Now pass git ls-files command, it will list the hipster.txt file, so the git is tracking this file.
  • Delete this file by using rm command as git rm hipdster.txt Git returns that the file has been removed and if we do an ls the file hipster.txt has no longer listed here
  • So git statusshows that the deletion is currently staged, that means the file has not been deleted permanently.
  • Let's unstage this file first by using git reset HEAD command as git reset HEAD hipster.txt
  • So Git confirms that it has unstaged the deletion.
  • However, if we do an ls the hipster.txt file is still in our working directory.
  • If we do a 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.

  • Deleting a File that is Tracking by Git :

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.

  • If we do a 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.

  • So will commit a message, git commit -m "deleted hipster.txt file"
  • Now do a git status, you can see that your working directory is clean.
  • Deleting a File outside the Git :

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.

As 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.
  • So in order to stage the deletion of all those files at once am going to issue git add command with -A parameter as git add -A
  • If you do a git status, the deletion of all these files have been let's do commit message, git commit -m "deleting level1 and all his children"
  • Git commit command confirms that we have deleted all the files and if you do git status, it tells us that we are in a clean working directory.

Create a GitHub Account

    0 results
    Comment / Suggestion Section
    Point our Mistakes and Post Your Suggestions