On the sixth day of Christmas, my true love gave to me… six copies of projects.
Yesterday, we learned how to push changes to a remote repository with Git. Today, we’re going to see how to copy remote Git repositories to your local computer.
If you are following our song, “The Twelve Days of Git,” here are the verses so far:
- The 12 Days of Git: Learn Git over the Holidays
- The 12 Days of Git, Day 2: Tracking Files with Git
- The 12 Days of Git, Day 3: Viewing Git History
- The 12 Days of Git, Day 4: Double-checking Changes with Git
- The 12 Days of Git, Day 5: Sharing Changes Remotely with Git
For today, we are NOT going to start in the same folder as we have been using. Instead, in a terminal, make another folder—such as your root Documents folder—your current working directory. We want to do this because today we are going to copy other repositories from GitHub down to our local computer.
If you look at my version of the Santa Tracker repo on GitHub, you will see a text box near the center left that has a special URL in it. This URL ends in “.git”, and it represents the repository’s address for interacting with the Git program. Bitbucket also lists a Git URL for projects on their site, but we will use GitHub for today’s work.
In the terminal, type the following to make a copy of my version of the Santa Tracker project into a new folder called
git clone https://github.com/vjwilson/santa-tracker.git santa-tracker-2
You will see output like this:
Cloning into 'santa-tracker-2'... remote: Counting objects: 8, done. remote: Compressing objects: 100% (7/7), done. remote: Total 8 (delta 1), reused 8 (delta 1), pack-reused 0 Unpacking objects: 100% (8/8), done. Checking connectivity... done.
The clone sub-command requires the Git URL of a repository. If you don’t also give it a new folder name, as we did here with
santa-tracker-2, it will use the remote repo’s name for the new folder. In this case, if you tried to do this in the same parent folder as the one that holds your own copy of folder called santa-tracker, the command would fail.
Once you have cloned my repo, make it your current working directory and look at its remote and history.
cd santa-tracker-2 git remote -v ... git log
Cloning someone else’s Git repository—assuming the project is open-source—is a perfectly acceptable way of getting a copy of a project. However, if you plan on modifying your copy, and saving those changes back to to your own account, there is a cleaner way to make a copy.
Forking a Git Repository
Often, you will want to copy an open-source repo of a project and make your own modification. Yet, if a new version of the project comes out, you’d like to be able to import those improvements from original project into your copy. You may also want to offer your modifications back to original project. Even if you are collaborating with others on a private project, you each may wish to have your own remote copy of the repo, so that each of you can review what the others are doing, without necessarily accepting all the others’ changes.
The standard way to do this in Git is for each person to create their own fork of a repo. This is one case where it’s easiest to use the online interface for the repository, rather than the command line. Go ahead and fork the repo for Twitter’s Bootstrap front-end framework. Visit its page on GitHub, https://github.com/twbs/bootstrap, and click the fork button in the upper-righthand corner.
Now, you have your own copy of that repo. You can clone your fork down to your machine and modify it to your heart’s content. Whenever you want to save a new version of your copy, simply commit your changes and then do a
git push to save those changes to your remote GitHub repo of the fork.
Come back for tomorrow’s verse, when we’ll learn about Keeping Your Git Repository Up-To-Date with Others’ Changes.
To see all the options available for the commands we’ve learned today, check them out in the online Git reference: