What is clone? How to Copy Someone Else's GitHub Repository to Your PC
In the previous articles, you mastered how to upload (push) a project created on your own PC (local) to a repository on GitHub. With this, you've learned the basics of managing your own code with GitHub.
This time, our theme is the reverse pattern: how to copy someone else's repository (project) from GitHub wholesale onto your own PC. This operation is called "clone." It's a crucial first step for using excellent open-source projects from around the world or participating in team development. Let's learn how to do it right away!
What is `git clone`? The Difference from a Simple Download
On a GitHub repository page, under the green "Code" button, there's also an option to "Download ZIP." You might think, "If I just want to copy the files, isn't this enough?" However, `git clone` and a ZIP download are similar but fundamentally different things.
- ZIP Download: This only downloads the set of project files at that specific moment, just as regular files. It does not include the `.git` directory, which contains the past change history (commit logs). This means you don't get the benefits of version control.
- git clone: In addition to all the project files, it also copies the entire history of changes (all commits). This creates a new, fully functional local repository on your PC. You can immediately look back at the change history or commit new changes.
To put it in an analogy, a ZIP download is like getting "just a copy of the last page of a book." In contrast, `git clone` is like "getting a brand new, complete copy of the same book."
How to Practice `git clone`
Now, let's actually use the `git clone` command to copy a repository from GitHub to your PC.
Step 1: Copy the URL of the Repository You Want to Clone
First, go to the GitHub page of the project you want to copy. For this example, let's try cloning the repository of the famous CSS framework, "Bootstrap."
Once you open the page, click the green "Code" button. You'll see several options for copying the URL. There are HTTPS and SSH, but since we set up an SSH key last time, it's convenient to use the SSH URL. Using SSH allows you to skip entering your password when you push or pull.
[Image: Clicking the "Code" button on Bootstrap's GitHub repository and copying the SSH URL.]
Click the copy icon to the right of the URL to copy it to your clipboard.
Step 2: Run the `git clone` Command in Your Terminal
Next, open your terminal (or Git Bash on Windows) and navigate to the location where you want to save the project. Your desktop or a development folder like `projects` would be a good place.
Once you've moved to your desired location, it's time for the `git clone` command. As shown below, execute the command followed by the URL you just copied.
git clone [Paste the copied SSH URL here]
When cloning the Bootstrap repository, the specific command will look like this:
git clone git@github.com:twbs/bootstrap.git
When you run it, the download will begin. Depending on the size of the repository, it might take a little time.
Step 3: Confirm That the Clone Was Successful
Once the command has finished executing, a folder with the same name as the repository (in this case, `bootstrap`) should have been created in your current directory. Let's check.
ls
If you see a folder named `bootstrap`, move into it.
cd bootstrap
With this, a complete repository of Bootstrap is now ready on your PC. This folder is already under Git's management, so you don't need to run `git init` or anything similar.
First Steps After Cloning
Once you've cloned a repository, you can start working on the project right away. Here are a few commands that are often used before starting work.
Check the Branches
To see what working branches are available, use the `git branch` command. Adding the `-a` option will also show you the branches in the remote repository.
git branch -a
Get the Latest Changes
The repository you cloned might be updated by other people while you are working. Before you start working, always run the `git pull` command to reflect the latest state of the remote repository in your local repository.
git pull
A Quick Note: If You Want to Contribute, Start with a "Fork"
Here's one important concept to introduce. You cannot directly push changes to someone else's repository if you don't have write permissions. If you want to "contribute" to a project by adding features or fixing bugs, you first perform an operation called "Fork."
A fork is a feature that copies someone else's repository wholesale into your own GitHub account, creating a remote repository just for you.
The basic workflow for contributing to an open-source project is as follows:
- Fork the project you want to contribute to on GitHub.
- Clone the repository that was created on your account by the fork to your PC.
- Modify the code on your PC and push it to your own remote repository.
- On GitHub, send a "Pull Request" from your repository to the original project, requesting that they incorporate your changes.
First, get used to `clone`, and as a next step, definitely try your hand at a pull request from a fork.