Chapter 7 GitHub Remote Repository
Version control really comes into its own when we begin to collaborate with other people. We already have most of the machinery we need to do this; the only thing missing is to copy changes from one repository to another.
Systems like Git allow us to move work between any two repositories. In practice, though, it’s easiest to use one copy as a central hub, and to keep it on the web rather than on someone’s laptop. Most programmers use hosting services like GitHub to hold the master copies. Alternatives include, BitBucket and GitLab.
Let’s start by sharing the changes we’ve made to our current project with the world. Log in to GitHub, then click on the icon in the top right corner to create a new repository called test_one
:
Creating a Repository on GitHub (Step 1)
Name your repository “test_one” and then click “Create Repository”:
Creating a Repository on GitHub (Step 2)
As soon as the repository is created, GitHub displays a page with a URL and some information on how to configure your local repository:
Creating a Repository on GitHub (Step 3)
This effectively does the following on GitHub’s servers:
> mkdir test_two
> cd test_two
> git init
Our local repository still contains our earlier work on file_one.txt
, but the remote repository on GitHub doesn’t contain any files yet:
Freshly-Made GitHub Repository
The next step is to connect the two repositories. We do this by making the GitHub repository a remote for the local repository. The home page of the repository on GitHub includes the string we need to identify it, where in this case we will use the
Where to Find Repository URL on GitHub
To create a secure link to the GitHub account we make use of SSH protocol. To set this up I would enter the following command into the Git Shell.
ssh-keygen -t rsa -b 4096 -C "kevinkotze@gmail.com"
This creates a new ssh key, using the provided email as a label. To generate a public/private rsa key pair you would normally accept the default file location for the storage of the key.
At the prompt, type a secure passphrase.
# Generating public/private rsa key pair.
# Enter file in which to save the key (/c/Users/Kevin Kotze/.ssh/id_rsa):
# Enter passphrase (empty for no passphrase):
# Enter same passphrase again:
# Your identification has been saved in /c/Users/Kevin Kotze/.ssh/id_rsa.
# Your public key has been saved in /c/Users/Kevin Kotze/.ssh/id_rsa.pub.
In addition, it will also provide details of the key. Now click on the ‘SSH’ link to change the protocol in the browser.
Copy that SSH from the browser, go into the local test_one
repository and run this command in the Git Shell:
> git remote add origin git@github.com:KevinKotze/test_one.git
Make sure to use the SSH for your repository rather than mine: the only difference should be your username instead of KevinKotze
.
We can check that the command has worked by running git remote -v
:
> git remote -v
# origin git@github.com:KevinKotze/test_one.git (fetch)
# origin git@github.com:KevinKotze/test_one.git (push)
The name origin
is a local nickname for your remote repository. We could use something else if we wanted to, but origin
is by far the most common choice.
Once the nickname origin
is set up, this command will push the changes from our local repository to the repository on GitHub:
> git push origin master
# Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts.
# Counting objects: 12, done.
# Delta compression using up to 8 threads.
# Compressing objects: 100% (8/8), done.
# Writing objects: 100% (12/12), 1.17 KiB | 0 bytes/s, done.
# Total 12 (delta 2), reused 0 (delta 0)
# remote: Resolving deltas: 100% (2/2), done.
# To github.com:KevinKotze/test_one.git
# * [new branch] master -> master
Now go to your browser and look at the files in the KevinKotze/test_one repository. Under the Code tab, you will note that the files that were intentionally ignored are not included in the repository. You should then find and click on the text that says “XX commits” (where “XX” is some number). This will provide details relating to all the commits that were made in chronological order.
Reviewing all the commits
If you click on the number of the commit (such as 339dcbf) it will show the results of the first commit.
Review details of a commit
We can also pull changes from the remote repository to the local one as well:
> git pull origin master
# Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
# From github.com:KevinKotze/test_one
# * branch master -> FETCH_HEAD
# Already up-to-date.
Pulling has no effect in this case because the two repositories are already synchronized. If someone else had pushed some changes to the repository on GitHub, though, this command would download them to our local repository.