0.3: GitHub
Learning Objectives
GitHub is a code-hosting website that hosts Git repos for individuals or teams to review and collaborate on
Know how to fork a repo on GitHub
Know how to clone a repo from GitHub
Know how to push changes to GitHub
Know how to pull changes from GitHub
Know how to view repo commit history in GitHub
Introduction
GitHub is a code-hosting website that hosts Git repos for individuals or teams to review and collaborate on. Team members can easily review latest code changes and commit history, making software development more transparent and thorough.
GitHub Workflow Summary
Fork repos we do not have edit access to to suggest changes or maintain our own copy
Clone repos to download local copies of GitHub repos
Once we have committed the changes we want locally, push our changes to GitHub to share them with others. Refresh the GitHub repo page to see those changes.
If we are working with teammates and wish to download their code while keeping local changes, pull their code from GitHub after committing our local changes.
GitHub Fork
A GitHub "fork" is a copy of another GitHub repo. SWEs typically "fork" repos they do not have edit access to either to make improvements to merge back into original repos, or to create and maintain independent versions of repos. At Rocket Academy we will fork Rocket exercise repos to complete and submit assignments.
We can fork a repo by clicking the Fork button on a GitHub repo page. Once forked, we can change our copy of the repo without affecting the original.
Git Clone
Once we have edit access to the repo we want to edit, either by forking an existing repo or creating a new repo, we can "clone" (i.e. download) that repo to our local machine to make changes to it.
Click the copy button in the Code dropdown menu on the GitHub page of the repo we wish to edit.
Then go to terminal, cd
to the relevant folder and enter the command git clone <repo-url>
, where <repo-url>
is the URL we just copied from GitHub. This will create a new folder named after the repo with the repo's contents inside.
Once we've cloned the repo we can make edits to it and track our changes with Git.
Git Push
git push
allows us to share local changes by "pushing" local commits to GitHub for others to view. Video demo below.
Git Pull
git pull
allows us to download new changes in a shared GitHub repo (e.g. by teammates) while keeping our local changes. Git will automatically merge downloaded and local changes, and let us know if there are "merge conflicts", for example if downloaded and local changes edit the same lines of code. We will work more with git pull
once we start group projects.
How to view commit history in GitHub
GitHub provides an easy way to view past changes to a repo. For example, if we are wondering which commit changed a line of code that caused a bug, we can easily find which commits changed that line, who made those commits and what other changes were in those commits.
Additional Resources
Git and GitHub in Plain English (blog post)
Git and GitHub by The Coding Train (video playlist)
(Below) Intro to GitHub video from prior version of Rocket's Coding Basics course
(Below) GitHub Fork video from prior version of Rocket's Coding Basics course
(Below) GitHub Repo Browsing video from prior version of Rocket's Coding Basics course
In the below GitHub Fork video we demonstrate git push origin master
, but for most purposes git push
will suffice.