Git - Setting up Git repository

Git

Share

In this tutorial we will learn to setup Git repository.

There are two ways we can setup a Git repository. The first scenario is to create a brand new Git repository from scratch. This is common if we are starting a new project. The second scenario is to clone an exisitng repository. This is common when a new developer is added to a project and she pulls the Git repository in her local machine.

Creating a brand new repository

We use the git init command when we want to create a new repository (repo).

We can either run this command inside the project folder or add the path of the project folder as an option like git init path/to/project folder.

This command will create a .git directory in the present working directory and will also create a master branch. More on branches in the later tutorial.

Creating a new project folder.

$ mkdir git-project
$ cd git-project

Initializing Git.

git-project $ git init
Initialized empty Git repository in /Users/yusufshakeel/Sites/git-project/.git/ 

The above command created a .git directory in the project folder git-project and also created a master branch.

We can check the current branch by running the git status command.

$ git status
On branch master

Cloning an existing repository

To clone an exisiting Git repository we use the git clone command.

This command will clone the repository in the current working directory by default. But we can clone in a specific directory by passing the path of the required directory as an option.

Cloning an exisiting Git repository in the current working directory using git clone [url] command.

$ git clone https://github.com/yusufshakeel/dyCalendarJS
Cloning into 'dyCalendarJS'...
remote: Counting objects: 213, done.
remote: Total 213 (delta 0), reused 0 (delta 0), pack-reused 213
Receiving objects: 100% (213/213), 72.01 KiB | 35.00 KiB/s, done.
Resolving deltas: 100% (122/122), done.

Cloning an exisiting Git repository in a given local directory using git clone [url] /path/to/directory command.

$ git clone https://github.com/yusufshakeel/dyCalendarJS repo/
Cloning into 'repo'...
remote: Counting objects: 213, done.
remote: Total 213 (delta 0), reused 0 (delta 0), pack-reused 213
Receiving objects: 100% (213/213), 72.01 KiB | 33.00 KiB/s, done.
Resolving deltas: 100% (122/122), done.

The above command will clone the Git repository inside the repo directory.

$ ls -la repo
total 48
drwxr-xr-x   9 yusufshakeel  staff   306 Sep 12 17:47 .
drwxr-xr-x  29 yusufshakeel  staff   986 Sep 12 17:46 ..
drwxr-xr-x  13 yusufshakeel  staff   442 Sep 12 17:47 .git
-rw-r--r--   1 yusufshakeel  staff  1070 Sep 12 17:47 LICENSE
-rw-r--r--   1 yusufshakeel  staff  1468 Sep 12 17:47 README.md
drwxr-xr-x   4 yusufshakeel  staff   136 Sep 12 17:47 css
-rw-r--r--   1 yusufshakeel  staff  8092 Sep 12 17:47 index-using-jquery.html
-rwxr-xr-x   1 yusufshakeel  staff  8027 Sep 12 17:47 index.html
drwxr-xr-x   8 yusufshakeel  staff   272 Sep 12 17:47 js