Sub topic 11
When to Clone Instead of Adding a Remote
This chapter assumes you have Git installed, can make commits, and have a GitHub account. It also assumes you know what a remote is and have seen git remote add and git push -u.
Two Ways to Start
You can connect a project to GitHub in two ways:
- Start locally, then add a remote. Use this when the code already exists on your machine.
- Start from GitHub, then clone. Use this when the repository already exists on GitHub.
Cloning does the connection work for you. git clone downloads the full history, creates a local repository, adds a remote named origin, checks out the default branch, and sets up upstream tracking. It replaces git init, git remote add, and the first git pull or git push -u.
Cloning in Practice
Clone with the same URL types you chose earlier, SSH or HTTPS:
git clone [email protected]:YOUR-USERNAME/YOUR-REPO.git
cd YOUR-REPO
git remote -v
git branch -vvgit remote -v shows origin with fetch and push URLs. git branch -vv shows the local main branch tracking origin/main. You did not run git push -u or git branch -M main, because clone used the branch name that GitHub reported as the default.
To put the clone in a different folder, add a directory name:
git clone [email protected]:YOUR-USERNAME/YOUR-REPO.git my-folderHow to Choose
| Situation | Better choice |
|---|---|
| Code exists only on your machine, with commits | Create an empty GitHub repository and use `git remote add` |
| Repository exists on GitHub and you have no local copy | `git clone` |
| You created the GitHub repository with a README or license and have no local work | `git clone` |
| You created a GitHub repository with a README, but you already have local commits | `git remote add`, then pull with `--allow-unrelated-histories` if needed |
| You are working on someone else's project | Fork on GitHub, clone your fork, then add the original as `upstream` |
The README case matters most. A repository created with a README already has a commit. If you also have local commits, the two histories share no ancestor, and the first push is rejected. When you have no local work, cloning avoids the problem. When you do have local work, the empty-repository path avoids it.
Cloning an Empty Repository
You can clone a repository that has no commits yet. Git prints a warning that you appear to have cloned an empty repository. The clone still sets up origin. Your first commit and git push -u origin main then work as usual, though you may need git branch -M main if your local default branch name differs.
Common Errors
destination path already exists and is not an empty directory: the target folder has files in it. Clone into a new folder name, or usegit initandgit remote addin the existing folder.Permission denied (publickey): the SSH key is not set up. Runssh -T [email protected]to test it.Authentication failedover HTTPS: the password is not accepted. Use a personal access token orgh auth login.Repository not found: the URL is misspelled, or the repository is private and your account has no access.
Exercise
You created a GitHub repository with a README. On your laptop you already have a project with three commits. Should you clone the GitHub repository? Explain what you would do instead.
Show answer
No. Cloning would give you a second local copy that has only the README commit, and you would have to copy your files into it by hand, losing your three commits. Instead, keep your existing repository, run git remote add origin URL, and run git pull origin main --allow-unrelated-histories to merge the README history. Then run git push -u origin main. Creating the GitHub repository empty, without a README, would have avoided the merge step.
Exercise
After running git clone, which command shows that the local main branch tracks origin/main, and why did you not need git push -u?
Show answer
git branch -vv shows the tracking relationship. Clone sets upstream tracking for the default branch automatically, so git push -u is not needed.