git clone Explained: How to Clone a Repository to Your Local Machine
- Home
- git clone Explained: How to Clone a Repository to Your Local Machine
git clone – How to Clone a Repository to Your Local MachineWhen onboarding onto a new project or working offline with source code hosted on GitHub, GitLab, or Bitbucket, the very first step is cloning the repository to your local machine. The git clone command does more than just download the latest files—it copies the full commit history, branches, and automatically sets up a connection to the remote repository (origin).
git clonegit clone <repository-url> [target-directory]
<repository-url>: The URL of the remote repository (most commonly HTTPS or SSH).
[target-directory] (optional): The local folder name where the repo will be saved. If omitted, Git creates a folder matching the repository’s name.
Clone a repository into the default directory:
git clone https://github.com/dcsofttech/sample-project.git
Clone a repository into a custom folder named my-app:
git clone https://github.com/dcsofttech/sample-project.git my-app
| Criteria | HTTPS | SSH |
| Setup | Fast—copy and paste the URL directly | Requires generating an SSH key pair (ssh-keygen) and adding the Public Key to your Git provider |
Authentication on push/pull |
Uses a Personal Access Token (PAT) or Credential Manager | Seamless and automatic via SSH key |
| Best For | Beginners, quick clones of public repos, short-lived CI/CD environments | Daily development on personal or dedicated work machines |
DCSoftTech Tip: If you work regularly with multiple private repositories, configure your SSH key once to avoid repeated token prompts and authentication errors.
git clone Flags in Real-World WorkflowsYou do not always need to pull down the entire commit history from day one:
Clone a specific branch (-b / --branch):
git clone -b develop https://github.com/dcsofttech/sample-project.git
Shallow clone (--depth): Downloads only the most recent commits—ideal for large repositories with long histories.
git clone --depth 1 https://github.com/dcsofttech/sample-project.git
Clone with submodules (--recurse-submodules): Automatically initializes and updates nested repositories inside the project.
git clone --recurse-submodules https://github.com/dcsofttech/sample-project.git
Once git clone finishes, run through these quick steps before writing code:
Navigate into the project directory:
cd sample-project
Verify your repository status and remote URLs:
git status
git remote -v
Create a dedicated working branch (avoid committing directly to main or master):
git checkout -b feature/your-feature-name
Permission denied (publickey): Your SSH public key has not been added to your GitHub/GitLab account, or the SSH agent is not running. Test your connection with ssh -T git@github.com, or switch to the HTTPS URL.
Repository not found or wrong account credentials: Double-check your repository access permissions, verify the URL spelling, or clear cached credentials in Windows Credential Manager / macOS Keychain.
destination path already exists and is not an empty directory: Git blocks cloning into non-empty folders to prevent overwriting files. Specify a different target directory name or empty the folder first.
Mastering git clone sets a clean foundation for your daily Git workflow. Knowing when to use HTTPS vs. SSH—alongside practical flags like --branch, --depth, and --recurse-submodules—keeps your local setup fast, organized, and error-free from the start.