Github for beginners

Author

These exercises where authored and tested by Kasper Thystrup Karstensen

Assignment

Part 1: First time setup, first repository, and Github calibration

Introduction

This assignment is designed to guide you through the process of setting up a new repository, connecting it to Github, and configuring your local Git environment.

Task

  1. On your Unix terminal, set up your user email to match the one you created a GitHub account with, using git config --global user.email "YOUR_GitHub@E.MAIL".
  1. Set up the authorship name (called user.name in git terminology), this name will be visible in the log files on your local and online repository. Use git config --global user.name "YOUR AUTHOR NAME".
  1. Navigate to the folder where you want to keep your repository on your machine. Let’s say you want to keep them in a folder called repo located at your home directory, yet the directory does not yet exist. First make the folder using mkdir ~/repo and then go into the new folder cd ~/repo
  1. Log into your Github account and create a new empty repository. Provide a meaningful name (E.g. my_first_repository). Consider to make it public as it would not require login information whenever you push changes to the repository from your local machine → You can always delete the repository when this exercise is done!.
  1. Make sure to check the Add a README file then click Create repository.
  1. Click the Code button, select the SSH pane, and copy paste the box contents by clicking on the icon next to the address., By now, the repository page should look something along the lines of this:
  1. Head back to your Unix terminal, from the ~/repo folder, pull the repository down to your local machine: git pull ADDRESS Where you replace ADDRESS with the copied link. e.g. git@github.com:GitHubUserName/my_first_repository.git

Part 2: Adding your scripts to the repository

Introduction

This assignment is designed to guide you through the process of adding your scripts to your newly created repository, and making sure they are properly tracked using Git.

Task

  1. In your Unix terminal, Generate a decoy file (empty file) in the repository using: touch awesome_script.py.
  1. Check the status of your repository with git status.
  1. Stage the latest changes (the empty file) using git add awesome_script.py and git commit. Be sure to put in a helpful title and perhaps a detailed description.
  1. Push the local changes to Github using git push origin main.
  1. Inspect your repository on Github to ensure that it matches your local repository (remember to refresh the page after pushing).
  1. Head back to the Unix terminal (make sure you are still at the repository folder → e.g. ~/repo/my_first_repository and create a script folder inside the repository folder (mkdir script ), then copy the empty file into the script folder.
  1. Edit the README.md file and write some random description in the file. Make sure to save the file before exiting.
  1. Check the latest detected changes using git status. You can see that the README and the script folder are untracked.
  1. Add the scripts folder and README.md file to the repository and commit.

Part 3: Managing multiple versions with branching

Introduction

This assignment is designed to guide you through the process of branching, a key concept in Git that allows you to work on different versions of your code in parallel.

Task

  1. Check existing branches using git branch.
  1. Generate a new branch (recommended name: dev) using git switch --create dev.
  1. As branching can be a bit difficult to keep a grasp on, grab a paper, draw a line labelled main, and draw a parallel branch from somewhere in the line and label it with the branch name (e.g., dev).
  1. Make another decoy script inside the script folder touch script/another_awesome_script.sh.
  1. Read the output of a git status command and make sure you are at the correct branch (dev).
  1. Do a git add script/ and commit changes this time using git commit -m "Added another awesome script" -m "This script can actually save the world, by spreading complete awesomeness over the entire metaverse - by shutting it down...". What do you think the two -m arguments does?
  1. Inspect the commits history by doing a git log --graph (--graph is an optional argument). Are there the expected amount of commits?
  1. Head back to the main branch, first by doing a git switch main and then following with a checkup: git status
  1. Check the contents of the script folder ls script/. How many files are there now?
  1. Push the new branch (dev) to your remote Github repository using git push origin dev.
  1. On Github, navigate over to the dev branch and confirm that your changes have indeed been pushed.

Part 4: Bringing it all together with merge

Introduction

This assignment is designed to guide you through the process of merging different branches together.

Task

  1. To merge the latest changes, change position to the branch which will ingest the target branch, in this case main will ingest the contents of dev. Use git switch main.
  1. Check the current status prior to merging git log --graph.
  1. Now merge the feature branch into the development branch using git merge dev.
  1. Rerun the above log command and inspect the output.
  1. Normally, this would be a good time to delete the branch, which just got ingested, using a git branch -d BRANCH_NAME. However in this case, we will do an exception, as the dev branch are continously being used to push new development, even unstable ones. Instead of deleting the dev branch, lets just push everything. git push origin main dev
  1. Inspect the results on GitHub.

Resolving conflicts

While you shouldn’t experience conflicts in this exercise. However, if you were to fill out the another_awesome_script.sh and awesome_script.py files with text, shifting back and forth between branches, could introduce conflicts. That is, you have made different changes to a specific section, which git requires help to resolve, if that where the case, the below guide is made to aid in resolving such conflicts.

Task

  1. For each conflicting file
    1. Open the conflicting file.
    1. Inspect <<<<<HEAD (current version at master).
    1. Inspect the ====== … >>>>>>>dev (the target version that is attempted to merge into master).
    1. Decide whether the HEAD or the dev contains the correct text
    1. Remove the wrong / outdated text alongside with the >>>, ===, <<<, HEAD, and dev sections.
    1. Add the conflicting files to your repository with git add and commit the changes with git commit.