๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Cloning a Git Repository - git clone


Introduction

  • git clone command is used to create a copy of an existing Git repository from a remote server (like GitHub, GitLab) to your local machine.
  • It initializes a new local repository and automatically sets up a remote connection to the source repository.
  • Features of git clone :
    • It will download all files, commit history, branches and tags from the remote repository.
    • It will creates a new folder with the same name as the repository (unless you specify a different name).
    • It will lnks your local repository to the remote source, making it easy to pull updates.

How to use git clone ?

  1. Open the Git terminal (or command prompt) and navigate to the project folder where your Git repository is initialized.
  2. Make sure that:
    • You have the remote repository URL (for example, from GitHub). In previous tutorial we have pushed one repository on GitHub.
    • Git Clone Url
  3. Now run the git clone command:
    • Syntax : git clone <remote-repo-URL>
    • Example : git clone https://github.com/SmartProgrammingCoders/GitDemo.git
  • The command will show the following:
    Git Clone Command
  • Here, we have cloned the GitHub repository from the URL https://github.com/SmartProgrammingCoders/GitDemo.git to our local machine using the git clone command.
  • This creates a local copy of the repository, allowing us to work on the project locally while staying connected to the remote repository on GitHub.

Different ways to use git clone ?

  1. Cloning Basic Repo:
    • Syntax:
      • git clone <repository-url>
    • Example:
      • Clone from Online Repo: git clone https://github.com/SmartProgrammingCoders/GitDemo.git
      • Clone from Local Repo (less preferred): git clone "C:\Users\Deepak Panwar\Desktop\gitdemo"
  2. Cloning into a Specific Directory:
    • Syntax:
      • git clone <repository-url> <custom-directory-name>
    • Example:
      • git clone https://github.com/SmartProgrammingCoders/GitDemo.git my-local-repo
  3. Cloning into a Specific Directory:
    • Syntax:
      • git clone <repository-url> <custom-directory-name>
    • Example:
      • git clone https://github.com/username/repo-name.git my-local-repo
  4. Cloning with Specific Branch:
    • By default, git clone fetches the default branch (usually main or master).
    • To clone a specific branch, syntax is:
      • git clone -b <branch-name> <repository-url>
    • Example:
      • git clone -b develop https://github.com/username/repo-name.git
  5. Cloning with Limited History:
    • If you want to save time and bandwidth, clone only recent commits:
    • Syntax:
      • git clone --depth <number> <repository-url>
    • Example:
      • git clone --depth 1 https://github.com/username/repo-name.git (clones only the latest commit)
  6. Cloning via SSH:
    • If you have SSH access configured, you can use the SSH URL:
    • Example:
      • git clone git@github.com:username/repo-name.git
    • Faster and more secure for private repositories.