Skip to content

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.

Terminal window
git clone https://github.com/yourusername/git-practice.git

This 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 origin already 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.

Terminal window
cd git-practice
git remote -v

Output:

origin https://github.com/yourusername/git-practice.git (fetch)
origin https://github.com/yourusername/git-practice.git (push)
Terminal window
git log --oneline

Shows the full commit history, identical to what is on GitHub.

By default, git clone names the directory after the repository. To specify a different name:

Terminal window
git clone https://github.com/yourusername/git-practice.git my-folder

This clones the repository into my-folder instead.

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:

Terminal window
git branch -a

To create a local branch that tracks a remote one:

Terminal window
git switch feature/add-trailhead

If 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.

Clone URLs come in two forms:

HTTPS:

https://github.com/yourusername/git-practice.git

Uses your credentials (username + Personal Access Token). Works anywhere, no setup required.

SSH:

git@github.com:yourusername/git-practice.git

Uses 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.

SituationRight approach
You have an existing local repo, want to put it on GitHubgit remote add origin <url> then git push
You have a GitHub repo, want to get it onto this machinegit clone <url>
Starting fresh with no local repoInitialize on GitHub, then git clone
Team project, you are joininggit clone the team’s repository
  1. In your terminal, navigate to a folder that is not inside your existing git-practice directory.

  2. Clone your GitHub repository:

Terminal window
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.)

  1. Enter the cloned directory:
Terminal window
cd git-practice-clone
  1. Run git remote -v and confirm origin is already configured.

  2. Run git log --oneline and confirm the full commit history is there.

  3. 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, and origin already configured.
  • Cloning does not require a separate git remote add step.
  • 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 add only when connecting an already-initialized local repo.

Next: the module recap — a consolidated reference for all GitHub commands from this module.