Average rainfall 2001-2016, global tropics

Map: Average rainfall 2001-2016, global tropics

Remote repositories with GitHub

Thomas Gumbricht bio photo By Thomas Gumbricht

Introduction

This post covers connecting a local git clone to a remote master at gitHub.com. The post is inspired by the Youtube Introduction to Git - Remotes by David Mahler, and the written post ‘Git’ with the Program: Getting Started with GitHub.

Create a GitHub account

GitHub is free for private users that are satisfied with public repositories. You just sign up, give your email and once you have confirmed your register via your email, and given answers to some simple questions about experiences and interests, you will land at the page “Create a new repository”.

github-nav-new-repo

If you do not get to the page “Create a new repository”, use the navigation menu next to the avatar to select “New Repository” (as shown to the right).

Go ahead and create you first repo, the figure below shows the fields to fill.

Github - Create a new repository.

You have to give the repo a name (“my-first-repo” in the example above). Also fill in a Description and let the repository be Public (to use Private repos comes with a fee). Click the radio button for Initialize this repository with a README. Click Create Repository and behind the scenes GitHub is staging and commiting the README.md file.

github-dismiss-github-action-popup

When the repo is ready, GitHub will open a page with your repo. If there is a pop-up window requiring about GitHub Actions (as shown to the right), just click Dismiss.

Your first GitHub repo is created, including one (1) commit at branch master that locked in the README.md markdown document. All summarised on the repo page shown below.

Github - My first repo with a single commit.

SSH connection

With SSH keys, you can connect to GitHub without supplying your username or password at each visit. Thie section is a summary of GitHub’s manual for Connecting to GitHub with SSH.

Check for existing SSH keys

First check for any existing SSH keys. As SSH keys are OS dependent, follow the instructions for your OS at Checking for existing SSH keys.

Generating a new SSH key and adding it to the ssh-agent

If you do not have an existing SSH key and wish to generate one, follow the GitHub instructions Generating a new SSH key and adding it to the ssh-agent. The Mac OSX instructions are summarised here. Open a Terminal and then enter:

$ ssh-keygen -t rsa -b 4096 -C “your_email@example.com”

(ssh-keygen -t rsa -b 4096 -C “thomas.karttur@gmail.com”)

(ssh-keygen -t rsa -b 4096 -C “thomas.gumbricht@gmail.com”)

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/thomasgumbricht/.ssh/id_rsa):
Created directory '/Users/thomasgumbricht/.ssh'.
Enter passphrase (empty for no passphrase):

thelma22

Your identification has been saved in /Users/thomasgumbricht/.ssh/id_rsa.
Your public key has been saved in /Users/thomasgumbricht/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:v0J0NJLG8IOGN10bY1tu7wxeGxsLUIA0gObpOIFJKDE thomas.karttur@gmail.com
The key's randomart image is:
+---[RSA 4096]----+
|E.   .o=oo*.o    |
|oo  o. +*+oO     |
|oo o..=.+o+.o    |
|o . oo ....o .   |
|   +   .S.  o =  |
|  o .   .. . * * |
|   .   .  . . *  |
|        .  .     |
|         ..      |
+----[SHA256]-----+

$ pico ~/.ssh/config

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa

$ ssh-add -K ~/.ssh/id_rsa

Enter passphrase for /Users/thomasgumbricht/.ssh/id_rsa:
Identity added: /Users/thomasgumbricht/.ssh/id_rsa (thomas.karttur@gmail.com)

$ ssh-add -K ~/.ssh/id_rsa_karttur

Enter passphrase for /Users/thomasgumbricht/.ssh/id_rsa:
Identity added: /Users/thomasgumbricht/.ssh/id_rsa (thomas.karttur@gmail.com)

Copy the contents of the id_rsa.pub file to the clipboard:

$ pbcopy < ~/.ssh/id_rsa.pub

$ pbcopy < ~/.ssh/id_rsa_karttur.pub

GitHub SSH setup

This section describes how to configure your GitHub account to use the SSH key, it is a shortened version of the GitHub page Adding a new SSH key to your GitHub account.

github-menu-select-settings

Go to your GitHub account at GitHub.com. Click on your avatar, in the drop down menu, select Settings (shown to the left).

github-settings-menu-SSH-keys




In the user settings window that opens, look at the sidebar for SSH and GPG keys (as shown to the right), and click it.










In the page for SSH-keys and GPG-keys that opens, click the New SSH key button (below).

Github - SSH keys page.

A new page, “SSH keys / Add new”, opens (below), In the Title field, add a descriptive label for the new key. The title should reflect both the machine (owner) and the lock (the GitHub repo). Then paste the content of the clipboard, captured with the pbcopy command, in the Key field. When done, click Add SSH key.

Github - add a New SSH key.

github-add-SSH-key.confirm-pswd

If prompted to confirm with your GitHub password, do that.

You should now have the same SSH key defined in your machine and on GitHub. And be ready for seamlessly linking your remote and local git repos.

Github - list of SSH keys.

Grab the SSH for a repo

Return to the GitHub page with the repo (“my-first-repo”) that you created above. Towards the right side, approximately mid down there is a Clone or Download button (shown below).

Github - Repository clone or download potions.

github-repo-clone-download-set-SSH

Click on the Clone or Download button, the default is Clone with HTTPS (shown in thus full view figure above), if you click the smaller text “Use SSH” the setting will change to Clone with SSH (shown to the right). Do that. And then copy the string in the textbox starting with git@github.com…. This string will allow us to connect from a local git repo directly to the remote repo on GitHub.

Clone to local repo

Open a Terminal window and cd to the parent directory where you want to create the clone of your remote (GitHub) repository. Then enter:

$ git clone

If you followed the instuctions in the previous section, you can now just paste the SSH link from your clipboard to the command line:

$ git clone git@github.com:thomasgumbricht/my-first-repo.git

Execute the command and your remote rep (“my-first-repo”) should clone to your local machine. If this is the first time you use the SSH key, you will be prompted the following statement and question:

Cloning into 'my-first-repo'...
The authenticity of host 'github.com (140.82.118.4)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?

Answer with a full yes

Warning: Permanently added 'github.com,140.82.118.4' (RSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
Receiving objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

Local repo settings

The first post of this series explains how to Check and setup user name. For the cloned repo (“my-first-repo”) you can setup a local user and email that matches the SSH key. First cd to the clone of the repo, and then set user and email:

$ cd my-first-repo
$ git config –local user.name thomasgumbricht
$ git config –local user.email thomas.karttur@gmail.com

git remotes

The command git remote returns the remotes for the repo. With the terminal window still point to the cloned repo (“my-first-repo”), type:

git remote

origin

The name origin is like a git alias for the default remote repository. To see the full paths (urls) and the rights that we have for remotes, type:

git remote -v

origin	git@github.com:thomasgumbricht/my-first-repo.git (fetch)
origin	git@github.com:thomasgumbricht/my-first-repo.git (push)

git log

In the post on git branches the following command was introduced as a means to get a condensed graphical view of the branches and the commit history:

$ git log –all –decorate –oneline –graph

* 9f30e5b (HEAD -> master, origin/master, origin/HEAD) Initial commit

For the clone we are working with the HEAD pointer points towards two new items: origin/master and origin/HEAD. As origin can be seen as an alias for the primary remote repo, the pointing is towards the reomte (GitHub) repo. As HEAD is pointing both towards the local master and the remote master, our local repo is in sync with the GitHub repo.

Edit origin/master

to try the different functions for linking a remote and a local repositories, return to your online GitHub account and the origin/master repo (“my-first-repo”). When in that repo, click the Create new file button.

Github - click the Create new file button.

In the window that opens, give the name of the new document in the textbox that says Name your file…. Then enter a few lines in the area for Edit new file..

Github - Create new file.

When done, scroll down towards the bottom of the page, GitHub has created a default (short) message. If you accept that message just click the Commit new file button.

Github - Commit new file.

The edits you made are staged and commited by GitHub, and you are returned to the front page of your online repo. If you look, it now says you have two commits, and in the list of contents you can see two files, each with its own commit.

Github - My first repo with two commits.

git fetch

Return to the Terminal window that points to your local clone of the GitHub (remote) repo. Your local copy should now be behind the online (orign/master). However, your local git is not yet aware of the latest online commit. You can check that out by either git status or git log. For your local git clone to catch up with online changes, you need to run git fetch.

$ git fetch or $ git fetch origin

remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:thomasgumbricht/my-first-repo
   9f30e5b..9cd2c08  master     -> origin/master

Check the status of your local repo:

$ git status

On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working tree clean

You can explore the situation graphically:

$ git log –all –decorate –oneline –graph

* 9cd2c08 (origin/master, origin/HEAD) Create Chapters.md
* 9f30e5b (HEAD -> master) Initial commit

The local master is trailing origin/master.

git merge

The post on git branches in local repositories outlined how to use git merge. As you have not done any edits to your local (receiving) repo, the merge of the origin/master can be done as a fast-forward merge (straight update of the HEAD pointer to the latest commit):

$ git merge origin/master

Updating 9f30e5b..9cd2c08
Fast-forward
 Chapters.md | 5 +++++
 1 file changed, 5 insertions(+)
 create mode 100644 Chapters.md

You can check that your local clone is in sync with orgin/master by either git status or git log.

git pull

The git pull command combines git fetch and git merge into a single command. Both online GithHub repos and the Github Desktop app have predefined functions for pull (rather than fetch + merge). It is, nevertheless, often better to use fetch + merge.

git push

The command git push is used for sending local clone changes to remote repos. To test it, make some edits to the local copy of Chapters.md. You can use any editor, for instance pico:

$ pico Chapters.md

...
## Add another chapter

Hit [ctrl]+[X] to exit pico and save the edits by pressing Y when asked.

commit the changes with the combined command:

$ git commit -am “Added chapter to Chapters.md”

[master 0b4547f] Added chapter to Chapters.md
 1 file changed, 2 insertions(+)

The local clone of the repo is now 1 commit ahead of origin/master. To put them in sync again, git push the local changes to the remote repo:

$ git push origin master

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 339 bytes | 169.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:thomasgumbricht/my-first-repo.git
   9cd2c08..0b4547f  master -> master

A quick look at your GitHub online repo (remember to refresh the page) reveals that you have now three commits, with the latest message being “Added chapter to Chapters.md”.

Github - My first repo with three commits.