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.
    DF-notepad-content
  • 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.
    DF-simple-txt
    DF-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
DF-gitrm-rm-ls

  • If you do an ls, our file simple.txt has been deleted and check git status, it shows that your working directory is clean.
    DF-git-status
  • 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
DF-newfile-txt

  • So here we have a newfile.txt file in the list and do an ls you can see newfile.txt in our directory.
    DF-newfile-txt-git
  • 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.
    DF-git-rm-ls
  • 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.
    DF-git-status-delete-file
  • Let's commit inline, git commit -m "deleting the new file" and press enter so that the deletion has recorded in the Git repository.
    DF-commit-git-status
  • 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.
    DF-ls
  • Now pass git ls-files command, it will list the hipster.txt file, so the git is tracking this file.
    DF-git-ls-files-hipster-txt
  • 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
    DF-rm-ls-hipster
  • So git statusshows that the deletion is currently staged, that means the file has not been deleted permanently.
    DF-gitstatus
  • Let's unstage this file first by using git reset HEAD command as git reset HEAD hipster.txt
    DF-reset-head
  • 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.
    DF-git-statusdelete-hipster

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

  • 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.
DF-ls-rm-ls

  • If we do a git status, it says that the hipster.txt file, which is tracking by Git is missing from the working directory.
    DF-delete-hipster-status

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

  • So will commit a message, git commit -m "deleted hipster.txt file"
    DF-commit-hipster-txt
  • Now do a git status, you can see that your working directory is clean.
    DF-git-commit-git-status
  • 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
DF-ls-rm-rf-ls

  • git status, notes all the file we have deleted.
    DF-git-status-delete-levelone
  • 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
    DF-git-add-gitstatus
  • If you do 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 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.
    DF-git-commit-status

Create a GitHub Account

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions