Curriculum
Course: Git
Login
Text lesson

Git Clone from GitHub

Clone a Fork from GitHub

Now we have our own fork on GitHub, but we also need a local clone to work on it using Git on our machine.

A clone is a complete copy of a repository, including all its history and versions of files.

Navigate back to the original repository and click the green “Code” button to get the URL for cloning:

Open Git Bash and clone the repository:

Example

[user@localhost] $

git clone https://github.com/code7school-test/code7school-test.github.io.git
Cloning into 'code7school-test.github.io'...
remote: Enumerating objects: 33, done.
remote: Counting objects: 100% (33/33), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 33 (delta 18), reused 33 (delta 18), pack-reused 0
Receiving objects: 100% (33/33), 94.79 KiB | 3.16 MiB/s, done.
Resolving deltas: 100% (18/18), done.

Check your file system, and you’ll see a new directory named after the cloned project.

Example

[user@localhost] $

ls
code7school-test.github.io/

Note: To clone the repository into a specific folder, add the folder name after the repository URL, like this: git clone https://github.com/code7school-test/code7school-test.github.io.git myfolder.

Go to the new directory and check the status:

Example

[user@localhost] $

cd code7school-test.github.io
git status
On branch master
Your branch is up to date with ‘origin/master’.

nothing to commit, working tree clean

And check the log to confirm that you have the complete repository data:

Example

[user@localhost] $

git log
commit facaeae8fd87dcb63629f108f401aa9c3614d4e6 (HEAD -> master, origin/master, origin/HEAD)
Merge: e7de78f 5a04b6f
Author: code7school-test
Date: Fri Mar 26 15:44:10 2021 +0100

     Merge branch ‘master’ of https://github.com/code7school-test/hello-world

commit e7de78fdefdda51f6f961829fcbdf197e9b926b6
Author: code7school-test
Date: Fri Mar 26 15:37:22 2021 +0100

      Updated index.html. Resized image

…..

Now, we have a complete copy of the original repository.

Configuring Remotes

Essentially, we have a full copy of a repository that we don’t have permission to modify directly.

Next, let’s check how the remotes are configured for this Git repository:

Example

[user@localhost] $

git remote -v

origin  https://github.com/code7school-test/code7school-test.github.io.git (fetch)origin  https://github.com/code7school-test/code7school-test.github.io.git (push)

We see that origin is set up to the original “code7school-test” repository, we also want to add our own fork.

First, we rename the original origin remote:

Example

[user@localhost] $

git remote rename origin upstream
git remote -v
upstream        https://github.com/code7school-test/code7school-test.github.io.git (fetch)
upstream        https://github.com/code7school-test/code7school-test.github.io.git (push)

Then retrieve the URL of our own fork:

And set that as the origin:

Example

[user@localhost] $

git remote add origin https://github.com/kaijim/w3schools-test.github.io.git
git remote -v
origin  https://github.com/kaijim/code7school-test.github.io.git (fetch)origin 
https://github.com/kaijim/code7school-test.github.io.git (push)
upstream        https://github.com/code7school-test/code7school-test.github.io.git (fetch)
upstream        https://github.com/code7school-test/code7school-test.github.io.git (push)

Note: According to Git naming conventions, it’s recommended to name your own repository origin and the original repository you forked from upstream.

Now we have two remotes:

  • origin – our own fork, where we have read and write access.
  • upstream – the original repository, where we have read-only access.

Next, we’ll make some changes to the code. In the following chapter, we’ll cover how to propose those changes to the original repository.