What is git init command- git init bare?

Git init command creates a new git repository. It is an initial command from the list of git commands.  This tutorial includes, how to create a git repository in git using Linux. Examples in this tutorial are based on CentOs and assume that git is already installed on the machine.

Type of git repository?

Now you have decided to create a repository for you and your team?  What options should you use in git init command or git init is all you need only?

git init command creates a repository with actual files. The directory created is the working directory. That includes a  full working tree.  The directory, have .git directory folder for all revision history. All files present in the working directory are as checkout copies of all files. This is suitable for you if you are the only user of the repository. I guess here not as you have a team. Want git repository to be accessible to all developers.

Another type of repository is a bare repository. A bare git repository is created by using –bare option in git init command. A bare repository can be shared among multiple developers over the network. Developers need to know the URL and login credentials for the repository.

You want to avail repository to all developers in your team, so creating a bare repository is good for you and team.

Git repository server location:

From where the git repository will be accessed? From inside office premises (over LAN)? Or from anywhere? If from anywhere, then the repository hosting server should have public IP or you can also choose existing platforms GitHub, bitbucket, etc.

If due to limitation or any other factor, if the requirement is, give access from office premises/LAN only. Then a machine on LAN (Local Area Network, have an internal IP address ) is a suitable option.

In this tutorial, the git repository will be created on a Linux machine in LAN. The IP address of the machine which will host the git repository is 192.168.1.159. The git user is the root user of the Linux machine. You can create a new user for accessing the git repository. For simplicity, the user is a root user in this example.

Create git bare repository:

After ssh on machine 192.168.1.159

[root@CentOS_6_64-159 ~]# cd /
[root@CentOS_6_64-159 /]#
[root@CentOS_6_64-159 /]# mkdir myrepository.git
[root@CentOS_6_64-159 /]# cd myrepository.git/
[root@CentOS_6_64-159 myrepository.git]#
[root@CentOS_6_64-159 myrepository.git]# git –bare init

The Above will create a bare repository, now assign the bare repository a working tree.

  • Do SSH  on another machine (192.168.1.160) in the same LAN.
  • Will create a local repository (git init command),
  • Add a new file (git add command) and commit the new file (git commit).
  • Push the code changes to the central repository.
[root@CentOS_6_64-160 gitdevelopment]# git init
[root@CentOS_6_64-160 gitdevelopment]# git add firstFile.c
[root@CentOS_6_64-160 gitdevelopment]# git commit -m “First Commit to Bare Repository”
[root@CentOS_6_64-160 gitdevelopment]# git remote add origin root@192.168.1.159:/myrepository.git
[root@CentOS_6_64-160 gitdevelopment]# git push origin master
Note: It will prompt for the password, enter the password for user root on machine  192.168.1.159

The remote repository is ready with the working tree. Other users will clone from machine 192.168.1.159  to machine 192.168.1.155, in the same LAN. Ssh to a machine. The git clone command is in git, so clone a git repository on a local machine.

[root@CentOS_6_64-155 gitexample]# git clone root@192.168.1.159:/myrepository.git
Note: It will prompt for the password, enter the password for user root on machine  192.168.1.159[root@CentOS_6_64-155 gitexample]# cd myrepository/Note: You can see the file, [root@CentOS_6_64-155 myrepository]# ls
firstFile.c
[root@CentOS_6_64-155 myrepository]#
Note: Now add a new file secondFile.c, first create a file with vim.[root@CentOS_6_64-155 myrepository]# git add secondFile.c
[root@CentOS_6_64-155 myrepository]# git commit -m “Added Second File”
[root@CentOS_6_64-155 myrepository]# git push
Note: It will prompt for the password, enter the password for user root on machine  192.168.1.159

 

Great:), git repository is ready. Invite developers to work on this.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top