In this Article, we will learn How to use git init command in git
Total Views: 1070
In this article, we will learn How to use the git init command in git. We can create a git repository in two ways. One is to initialize a git repository in a directory (that can be empty or having an existing project) and another one is to clone an already existing git repository. We will see How to clone a git repository in the next part of this tutorial series.
Let’s go to the directory where we want to initialize the git repository. Right-click in that folder and click on Git Bash here in order to open the Git bash in this directory.
Run “git init” command through Git Bash. Make sure you are at the top level of the project tree you want to put under the git.
It will create a .git subdirectory that contains the git repository skeleton. The same command needs to be run in Git Bash if you already have a project which needs to be initialized as a Git repository. A .git subdirectory as shown in the below image will be created after successful execution of the command.
Another way is to pass the directory location with the command, “git init <directory_location>” where you want to initialize the Git repository.
After successfully executing the command, you can see that a .git directory folder is created in the location passed in the above command.
Point to remember: Never edit the files inside the. git directly. It may corrupt the whole directory.
Bare repository: Bare repository is much similar to Non-Bare repository but no one can commit code in the bare repository. “git init –bare” command is used to initialize the bare repository.
Bare repository (initialize git with bare flag) contains no working or checkout copy of the source code. The shared or central repository should be created as a Bare repository. –bare flags create a repository that doesn’t have the working copy.
Content of .Git Repository: Git repository contains several files and folders which have a specific role in managing the git repository.
1. Hooks: Hooks folder contains scripts that can be called automatically by git when some event occurs.
2. Info: Info folder contains a file exclude which is used for excluding files from the repository. This file is not shared with others whereas .gitignore is shared by others.
3. Objects: This is the database directory containing information about all the files, revisions, tags, etc.
4. Refs: Here git stores the information about the branches, tags, etc.
5. Config: contains the local configuration settings applied to the local project only.
6. Description: The description contains a short description of the repository.
7. Head: Head refers to the project branch or revision on which you are working on.