git clone | Atlassian Git Tutorial (2024)

Purpose: repo-to-repo collaboration development copy

If a project has already been set up in a central repository, the git clone command is the most common way for users to obtain a development copy. Like git init, cloning is generally a one-time operation. Once a developer has obtained a working copy, all version control operations and collaborations are managed through their local repository.

Repo-to-repo collaboration

It’s important to understand that Git’s idea of a “working copy” is very different from the working copy you get by checking out code from an SVN repository. Unlike SVN, Git makes no distinction between the working copy and the central repository—they're all full-fledged Git repositories.

This makes collaborating with Git fundamentally different than with SVN. Whereas SVN depends on the relationship between the central repository and the working copy, Git’s collaboration model is based on repository-to-repository interaction. Instead of checking a working copy into SVN’s central repository, you push or pull commits from one repository to another.

git clone | Atlassian Git Tutorial (3)

git clone | Atlassian Git Tutorial (4)

Of course, there’s nothing stopping you from giving certain Git repos special meaning. For example, by simply designating one Git repo as the “central” repository, it’s possible to replicate a centralized workflow using Git. The point is, this is accomplished through conventions rather than being hardwired into the VCS itself.

Usage

git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location. The original repository can be located on the local filesystem or on remote machine accessible supported protocols. The git clone command copies an existing Git repository. This is sort of like SVN checkout, except the “working copy” is a full-fledged Git repository—it has its own history, manages its own files, and is a completely isolated environment from the original repository.

As a convenience, cloning automatically creates a remote connection called "origin" pointing back to the original repository. This makes it very easy to interact with a central repository. This automatic connection is established by creating Git refs to the remote branch heads under refs/remotes/origin and by initializing remote.origin.url and remote.origin.fetch configuration variables.

An example demonstrating using git clone can be found on the setting up a repository guide. The example below demonstrates how to obtain a local copy of a central repository stored on a server accessible at example.com using the SSH username john:

gitclonessh://john@example.com/path/to/my-project.git
cdmy-project
#Startworkingontheproject

The first command initializes a new Git repository in the my-project folder on your local machine and populates it with the contents of the central repository. Then, you can cd into the project and start editing files, committing snapshots, and interacting with other repositories. Also note that the .git extension is omitted from the cloned repository. This reflects the non-bare status of the local copy.

Cloning to a specific folder

gitclone<repo><directory>

Clone the repository located at <repo> into the folder called ~<directory>! on the local machine.

Cloning a specific tag

gitclone--branch<tag><repo>

Clone the repository located at <repo> and only clone the ref for <tag>.

Shallow clone

gitclone-depth=1<repo>

Clone the repository located at <repo> and only clone the history of commits specified by the option depth=1. In this example a clone of <repo> is made and only the most recent commit is included in the new cloned Repo. Shallow cloning is most useful when working with repos that have an extensive commit history. An extensive commit history may cause scaling problems such as disk space usage limits and long wait times when cloning. A Shallow clone can help alleviate these scaling issues.

Configuration options

git clone -branch

The -branch argument lets you specify a specific branch to clone instead of the branch the remote HEAD is pointing to, usually the mainbranch. In addition you can pass a tag instead of branch for the same effect.

gitclone--branch

git clone -mirror vs. git clone -bare

git clone --bare

Similar to git init --bare,when the -bare argument is passed to git clone,a copy of the remote repository will be made with an omitted working directory. This means that a repository will be set up with the history of the project that can be pushed and pulled from, but cannot be edited directly. In addition, no remote branches for the repo will be configured with the -bare repository. Like git init --bare, this is used to create a hosted repository that developers will not edit directly.

git clone --mirror

Passing the --mirror argument implicitly passes the --bare argument as well. This means the behavior of --bare is inherited by --mirror. Resulting in a bare repo with no editable working files. In addition, --mirror will clone all the extended refs of the remote repository, and maintain remote branch tracking configuration. You can then run git remote update on the mirror and it will overwrite all refs from the origin repo. Giving you exact 'mirrored' functionality.

Other configuration options

For a comprehensive list of othergit cloneoptions visit theofficial Git documentation. In this document, we'll touch on some other common options.

git clone --template

gitclone--template=<template_directory><repolocation>

Clones the repo at <repo location> and applies the template from <template directory> to the newly created local branch. A thorough reference on Git templates can be found on ourgit init page.

Git URLs

Git has its own URL syntax which is used to pass remote repository locations to Git commands. Because git clone is most commonly used on remote repositories we will examine Git URL syntax here.

Git URL protocols

'-SSH

Secure Shell (SSH) is a ubiquitous authenticated network protocol that is commonly configured by default on most servers. Because SSH is an authenticated protocol, you'll need to establish credentials with the hosting server before connecting. ssh://[user@]host.xz[:port]/path/to/repo.git/

'- GIT

A protocol unique to git. Git comes with a daemon that runs on port (9418). The protocol is similar to SSH however it has NO AUTHENTICATION. git://host.xz[:port]/path/to/repo.git/

'- HTTP

Hyper text transfer protocol. The protocol of the web, most commonly used for transferring web page HTML data over the Internet. Git can be configured to communicate over HTTP http[s]://host.xz[:port]/path/to/repo.git/

Summary

In this document we took a deep look at git clone. The most important takeaways are:

1. git clone is used to create a copy of a target repo

2. The target repo can be local or remote

3. Git supports a few network protocols to connect to remote repos

4. There are many different configuration options available that change the content of the clone

For further, deeper reference on git clone functionality, consult theofficial Git documentation. We also cover practical examples ofgit clonein oursetting up a repository guide.

Share this article

Next Topic
Git config
git clone | Atlassian Git Tutorial (2024)

FAQs

How to use the git clone command? ›

Type git clone , and then paste the URL you copied earlier. Press Enter to create your local clone. $ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY > Cloning into `Spoon-Knife`... > remote: Counting objects: 10, done. >

How to clone a file in git? ›

These instructions show you how to clone your repository using Git from the terminal.
  1. From the repository, select the Clone button.
  2. Copy the clone command (either the SSH format or the HTTPS). ...
  3. From a terminal window, change to the local directory where you want to clone your repository.

How do I clone an existing git repository? ›

To Git clone a repository navigate to your preferred repository hosting service like GitHub, select the repository you want to clone, copy the repository URL via HTTPS or SSH, type git clone in the command line, paste the URL, and hit enter .

What is the difference between git clone and git pull? ›

Git clone copies all files to the local machine, while git pull only copies the modified files to the local machine. Git clone creates a connection between both repositories, while git pull requires a connection to be made before it can work.

How do you use the clone command? ›

Open the Chat menu and type clone [first coordinates] [second coordinates] [destination coordinates]. For example, you might type clone 302 3 2 300 1 0 ~ ~2 ~. Make sure your character is at least somewhat close to both the target that you're cloning and the destination where you're cloning it to.

How to clone a commit in git? ›

Steps to git clone a specific commit
  1. Obtain the URL of the remote repository.
  2. Perform a git clone of the remote repo.
  3. Switch to the branch that contains the commit.
  4. Obtain the seven-digit SHA id of the commit to clone.
  5. Issue a hard git reset that points to the commit id.
Sep 4, 2023

What is the difference between git clone and git checkout? ›

The git checkout command may occasionally be confused with git clone . The difference between the two commands is that clone works to fetch code from a remote repository, alternatively checkout works to switch between versions of code already on the local system.

How to clone from a branch in git? ›

For example, to clone the 1.x branch of sentry-cli :
  1. git clone -b 1.x https://github.com/getsentry/sentry-cli. Once the repository is downloaded, we can check the branch by running these commands:
  2. cd sentry-cli git status. We should receive the following output:
  3. On branch 1.x Your branch is up to date with 'origin/1.x'.
Mar 15, 2023

How to pull git command? ›

The git pull command is actually a combination of two other commands, git fetch followed by git merge. In the first stage of operation git pull will execute a git fetch scoped to the local branch that HEAD is pointed at. Once the content is downloaded, git pull will enter a merge workflow.

Do I need to install git to clone? ›

First, install Git from the official website, then open your command prompt, navigate to your desired directory, and use the command "git clone [repository URL]" to clone your repository.

Is it necessary to clone git? ›

Git, a distributed version control system, is widely used for tracking changes in source code during software development. One of the fundamental commands in Git is git clone . This command is important for developers to collaborate on projects by allowing them to create a local copy of a remote repository.

What happens when you clone a git? ›

Usage. git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location. The original repository can be located on the local filesystem or on remote machine accessible supported protocols. The git clone command copies an existing Git repository.

What does the clone command do in git? ›

git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location. The original repository can be located on the local filesystem or on remote machine accessible supported protocols. The git clone command copies an existing Git repository.

How to install packages using git clone? ›

To pip install a package from a specific branch of a Git repository, you can follow these steps:
  1. Step 1: Install Git. Make sure Git is installed on your system. ...
  2. Step 2: Clone the Git Repository. ...
  3. Step 3: Install the Package. ...
  4. Step 4: Install from a Specific Branch. ...
  5. Step 5: Update the Package.
Nov 2, 2023

Top Articles
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 5404

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.