Cloning a repository
Introduction
This section will guide you through the process of cloning a GitHub repository (repo). A repo contains all of your code, your files and each file’s version history. Repos can have multiple collaborators and can be either private or public. You can find out more about GitHub repos on GitHub Docs.
If you have a repo on Azure DevOps, GitHub or GitLab, you generally can’t edit or run your work on those platforms, so if you need to work with the contents of a repo, you’ll usually need to make a clone.
There are several ways to clone an existing repo. Below, you’ll find two methods. The first uses Rstudio and the second uses Git Bash. Start with step 1, which is the same for both methods, and then decide which method to follow. Once you have successfully cloned, you should see the files appear in the location you have chosen to store your repo.
Step 1: Get the repository clone link
a. Go to the git-academy-sandbox repository page on GitHub.
b. Click the “Code” button and copy the URL (HTTPS link).
Step 2a: Open folder and Git Bash
Navigate to the folder on your device where you want to clone the repo. Right-click in the directory and select “Git Bash Here”.
Step 2b: Clone the Repository in Git Bash
- In the Git Bash window that opens, use the `git clone` command followed by the URL you copied from GitHub. For this repo, we would enter:
git clone https://github.com/dfe-analytical-services/git-academy-sandbox.git
You will need to right-click to paste, as the keyboard shortcut will not work in Git Bash and then press enter.
Step 2c: Verify the clone in Git Bash
Navigate into the repo by typing cd git-academy-sandbox and pressing enter.
You should now see the current branch name (e.g. main) at the end of the file path as below:
- Type git log and press enter to view recent commits. Press q when you are ready to exit git log.
- Type git status and press enter to check the state of the cloned repository (at this point, it should say everything is up to date, as you haven’t made any changes yet).
Step 2a: Open RStudio
Open RStudio on your device.
Step 2b: Clone the Repository in RStudio
In RStudio, go to File > New Project > Version Control > Git.
Paste the URL you copied from GitHub into the “Repository URL” field.
Choose a directory on your device where you want to save the repo.
Click “Create Project”.
Step 2c: Verify the Clone
Check the Files pane in RStudio to see if the repo files have been cloned to your local machine. The files pane normally opens in the right-hand bottom corner of RStudio.
Conclusion
You have successfully cloned a GitHub repository! Continue on to learn how to create branches.