Git Clone

The git clone command makes a full copy of an existing repository, including all files, branches and the history. You use it when you want to start working on a project which is already there, like a GitHub repository.

Syntax

git clone <repository-url>
git clone <repository-url> <folder-name>

The URL can use HTTPS (https://github.com/user/repo.git), SSH (git@github.com:user/repo.git) or even a local folder path.

Clone a repository

To practice without internet, we make a small repository to act as the server, and then clone it. The clone works the same with a GitHub link.

Terminal output

$ git clone server.git my-copy
Cloning into 'my-copy'…
done.
$ cd my-copy
$ ls
README.md
$ git log –oneline
f587a54 (HEAD -> main, origin/main, origin/HEAD) Add README
$ git remote -v
origin /home/robin/server.git (fetch)
origin /home/robin/server.git (push)

After the clone, Git has already set the remote named origin, which points to the place you cloned from.

Clone into a different folder name

git clone https://github.com/user/project.git my-folder

Clone a specific branch

git clone --branch develop https://github.com/user/project.git

Shallow clone (only the latest history)

A big project can have a very long history. If you need only the latest code, you can download just the last commit. It is faster and smaller.

git clone --depth 1 https://github.com/user/project.git

Clone vs download ZIP

git cloneDownload ZIP
Gets the full history and all branchesGets only the files of one branch
You can push and pull changesNot connected to any repository
Has the .git folderNo .git folder
Note: A clone is a full backup of the project. If the server is lost, any person’s clone can be used to restore it. Next: Git Status.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.