Branching & Forking on Github

Video recording for this session




Table of contents



A few weeks ago, we learned how to contribute to a Github repository on which you are a collaborator. The goal was to modify the content directly. This works if two conditions are met:

This may not always be the case. In fact, it’s possible that you and your collaboror(s) on this repository may be contributing edits and additions to the same files and folder simultaneously. This will undoubtedly create conflicts which will need to be resolved by at least one person with appropriate permissions to the repo.

But, there are far better ways to collaborate on GitHub without runnign into these pesky problems! We will be visiting two main techniques today.









































































Prep

For today’s exercise, you will be performing roles of two collaborators simultaneously. For this reason, do the following:

  1. Get a second github account (first, you will need to get a second email address. You can’t use the same email address for two different github accounts).

  2. Sign into this new account using an incognito browser window

  3. Sign into your regular github account using normal browser window

We will refer to these two accounts as follows. Your account names will be different obviously, so make sure to keep track of which is which.

  1. for the incognito account, go ahead and generate a new ssh key-pair as follows. Note that it’s not necessary to generate a separate key. In fact, you can use the same ssh-key pair for both accounts and setting up ~/.ssh/config file accordingly. But I have found it to be a hassle and having separate keys simplifies things.
ssh-keygen -t rsa -b 4096 -C "your_new_email@host.com"
  1. Add information on your new key to the ~/.ssh/config file as follows:
Host puffin.github.com
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_puffin
  1. Associate your new public key with your new github account.
cd ~/.ssh/

pbcopy < id_rsa_puffin.pub
  1. In your incognito browser window, go inside your github account, and add the copied key. Name it accordingly on the website form.

  2. Now you are ready to rock n roll.



1. Collaborating with branches

This section assumes that your workflow is that of a collaborator.


1.1 penguin invites puffin

  1. With your penguin account, create a new private repository named climate.

  2. Leave all options unchecked

  3. Invite your puffin account as a collaborator

  4. In your puffin account, accept the invitation


1.2 penguin generates initial content

  1. Create a folder named climate and initialize it as a git repo
cd ~/Github

mkdir climate

cd climate

git init
  1. Create a README.md and generate following content in it.
---
title: Climate Change from 1983 to 2008
---


## Exploratory Data Analysis (EDA)

- Import the data into R and check variable names.
library(tidyverse)
library(plotly)
library("RColorBrewer")

climate <- read_csv("data/climate_change.csv")

names(climate)
- Check how the data is organized
str(climate)

spec_tbl_df [308 × 11] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ Year    : num [1:308] 1983 1983 1983 1983 1983 ...
 $ Month   : num [1:308] 5 6 7 8 9 10 11 12 1 2 ...
 $ MEI     : num [1:308] 2.556 2.167 1.741 1.13 0.428 ...
 $ CO2     : num [1:308] 346 346 344 342 340 ...
 $ CH4     : num [1:308] 1639 1634 1633 1631 1648 ...
 $ N2O     : num [1:308] 304 304 304 304 304 ...
 $ CFC-11  : num [1:308] 191 192 193 194 194 ...
 $ CFC-12  : num [1:308] 350 352 354 356 357 ...
 $ TSI     : num [1:308] 1366 1366 1366 1366 1366 ...
 $ Aerosols: num [1:308] 0.0863 0.0794 0.0731 0.0673 0.0619 0.0569 0.0524 0.0486 0.0451 0.0416 ...
 $ Temp    : num [1:308] 0.109 0.118 0.137 0.176 0.149 0.093 0.232 0.078 0.089 0.013 ...
 - attr(*, "spec")=
  .. cols(
  ..   Year = col_double(),
  ..   Month = col_double(),
  ..   MEI = col_double(),
  ..   CO2 = col_double(),
  ..   CH4 = col_double(),
  ..   N2O = col_double(),
  ..   `CFC-11` = col_double(),
  ..   `CFC-12` = col_double(),
  ..   TSI = col_double(),
  ..   Aerosols = col_double(),
  ..   Temp = col_double()
  .. )
- Explore the change in CO2 levels over the years using a barplot.
climate_change_chart <- ggplot(climate, aes(x = Year, y = Temp, fill = CO2)) + 
    xlab("Year") +
    ylab("Temperature") + 
    theme_minimal(base_size = 14)
barplot <- climate_change_chart +
    geom_bar(position = "dodge", stat = "identity", color = "white")

ggplotly(barplot)

ggsave("co2_1983_2008.png")
<center>
<img src="co2_1983_2008.png" width=750 />
</center>
  1. Save and close the file.


1.3 Configure local repository and push upstream

  1. Tell git about your github repo
git config user.name "penguin"

git config user.email "penguin@arctic.info"

git remote add origin git@github.com:penguin/climate.git
  1. Now add, commit and push the repo upstream.
git add .

git commit -m "initial commit by penguin"

git push -u origin master


1.4 puffin clones the repo

cd ~/

mkdir Github2

cd Github2

ssh-add ~/.ssh/id_rsa_puffin

git clone git@github.com:penguin/climate.git

1.5 puffin creates a new branch

git branch

* master
git checkout -b puffin-update
## Temperature changes through time
library(lubridate)

climate_change_ymd <- climate %>% 
    mutate(year_month = ymd(paste(climate$Year, climate$Month, truncated = 1)))

L1  <- ggplot(climate_change_ymd, aes(year_month, Temp)) +
    geom_line() +
    geom_smooth(se=FALSE, linetype="dotted") +
    labs(title = "Temperature (1983-2008)",
        x = "Year",
        y = "Temperature") +
    theme(plot.title = element_text(hjust=0.5))

ggplotly(L1)

ggsave("temperature_years.png")
<center>
<img src="temperature_years.png" width=750 />
</center>


1.6 puffin pushes new branch upstream

git config user.name "puffin"
git config user.email "puffin@arctic.com"

git config --list
git remote -v
git add .

git commit -m "Updated README.md

git push -u origin puffin-update

1.7 penguin merges the new branch into main





2. Collaborating with forks