git init

Create a new Repository with Git Init 

Before starting any code development, you may need a new repository to track and collaborate with the development. Here comes the command git init. It is an initial command from the list of git commands. This tutorial includes how to create a git repository using the Linux command line Interface. Examples in this tutorial are based on CentOs7 and assume that git is already installed on the machine.

Steps for creating a new repository from start:

  • Create a new directory structure – will contains the project files
  • After moving into the directory execute the git init – to initialize the repository.
  • Develop the initial code (maybe a simple readme file) – the initial files.
  • Use git add – adds new files for next check-in
  • Type git commit – sends new files to the local repository

How to create a new repository with an existing project?

For an existing project. The steps are similar to above, the difference is that we can create a ignore list for unwanted or temporary files. Following are the steps

  • Move to project folder
  • Type git init
  • Add all the file – git add command. May also use git add .gitignore
  • Execute git commit.

Types of git repositories (Local and Bare)?

Now you know theoretically, how to create a repository for you and your team?  But a default is not suitable for you. In this section, we will describe the types of git repositories. There are two types, one is local and another is shared (or bare).

Local or personal :

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.

Shared or bare:

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 the 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:

The following are the actual Linux steps for creating a git repository, that can be accessed by multiple developers.

After ssh on machine 192.168.1.159

[[email protected]_6_64-159 ~]# cd /
[[email protected]_6_64-159 /]#
[[email protected]_6_64-159 /]# mkdir myrepository.git
[[email protected]_6_64-159 /]# cd myrepository.git/
[[email protected]_6_64-159 myrepository.git]#
[[email protected]_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.
[[email protected]_6_64-160 gitdevelopment]# git init
[[email protected]_6_64-160 gitdevelopment]# git add firstFile.c
[[email protected]_6_64-160 gitdevelopment]# git commit -m "First Commit to Bare Repository"
[[email protected]_6_64-160 gitdevelopment]# git remote add origin [email protected]:/myrepository.git
[[email protected]_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.

[[email protected]_6_64-155 gitexample]# git clone [email protected]:/myrepository.git
Note: It will prompt for the password, enter the password for user root on machine  192.168.1.159[[email protected]_6_64-155 gitexample]# cd myrepository/Note: You can see the file, [[email protected]_6_64-155 myrepository]# ls
firstFile.c
[[email protected]_6_64-155 myrepository]#
Note: Now add a new file secondFile.c, first create a file with vim.[[email protected]_6_64-155 myrepository]# git add secondFile.c
[[email protected]_6_64-155 myrepository]# git commit -m "Added Second File"
[[email protected]_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