Skip to content

Latest commit

 

History

History
112 lines (79 loc) · 4.15 KB

README.md

File metadata and controls

112 lines (79 loc) · 4.15 KB

Getting Code with Git

Learning Goals

  • Copy a repository to your local machine with git clone
  • List remotes with git remote
  • Duplicate other organizations' repositories into your own via GitHub with the "Fork" button

Introduction

In the previous lesson, we learned how Git allows us to create logged histories of all the versions of the files we "track." Another great benefit of using Git is that it makes it easy for developers to "share" code by "pushing" a copy of their repo to the internet (specifically, to GitHub). From there, other developers can clone down the code onto their own machine and use it.

Of course, you've been taking advantage of this feature of Git for a while with the labs you've been working on in this course! For this reason, a lot of the content in this lesson will be review.

The important thing to understand is that the forking and cloning workflow that you've been using with labs can be used with any repo available on GitHub.

Copy a Repository to Your Local Machine with git clone

Let's review the cloning process using a repo that isn't part of the curriculum. We can get the code for the popular ReactJS framework:

  1. Navigate to the React repository.

  2. Click the green "Code" button on the right.

  3. Make sure SSH is selected.

  4. Click the copy button.

clone-repo

  1. In the terminal, navigate to where you want to put the repo. Type git clone and a space, then paste in the copied SSH link from GitHub. It should look like this:
$ git clone [email protected]:facebook/react.git

This will create a local copy of the GitHub repository on your machine.

List Remote Repos with git remote

If you use the ls command, you'll see Git created a directory called react. Use cd to enter that directory.

$ cd react

The git remote command will return the names of each remote repository (or, "remote") available. Go ahead and run the command; you should see a remote named origin returned. This is the "nickname" that Git assigns by default to whatever remote you cloned from:

$ git remote
origin

To prove that the origin name points to the repo we cloned from GitHub, we can run git remote -v (the v flag stands for "verbose"). You should see something like this:

$  git remote -v
origin	[email protected]:facebook/react.git (fetch)
origin	[email protected]:facebook/react.git (push)

Here you can see that the "remote address" ([email protected]:facebook/react.git) assigned to the "remote name" (origin) is the same thing you copied from the GitHub web interface. This confirms that the remote repository you cloned automatically set up a remote name called origin.

In the next lesson we'll learn how to push code up to GitHub. The git remote command is what will enable you to ensure that the code is being pushed to the right place.

Duplicate Other Organizations' Repositories into Your Own via GitHub with the "Fork" Button

This is also part of the workflow you've been using for labs: you create your own personal copy of a lab by clicking the "OctoCat" icon on Canvas, then the "Fork" button on GitHub.

Let's go back to the React repository on GitHub. In the upper right corner of the page, you'll see three buttons, including the familiar Fork button:

Fork Button

Clicking the Fork button on the page for this repo (or any other repo available on GitHub) will make a copy of the repo and store it in your GitHub account. Having your own copy gives you the ability to update its main (or master) branch without affecting the original repo.

Conclusion

GitHub gives developers many ways to collaborate. Using GitHub's "Fork" button and git clone together allows you to make copies of others' code.

Often, the original authors will include license information regarding how you can use their repository, so make sure to check before you publish, sell or distribute any material you've forked, cloned and modified.