Init

`Git Init ’

Git local flow

Git local flow (Source:s3.ap-south-1.amazonaws.com)

Introduction

In the workflow when using Git in local, the first step that must be taken is to initialize Git in the folder, so that Git can record any changes that occur in the folder. The command is:

git init

git init can turn any directory(📁 folder) into a Git repository. Let’s study git init further.

What Does git init Do?

To start a new project and initialize a repository with Git, we can use the git init command. Git creates a hidden directory called .git and it is this directory that separates the regular folder from the Git repository.

How to Use git init

Common usage and options for git init

  • git init: Converts the current directory to a Git repository.
  • git init <directory>: Creates a folder and turns it into a Git repository.

We can see all the git init options in the [git-scm] documentation(https://git-scm.com/docs/git-init).

Example git init

It’s possible that we already have an application project locally, but it’s not yet integrated with Git, so git init is the right command. This is only run once, even if other collaborators want to contribute to our project. For example, consider the Git command below:

Git init

Git init (Source: amazonaws.com)

As seen above, to check whether folder my-cool-repo has become a Git repository or not, you can use the git status command. Once we see that the folder is not a Git repository, we can use the git init command.

Now folder my-cool-repo has become a Git repository. So any changes in that folder will be recorded by Git. Next, we can carry out activities using Git, namely:

  • git add: Selects files and adds them to the staging area in preparation for versioning.
  • git commit: Records files in the staging area permanently as version history.
  • git push: Upload all local commits to the server.

The following is the flow of Git activity:

Git init flow

Git init flow (Source: miro.medium.com)

Git init Existing Folder

By default git init is to change the current directory to a Git repository. To make an existing project a Git repository, navigate to the targeted root directory. Then, run git init.

Alternatively, we can create a new repository in the directory at the current path. Use git init <directory> and specify which directory to turn into a Git repository, for example: git init ./Downloads/my-cool-repo

Git init Gone Wrong

Running git init in the wrong place will create an unexpected repository. What does it mean? 🤔 Nested git init is the most common example:

  📁repository (git init)
    |_ 📁.git
    |_ 📄file
    |_ 📄file
    |_ 📁repository (git init)
      |_ 📁.git
      |_ 📄file
      |_ 📁repository (git init) <- YOU ARE HERE!
         |_ 📁.git
         |_ 📄file

Doing the above will duplicate version tracking information on each sub-directory and make the root .git unable to track changes to the sub-directory because it has its own .git. Duplicating version information in sub-directories will create abnormal situations that will result in inconsistencies sooner or later.

To fix this, we have to track down which directories are unwanted repositories. Use git status to see if the current directory is tracked by Git. If so, we can run ls -al and look for hidden .git directories.

If you don’t see it, navigate one level up in the directory structure with cd ... Then use git status again in combination with ls -al. Repeat until you find the .git directory you want to delete

Once you’ve found the .git directory, and are sure that you don’t want it to be a Git repository, use rm -rf .git. This will delete the .git directory, effectively uninitializing the unwanted repository.


But what if there is already a structure like that, but we have already done several history commits in it and don’t want to delete it? 🤔 The solution is that we can move the existing nested directory .git to a separate directory and delete the previous repository directory. Examples are as follows:

📁repository (git init)
   |_ 📁.git
   |_ 📄file
      📁repository2 (git init)
      |_ 📁.git
      |_ 📄file

become

📁repository (git init)
   |_ 📁.git
   |_ 📄file
      📁repository2 (git init) <- [REMOVE]
      |_ 📁.git <- [REMOVE]
      |_ 📄file <- [REMOVE]

📁repository2 (git init)
   |_ 📁.git
   |_ 📄file

The method above can be used as a corrective step.

It’s best to know the directory structure we will use when we initialize a Git repository project. Avoid nested directories as they can be very confusing.

Back to top