Fully Delete a Git Repository

  1. Remove Git Directory Using GUI
  2. Rename Git Directory Using Command Line

Initializing git repository using git init or cloning the git repo from GitHub also comes with a .git directory containing different directories/files related to the project.

Locally deleting a git repository sounds like one of the easiest things to accomplish; however, since the .git folder is initially hidden, the removal of the folder varies according to a different OS.

This article will use the command line and GUI to delete the repository and reinitialize it.

Remove Git Directory Using GUI

In Windows, if using a graphical user interface, we first need to enable the show hidden files option. This can be done by following this article. After seeing the hidden folder, we can delete the .git folder and then again reinitialize if needed.

Rename Git Directory Using Command Line

With the usage of the command line, the task is rather simpler. First, we need to navigate to the repository which was cloned or where we had initialized git. We can do that using:

cd <path_to_the_folder>

Once inside the folder, we can list the visible as well as hidden folders:

ls -la

For example, the output is similar to the one below.

total 24
drwxr-xr-x  4 mario mario  4096 Nov 21 00:02 .
drwxrwxrwt 24 root    root    12288 Nov 21 00:02 ..
drwxr-xr-x  2 mario mario  4096 Nov 21 00:02 frontend
drwxr-xr-x  7 mario mario  4096 Nov 21 00:02 .git

Here, in the last line, we can see the folder named .git, we ought to remove the folder by using:

rm -fr .git

The flags -f is for forcefully removing the nonexistent files and arguments without prompting (be careful with its usage).

The flag -r is for recursively deleting each directory and its contents.

We can check if the directory is initialized with git or not by using the git status command.

It will throw the following error if the folder is not initialized with git.

fatal: not a git repository (or any of the parent directories): .git

Then, we can reinitialize the folder with the git init command.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.