How to Clone a Repository from GitHub |Video upload date:  · Duration: PT11M35S  · Language: EN

Step by step guide to clone a GitHub repository using Bash and VS Code with branch tips and common fixes

Why bother cloning a repo

Because reading code on a website is like reading a menu through a fogged up window. If you want to run tests debug stuff or actually edit code you need the project on your local machine. Cloning a GitHub repository gives you a local copy of the repo so you can use git commands open the project in VS Code and not cry into the terminal.

Tools you will need

  • Git installed and available from Bash or your terminal
  • A GitHub account for private repos or contributing
  • Basic comfort with the command line or willingness to fake it
  • VS Code for peak productivity and a few helpful extensions

Quick checklist before you clone

  • Decide if you will use HTTPS or SSH for the remote URL
  • Open a terminal in the folder where you want the project to live
  • Have any required credentials or SSH keys ready

Step by step cloning workflow

Follow these steps and you will have a local repo faster than you can say git checkout main. This is the pragmatic approach used by developers who prefer working code over motivational posters.

Step 1 Get the clone URL

On the GitHub page click the Code button and copy the URL shown. You will see an HTTPS option and an SSH option. HTTPS is straightforward for one offs. SSH is nicer long term because you will get fewer password prompts.

Step 2 Open a terminal in your project folder

Use your file manager or run a command like this from your home directory to move into the folder where the repo should live.

cd path to projects folder

Step 3 Run the clone command

Paste the URL into a git clone command. If you like typing less or living dangerously you can add a custom folder name after the URL.

git clone https //github.com user repo.git

or for SSH

git clone git@github.com user repo.git

or to set a custom local folder name

git clone https //github.com user repo.git my local folder

The new folder will match the repository name unless you provided a different folder name. Congratulations you now have a copy of the repo on your local machine.

Step 4 Switch to a specific branch if needed

If you need a branch that is not the default do one of these depending on your mood and efficiency needs.

  • Clone then checkout
cd repo
git checkout branch name
  • Clone directly to the branch in one command
git clone -b branch name https //github.com user repo.git

Open in VS Code

VS Code will detect the project and suggest useful extensions. From your terminal run this to open the folder in VS Code.

code repo

If code is not found add the VS Code command line tools or open the folder from the editor GUI. VS Code will pick up tooling like linters formatters and recommended extensions the way a nosy friend picks up gossip.

Troubleshooting and tips that save time

  • Set up SSH keys so you do not get password nags repeatedly. Use ssh keygen and add the public key to GitHub.
  • If authentication fails check that your remote URL is correct and that your keys are loaded into the agent with ssh add.
  • Use git remote -v to confirm the remote name and URL. If you want a different remote name run git remote add origin URL after cloning.
  • If you accidentally cloned the wrong branch delete the folder and redo or use git fetch and git switch to move around branches.

Wrap up

Cloning a GitHub repository is a small number of commands that unlock a world of local editing testing and version control. With a reliable workflow you will spend less time wrestling with remotes and more time actually writing code. If you want less friction set up SSH keys and get comfortable with git clone -b and the code command for VS Code. Now go clone something useful and resist the urge to accidentally push a test file named final final final dot txt.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.