Leave a rating/review
Now that you’ve seen how to create a repository, in this video, you’ll see how to clone a repository. First things first, if you don’t have an account for GitHub, you can go to github.com to set up an account. Enter your email, a password, and a username. Once that’s done, check your email for your verification code, and that’s it. These options are just customizations for your experience using GitHub. Now you have a GitHub account. In order to interact with GitHub, you’ll need an SSH key.
You can Google for this or just go to their docs and look for “generate new SSH key.” Copy this command. Open up your terminal and run it with your GitHub email. You can choose a specific name for your key. Since I have different GitHub accounts, I’ll choose the account name for this key. You can choose to enter a passphrase or not. I’m not going to at this time and just hit enter. If you didn’t choose a passphrase, hit enter again. If you did choose a passphrase, you’ll need to enter it here. Now our key is created. I can see that if I use the command ls to list what’s in my directory. On the Mac, these keys go into a .ssh folder. If you don’t have one created yet, you’ll need to do that first.
You’ll need to create it in your home directory. I already have it created, so I’m just going to move my keys over.
I’ll use the mv command to move both the private and public keys to that .ssh directory. GitHub needs to know the public key, so you can open that file and copy it to your clipboard. On the Mac, I can just use the pbcopy command to put the key into my pasteboard. Then return to github.com, click your profile, click settings, click SSH and GPG keys.
Then click “New SSH key.” Give your key a name. I find it convenient to just name it the same name as the key itself. Then paste the key into the key text field. Scroll down and add SSH key.
Now that your key is added to GitHub, you can use that key to interact with GitHub.
If you didn’t publish your repository from the last video because you didn’t have a GitHub account, pause this video and go ahead and push your code to your own repo. If you have already done that, continue on to clone a repo.
This time, you’ll see how to clone the repository from the command line. You’ll go through more command-line options later in this course, but the key to remember is that the GitHub Desktop app is essentially doing what you can do from the command line in a nice, easy-to-use GUI.
The repo I’m going to clone is the same repo we created in the previous video as the new user I just created on GitHub. This is the repo you just published to your own account. If you don’t want to create another GitHub account like I did, feel free to just sit back and watch how to clone a repo from the command line.
If I go to github.com and go to the repository I created earlier and click on “Code,” GitHub gives you options to clone the repo. Since we just created an SSH key, we’ll use SSH to clone the repo.
Copy this, return to the terminal, and in a directory where you want the repo to go, enter git clone and the SSH URL of the repo.
Open the directory of the repo and open the recipes.txt file. Just like in the Google Docs example, you can add more recipes to this document as another user. Add “Brisket” and “Aloha Tacos.”
You’ll cover more command-line basics with git later in this course, but for now, you’ll just briefly use some commands to push these changes as the second user.
In the directory of the repo, type git add .. This adds all of your changes so you can commit them.
Then type git commit -m followed by double quotes and a message for the commit. This makes a commit with the message “Added BBQ recipes.”
Then to push your changes to the repository, use git push origin HEAD.
Now return to a browser to view the repo. Refresh the browser. You’ll see the changes from the second user. You can see the commits have now increased from 2 to 3. Click on it. Click on the latest commit. See the changes the second user added.
If you return back to the GitHub app, which is the account that created the original two recipes, if you go to the history, you’ll see it only shows the original two commits but not the third commit by the other user. That’s because Git doesn’t know changes were made to the remote repo by another user. You need to tell Git to fetch those changes. Click on “Fetch origin.” Origin is a shorthand name for the location of the remote repo of our project.
By fetching, you’re telling the GitHub app to fetch the changes into your local repo but not necessarily pull them into your working directory. This is handy so you can see what other developers on your team are working on but not yet work them into your current working directory while you are building your own feature.
You can see that by the 1 here. This means that your local repo is one commit behind the remote repo. So, if you are ready to pull those changes into your working directory, you can.
Click “Pull origin” to pull those changes into your working directory. Now you can see the latest change by the second user, adding BBQ recipes.
Lastly, if you want to clone a remote repository onto your local machine, you don’t need to use the command line. You can also do that directly from the GitHub app. Go to File, Clone Repository. Go to the URL tab. Here you can enter a repository you are interested in cloning and the destination. Enter the materials repo for this course: kodecocodes/video-sgit-materials. Then choose a path to put the repo and select “Clone.”
And that’s it! Now the repo is cloned to my desktop, and the GitHub Desktop app has it selected as the current repo. Switch back to the recipes-book repo for the rest of this course.