Git Product home page Git Product logo

git-version-control-getting-code-with-git's Introduction

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

Git repositories let us create logged histories of the versions of the files we "track." Just think, right now, around the world people are using Git to track their projects: Star Trek Fan Fiction, resumes, Ruby Code, JavaScript code, PhD theses, etc.

Git not only lets you track files in a local repo on your machine, you can "share" your repo on the internet so that others can use your code. In this lesson we'll discuss how to get others' repositories.

In a later lesson we'll cover how to push our locally-created repositories onto the internet so that others can see our projects.

Copy a Repository to Your Local Machine with git clone

We use git clone to copy someone else's repo from the internet to our local machine. We are not getting their repo from their local machine (that would be very creepy).

Instead, they must have already "mirrored" their local repository onto the internet. In Git-speak we'd say they would have had to have created a remote repository: a copy of their local repository, but on the internet. We'll be cloning that remote repository.

Let's get the code for the popular ReactJS framework.

  1. Navigate to the https://github.com/facebook/react repository.

  2. Click the green "Clone or download" button on the right.

  3. Make sure you select Use SSH as your URL type.

    SSH Clone
  4. Click the "Copy to clipboard" button (highlighted below). This will copy the URL for us to use when we clone.

    Copy command to clone repo button
  5. In the terminal, run the git clone command. It takes the URL we just copied as an argument, like so:

$ git clone your-copied-github-url

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

List Remotes 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

Type git remote to see the names of each remote repository (or, "remote") available.

Since you cloned your repository, you should see a remote name called origin. The remote name origin is the default name Git gives to the remote you cloned from:

$ git remote
origin

Let's prove that the origin name has some relationship to the address GitHub gave us.

$  git remote show origin
* remote origin
  Fetch URL: [email protected]:facebook/react.git

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.

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

Forking a GitHub repository is just a way to create a personal, online duplicate of it. When you fork a repository, GitHub creates a duplicate of that repository under your control.

If my GitHub username were octocat and I "forked" facebook/react, GitHub would copy the remote repository facebook/react and create it under my name as octocat/react. It's making a copy of one remote repository to a new remote repository.

It's like saying "Hey, can I have the Louvre's version of The Mona Lisa?" The Louvre would say, "No." If you were to create a perfect online duplicate by forking it from louvre/mona_lisa to your-name/mona_lisa, and then were to clone from that remote repository, then the Louvre can keep their copy and you can update your copy as you choose.

Fork Button

Let's try a fork and clone workflow.

Click the GitHub icon at the top of this page:

GitHub Octocat Icon

This will bring you to the "learn-co-students" version of this lesson. Click the 'Fork' button in the upper right corner of the page. You will be prompted to choose where the repository should be forked to, so go ahead and choose your account. GitHub will take a few moments to create the fork, then navigate to your copy of the repository. If all has gone well, you will see your username at the top of the page, followed by a / and the name of the repository, along with a link just below to the original repository. (More on forking in the GitHub docs.)

The important take away is to not misuse the words "fork" and "clone" when speaking with other Git users. To get a local copy: clone; to make an online copy of a repository to your personal organization so that you have the ability to update its master branch, fork.

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.

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.

git-version-control-getting-code-with-git's People

Contributors

drakeltheryuujin avatar jenmyers avatar marcnjaramillo avatar maxwellbenton avatar meg-gutshall avatar rrcobb avatar sgharms avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.