How to Remove Git Init
In this tutorial, we will learn about undoing the effect of the git init
command in Git.
The git init
command creates a new empty Git repository.
When used with an existing project directory that is not yet versioned, the git init
command transforms the current directory into a Git repository.
Sometimes, we may wish to remove the project directory from the Git versioning system.
We will now illustrate this with an example.
Undoing git init
for a Directory in Git
When setting up a repository in Git, we use the git init
command.
The git init
command creates a directory called .git
(a hidden directory) in the project directory. This directory is used to store all the objects and refs that Git uses. These objects and refs created and stored in the .git
folder are used by Git for the history of the project directory.
The existence of the .git
directory is one that separates a normal directory from a Git repository.
We can see in the following the typical contents of a .git
folder of a Git repository.
$ ls .git
branches config FETCH_HEAD HEAD index logs ORIG_HEAD
COMMIT_EDITMSG description gitk.cache hooks info objects refs
As mentioned earlier, the git init
command creates the .git
directory and its sub-directories. These sub-directories are the metadata of the repository.
The HEAD
file is created by the git init
command, which points to the currently checked-out commit.
Thus, to undo the effect of the git init
command means to remove the project directory from the versioning system of the Git; basically, the project directory will no longer remain a Git repository.
To achieve this, we need to delete the .git
folder from the project directory.
$ rm -rf .git
After deleting the .git
folder, the project folder is no longer tracked by the Git versioning system.
Whatever changes done so far will remain, and nothing will be affected.
Of course, as the project directory is no longer a Git repository, we now cannot fetch the new changes pushed into the remote repository.