Sub topic 4
Running git clone
This chapter covers the git clone command itself: its syntax, what it does step by step, and how to choose the target directory. It assumes you have Git installed, a GitHub account, and basic terminal skills. It also assumes you know what a commit and a branch are, and that you have already picked an HTTPS or SSH clone URL.
The basic command
git clone takes a repository URL and creates a local copy. The simplest form uses an HTTPS URL:
git clone https://github.com/octocat/Hello-World.gitGit creates a directory named Hello-World in your current folder and fills it with the repository. If you have an SSH key registered with your GitHub account, you can use the SSH form instead:
git clone [email protected]:octocat/Hello-World.gitBefore you clone over SSH, test that your key authenticates:
ssh -T [email protected]A successful test prints a greeting that includes your username. GitHub does not give you a shell, so the connection closes right away.
What happens during a clone
A single git clone runs several steps in order:
- Git creates a new directory and runs
git initinside it. - It adds a remote named
originthat points to the URL you gave. - It downloads the full history, meaning all commits, branches, and tags.
- It creates remote-tracking branches such as
origin/main. - It checks out the remote's default branch into your working directory.
Because the full history is local, most later operations, such as git log and git diff, work without a network connection.
Choosing the target directory
By default, Git names the directory after the repository, without the .git suffix. To choose a different name, add it as a second argument:
git clone https://github.com/octocat/Hello-World.git my-projectThis creates my-project instead of Hello-World. The directory name has no effect on the repository. Git does not record it anywhere.
Two rules apply to the target:
- If the directory does not exist, Git creates it.
- If it exists, it must be empty. Otherwise Git stops with an error such as
destination path 'my-project' already exists and is not an empty directory.
You can also clone into the current directory with . as the target, provided that directory is empty:
git clone https://github.com/octocat/Hello-World.git .Checking the result
After cloning, move into the directory and look at the state Git created:
cd Hello-World
git status
git remote -v
git branch -a
git log --oneline -n 5git status shows the checked-out branch. git remote -v lists origin with its fetch and push URLs. git branch -a shows the local branch and the remotes/origin/... branches. git log confirms the history arrived.
Common errors at this step
| Message | Likely cause |
|---|---|
| `repository not found` | The URL has a typo, or the repository is private and you are not authenticated as someone with access. |
| `Permission denied (publickey)` | The SSH key is missing, not loaded, or not added to your GitHub account. |
| `Authentication failed` | Over HTTPS, GitHub requires a personal access token or credential helper, not your account password. |
| `destination path ... already exists` | The target directory exists and is not empty. |
Exercise
You run git clone https://github.com/octocat/Hello-World.git site in an empty folder. What is the name of the directory created, what is the name of the remote, and which command lists the remote's URL?
Show answer
Git creates a directory named site, not Hello-World. The remote is named origin. Run git remote -v inside site to list its fetch and push URLs.
Exercise
A clone fails with destination path 'app' already exists and is not an empty directory. Name two ways to fix it.
Show answer
Choose a different target name, such as git clone URL app2, or empty or remove the existing app directory first. Either way, the target must not contain files.