git clone copies an existing Git repository into a new directory on your local machine. It initializes a .git directory, downloads all remote branches, sets up tracking branches, and creates a remote called origin pointing back to the source.
The basic syntax:
bashgit clone [OPTIONS] REPOSITORY [DIRECTORY]
If DIRECTORY is omitted, Git uses the repository name as the directory name.
Clone over HTTPS:
bashgit clone https://github.com/user/repo.git
This creates a repo/ directory and checks out the default branch — usually main or master.
Clone over SSH:
bashgit clone git@github.com:user/repo.git
SSH is the better choice for repositories you push to regularly. It uses a key pair for authentication and does not prompt for credentials on each push or pull. Most Git hosting services no longer accept account passwords over HTTPS — if you see “Authentication failed”, use a personal access token instead.
git clone downloads ALL remote branches but checks out only one. Use git branch -r to list all remote branches after cloning.
Clone into a specific directory:
bashgit clone https://github.com/user/repo.git my-project
To clone into the current directory, use . as the target. The directory must be completely empty:
bashgit clone https://github.com/user/repo.git .
Check out a specific branch at clone time with -b:
bashgit clone -b develop https://github.com/user/repo.git
The full repository history is still downloaded — only the checked-out branch differs. To limit the download to only that branch’s history, add --single-branch:
bashgit clone -b develop --single-branch https://github.com/user/repo.git
Clone a specific tag the same way — pass the tag name with -b:
bashgit clone -b v1.0.0 https://github.com/user/repo.git
This checks out in detached HEAD state. Create a branch before making any commits.
Clone a local repository for an isolated copy without affecting the original:
bashgit clone /path/to/local/repo new-copy
A shallow clone downloads only a limited number of recent commits instead of the full history. Common in CI/CD pipelines where full history is not needed:
bashgit clone --depth 1 https://github.com/user/repo.git # latest commit onlygit clone --depth 10 https://github.com/user/repo.git # last 10 commits
To convert a shallow clone to a full clone later:
bashgit fetch --unshallow
Common errors:
~/.ssh/known_hostsgit clone URL . requires the current directory to be completely emptyUse git clone URL for a standard clone, add --single-branch -b branch to limit what is downloaded, and --depth 1 for CI pipelines. Leave a comment below if you run into any issues.
The two main tools for copying files and directories in Linux are cp and rsync. cp handles quick, local file…
The jobs command is a Bash builtin that lists all background and suspended processes belonging to the…
The Linux kernel writes messages to the kernel ring buffer throughout the boot process and…
rsync over SSH combines secure encrypted transport with fast incremental transfers. Instead of copying every file…
The pstree command in Linux displays running processes in a tree structure rather than a flat list.…
The type command in Linux shows how the current shell would interpret a name typed on the…