git clone
Beginner Git CLI Safe
Summary: git clone <url> downloads a complete repository — every commit, branch and tag — into a new folder, names the source origin, and checks out the default branch so you can start working.
Safe — it creates a new folder and never modifies anything that already exists.
What it does
Creates a folder named after the repository, copies the full history into its .git, sets the remote origin to the URL, creates a local main (or whatever the default branch is) tracking origin/main, and writes the files of that branch into the working tree.
Why it exists
Git is distributed: every participant needs a full copy. clone is the one-step way to obtain it, and the reason push and pull know where to go without being told.
When to use it
- Joining any project that already lives on GitLab, GitHub or another server.
- Getting a second copy on another computer, or a second working copy for testing.
- Copying a public project to read it locally.
When not to use it
- For a folder that is not yet a repository:
git init. - To update a clone you already have:
git pull, not a second clone. - Inside an existing repository (nested clones are a common mistake).
Syntax
| Form | Meaning |
|---|---|
git clone <url> |
Clone into a folder named after the repository |
git clone <url> <folder> |
Clone into a folder of your choice |
git clone --branch <name> <url> |
Check out a specific branch instead of the default |
git clone --depth 1 <url> |
Shallow clone: only the latest commit; faster for huge histories, limited for history work |
git clone <local-path> |
Clone another repository on the same computer (handy for experiments) |
URLs come in two forms: HTTPS (https://gitlab.com/group/project.git, needs a token on push) and SSH (git@gitlab.com:group/project.git, needs an SSH key). Lesson 8.1 compares them.
Examples
$ cd ~/git-practice
$ git clone https://gitlab.com/northwind-trails/trailguide.gitCloning into 'trailguide'...
done.(Real clones over the network also print progress lines: remote: Enumerating objects…, Receiving objects: 100%.) The result is ready to use:
$ cd trailguide
$ git remote -vorigin https://gitlab.com/northwind-trails/trailguide.git (fetch)
origin https://gitlab.com/northwind-trails/trailguide.git (push)$ git statusOn branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree cleanInto a differently named folder, from a local path:
$ git clone trailguide trailguide-copyCloning into 'trailguide-copy'...
done.Expected result
A new folder with the files of the default branch and a .git containing the whole history; git log shows every commit; git branch -a lists main and remotes/origin/…. Nothing outside the new folder changes.
Common mistakes
fatal: destination path 'trailguide' already exists. A folder with that name is there. Clone into another name, or move the old folder.Repository not found/Authentication failed. Wrong URL, a private project you have no access to, or missing credentials. Check the URL on the project page and your access (lesson 9.3).- Cloning, then editing the ZIP download instead. The ZIP has no
.git; work in the clone. warning: You appear to have cloned an empty repository. The project has no commits yet; that is fine for a new project you will push into.
How to undo or recover
Delete the folder. A clone has no effect on the remote or on any other folder.
In VS Code
Command Palette → Git: Clone, paste the URL, choose the parent folder; or the Clone Repository button in an empty Source Control view. See Clone and open.
In IntelliJ IDEA
Welcome screen → Get from VCS, or File → New → Project from Version Control…. See Clone (Get from VCS).
Related commands
git init starts an empty repository instead · git remote shows or changes where a clone points · git fetch and git pull keep a clone up to date.