git_init_and_clone
git init and clone
Initialize new repositories or clone existing ones.
π git init
Initialize in Current Directory
git init
Creates a new Git repository in the current folder. Creates a
.gitsubdirectory.
Initialize in New Directory
git init project-name
Creates a new folder called
project-nameand initializes a Git repository inside it.
Initialize with Default Branch Name
git init -b main
Initializes repository with
mainas the default branch instead ofmaster.
Initialize Bare Repository
git init --bare
Creates a bare repository (no working directory). Used for remote/server repositories.
π What git init Creates
graph TD
A[git init] --> B[.git folder created]
B --> C[HEAD file]
B --> D[config file]
B --> E[objects folder]
B --> F[refs folder]
π₯ git clone
Clone from GitHub (HTTPS)
git clone https://github.com/user/repo.git
Downloads the repository using HTTPS. Prompts for credentials if private.
Clone from GitHub (SSH)
git clone git@github.com:user/repo.git
Downloads the repository using SSH. Requires SSH key setup.
Clone into Specific Folder
git clone https://github.com/user/repo.git my-folder
Clones repository into a folder named
my-folderinstead of repo name.
Clone into Current Directory
git clone https://github.com/user/repo.git .
Clones repository contents into current directory (must be empty).
Shallow Clone (Last Commit Only)
git clone --depth 1 https://github.com/user/repo.git
Downloads only the latest commit. Much faster for large repos.
Shallow Clone with Depth
git clone --depth 100 https://github.com/user/repo.git
Downloads only the last 100 commits.
Clone Single Branch
git clone --single-branch -b main https://github.com/user/repo.git
Clones only the
mainbranch, not all branches.
Clone with Submodules
git clone --recurse-submodules https://github.com/user/repo.git
Clones repository and all its submodules.
π Clone Flow
graph LR
A[git clone URL] --> B[Download .git data]
B --> C[Create working directory]
C --> D[Checkout default branch]
D --> E[β
Ready to work]
π‘ Tips
Use SSH for easier authentication with private repositories.
Use --depth 1 for faster cloning of large repositories.
π Related
#git #init #clone #basics