Creating a Git Repository
This chapter details how to create a Git repository, including creating a new one from scratch and cloning an existing one.
Ways to Create a Repository
Git repositories can be created in the following ways:
- Initialize a new repository -
git init - Clone an existing repository -
git clone - Create from an existing project
- Create using a template
Method 1: Initialize a New Repository
Basic Initialization
Output:
This creates a .git directory containing all necessary files for the Git repository.
Initialize with Specific Branch Name
Create a Bare Repository
A Bare Repository has no working directory and is usually used as a remote repository:
Bare repositories directly contain Git data without working directory files.
Basic Setup After Initialization
Method 2: Clone Existing Repository
Basic Clone Operations
Cloning with Different Protocols
HTTPS Clone
SSH Clone
Local Clone
Shallow Clone
For large repositories, you can use shallow clone to fetch only recent history:
Partial Clone
Git 2.19+ supports partial clones to download only needed objects: