5. Understanding ” git init ” command

The git init command create an new empty Git repository. It can also be used in a large source code directory to convert that unversioned project to start using source code management. Without git init command in a directory, no other git command will work, hence git init is the first command to be used for creating git repository.

Lets try to understand what example git init command does,

 $ mkdir git-helloworld  
 $ cd git-helloworld 
 $ ls -al
total 8
drwxr-xr-x 2 devlab devlab 4096 Jun 18 21:10 .
drwxr-xr-x 4 devlab devlab 4096 Jun 18 21:10 ..

As we can see above this is an empty directory without any files in it. Now we will use “git init” command to initialise an empty git as,

 $ $ git init
Initialized empty Git repository in /home/devlab/git-helloworld/.git/

if we now check, what are the contents on directory as

 $ ls -al
drwxr-xr-x 7 devlab devlab 4096 Jun 18 21:11 .git

As we can see above, git init command created “.git” hidden directory, contents of which may look like as below,

 $ tree .git/
.git/
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

The important files of this hidden .git directory are as below,

 $ cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true

.git/config is an text file which saves the configuration settings of this specific repository

 $ cat .git/description 
Unnamed repository; edit this file 'description' to name the repository.

You can edit .git/description file, and add some title/heading which may be used by some tools to identify purpose of this source code

 $ cat .git/HEAD 
ref: refs/heads/master

.git/HEAD is another important file, which saves what is the current working branch of this git repository

Other directories mainly stores the binary database of your repository as your code starts growing.

If you want to understand, how git init is actually getting used when you create your git repositories, please refer to “3. Starting your first Git Repository”

Follow Lynxbee

Leave a Reply / Ask Question