Cloning a Repository
Cloning is how you get a copy of an existing repository onto your machine. Whether it is your own repository from another computer, an open-source project you want to contribute to, or a team repository on your first day at a job — git clone is the command.
What cloning does
Section titled “What cloning does”git clone https://github.com/yourusername/git-practice.gitThis creates a new directory named git-practice in your current folder and copies into it:
- Every file in the repository
- The complete commit history
- All branches
- The remote configuration, with
originalready pointing to the URL you cloned from
After cloning, you have a fully functional local repository — no separate git remote add step needed. The remote is already set up.
Verifying the clone
Section titled “Verifying the clone”cd git-practicegit remote -vOutput:
origin https://github.com/yourusername/git-practice.git (fetch)origin https://github.com/yourusername/git-practice.git (push)git log --onelineShows the full commit history, identical to what is on GitHub.
Cloning into a specific directory
Section titled “Cloning into a specific directory”By default, git clone names the directory after the repository. To specify a different name:
git clone https://github.com/yourusername/git-practice.git my-folderThis clones the repository into my-folder instead.
What about branches?
Section titled “What about branches?”When you clone, Git creates a local branch for the default branch only (usually main). All other remote branches exist as remote-tracking branches (origin/feature/...) but not as local branches yet.
To see all branches including remote-tracking:
git branch -aTo create a local branch that tracks a remote one:
git switch feature/add-trailheadIf feature/add-trailhead does not exist locally but does on origin, Git automatically creates a local tracking branch. This shorthand works because Git checks for a matching remote-tracking branch.
SSH vs HTTPS
Section titled “SSH vs HTTPS”Clone URLs come in two forms:
HTTPS:
https://github.com/yourusername/git-practice.gitUses your credentials (username + Personal Access Token). Works anywhere, no setup required.
SSH:
git@github.com:yourusername/git-practice.gitUses your SSH key. No credentials prompt once set up. Most developers prefer this for repositories they own and push to frequently.
GitHub shows both options when you click the Code button on a repository page. Use whichever matches how you authenticated in the previous lessons.
When to clone vs when to connect
Section titled “When to clone vs when to connect”| Situation | Right approach |
|---|---|
| You have an existing local repo, want to put it on GitHub | git remote add origin <url> then git push |
| You have a GitHub repo, want to get it onto this machine | git clone <url> |
| Starting fresh with no local repo | Initialize on GitHub, then git clone |
| Team project, you are joining | git clone the team’s repository |
Exercise
Section titled “Exercise”-
In your terminal, navigate to a folder that is not inside your existing
git-practicedirectory. -
Clone your GitHub repository:
git clone https://github.com/yourusername/git-practice.git git-practice-clone(Replace yourusername with your actual username. The trailing git-practice-clone puts the clone in a differently-named folder so it does not conflict with your existing git-practice folder.)
- Enter the cloned directory:
cd git-practice-clone-
Run
git remote -vand confirmoriginis already configured. -
Run
git log --onelineand confirm the full commit history is there. -
You now have two local copies of the same repository — both connected to the same GitHub remote. This is exactly how multi-machine or team workflows work.
git clone <url>creates a local copy of a remote repository — with full history, branches, andoriginalready configured.- Cloning does not require a separate
git remote addstep. - By default, only the default branch becomes a local branch; other branches exist as remote-tracking branches until you switch to them.
- Choose HTTPS or SSH based on how you authenticated in the previous lessons.
- Clone when getting a repo onto a new machine or joining a team. Use
git remote addonly when connecting an already-initialized local repo.
Next: the module recap — a consolidated reference for all GitHub commands from this module.