Introduction to GitHub
Introduction to GitHub
Before covering GitHub, we will cover the basic concepts of Version Control and Git. Version Control (VCS)
Version Control (or Version Control System) refers to the management of changes in a single file, folder, or project (multiple folders). By storing records of the changes made, past versions of the same document can be reviewed at a later date or restored permanently. To demonstrate through a very common problem:
- Bob is working on a document called homework.docx
- The document is ready for submission, he renames it to Bob_UniversityNumber_A1.docx - Bob wants to make further changes, but also wants a copy of the current document. - Bob renames Bob_UniversityNumber_A1.docx to Bob_UniversityNumber_A1_v1.docx - Bob duplicates Bob_UniversityNumber_A1_v1.docx as Bob_UniversityNumber_A1_v2.docx - Every time Bob is not assertive of the changes he has made, he repeats this process.
At the end of the semester, Bob’s folder looks something like this screenshot:
Applications such as an Office suite will often have a version control feature built in, such as the review tab and OneDrive sync in Microsoft Word. In this screenshot, uncommitted changes by the user are coloured in red:
To enable the Track Changes feature:
- Click on the Review tab from the ribbon.
- Click on Tracking, a drop-down menu will appear.
- Click on Track Changes, a second drop-down menu will appear.
- Click on Just Mine.
For SIM Students, you can sync your documents to enable Version History with your SIM OneDrive:
Further information on Microsoft Word’s “Track Changes” feature can be found here.
Similarly, Google Docs offers a similar feature called Version History. Unlike Microsoft Word, this feature is enabled by default when using Google Docs. Further information on Google Docs’ “Version History” feature can be found here.
Therefore, in Bob’s case, enabling version control would have eliminated the need for multiple duplicates of the same document. Now, Bob wants the same feature when working with files that do not support version control. Examples of such files include the .txt file or common filetypes for programming languages like the .java or .py file. Thus, Bob decides to use Git.
What is Git?
For beginners, Git is often used to track changes to a file that is stored locally. Installation of Git is very straightforward on both Windows and macOS. Upon installation, basic familiarity with the command line is required. If you prefer to use a graphical interface, skip to the GUI section, and revisit this post at a later date.
On Windows, start PowerShell by hitting the “⊞” and “x” key together, and select “Windows PowerShell”. The following window would appear, where SIMITC would be your username:
Alternatively, from File Explorer, right click on an empty area (i.e. do not right click on a file or folder) and select “Git Bash Here”.
If Windows’ Terminal is installed, users also have the option to right click and select “Open in Terminal”.
Accessing a folder through the command line is done with cd or chdir, which stands for change directory. Once you are familiar with the cd command, set your name and email for git (also shown below), and enter the folder which contains the files you’d like to track changes with:
PS: C:\Users\SIMITC> git config –global user.name “Bob”
PS: C:\Users\SIMITC> git config –global user.email “firstname.lastname@example.org”
PS: C:\Users\SIMITC> cd Documents\Blog
PS: C:\Users\SIMITC\Documents\Blog> git init
PS: C:\Users\SIMITC\Documents\Blog> git add .
PS: C:\Users\SIMITC\Documents\Blog> git commit -m “Describe your commit succinctly.” PS: C:\Users\SIMITC\Documents\Blog> git log
A brief summary of the git arguments in layman terms:
init Initializes the folder, where any file in this folder can be added into the version control.
add Adds files to be recorded in the version control
To add a single file: `git add homework.txt`
To add multiple files: `git add homework.txt website.html data.py`
add . Records all files in this folder, inclusive of any subfolders into the version control. commit Confirm the changes made, similar to Word’s “Accept All Changes” from the Review tab. log Shows you all the commits you’ve made
You can check out this cheat sheet to get a better idea of these commands and more, or simply enter:
PS: C:\Users\SIMITC\Documents\Blog> git
Two useful commands to explore when working locally are `git diff` (to compare different versions of a file) and `git status` (to list the files which contain changes or were deleted, etcetera, for the next commit). Other commands which are commonly used are the `clone`, `fetch`, `pull`, and `push` commands. To get a clearer understanding of these commands, we will proceed with the introduction of GitHub.
With GitHub, you can upload your git repositories online, and view the repositories of other users. Repositories can be set to public or private, and can also be initialized from the site itself, meaning you would not need to execute `git init`. After signing up for an account, you have the option to create a git repository from the site, or `push` an existing local repository to GitHub. For the sake of convenience and usability, we will only cover repositories initialized through GitHub. Click the aforementioned link (while logged in to GitHub) to create a new repository.
Repository Creation & Cloning
Before cloning the repository into our local directory, we first have to select a mode of authentication. Sounds confusing, so to put it simply:
PS: C:\Users\SIMITC\Documents> git clone
PS: C:\Users\SIMITC\Documents> cd MyRepositoryName
Say you have made a few changes to your local repository, and you’ve executed `git commit`. The next step is to update those changes on GitHub, which would be done with `git push`. If your git username and email matches the one on GitHub, this approach will work correctly. There are other approaches (which are the modes of authentication) such as cloning with SSH or using the GitHub CLI tool. Note that GitHub CLI only works with GitHub, though there are alternatives to git hosting such as GitLab, Codeberg, BitBucket, and more. Thus, familiarizing with git on the command line guarantees a workflow that is compatible when collaborating with code or files hosted on sites other than GitHub.
Creating an SSH key to work with Git
The following is one of many suggested configurations of generating an SSH key:
PS: C:\Users\SIMITC> ssh-keygen -o -a 100 -t ed25519 -C $(git config –global user.email)
The output below will appear:
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\SIMITC/.ssh/id_ed25519):
Hit the “Enter” key, which saves the key in the default directory.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Enter your passphrase/password. As seen in the prompt above, you must enter your password twice.
Your identification has been saved in C:\Users\SIMITC/.ssh/id_ed25519.
Your public key has been saved in C:\Users\SIMITC/.ssh/id_ed25519.pub.
The key fingerprint is:
The key’s randomart image is:
An SSH key contains a public and private key. Never ever share the private key file “id_ed25519” with anyone. We have to copy the contents of the public key file “id_ed25519.pub” to GitHub. Open this file in notepad:
C:\Users\SIMITC> notepad $HOME/.ssh/id_ed25519.pub
A new notepad window should launch. Subsequently, add the SSH key into your GitHub account:
- Set a name for your key in “Title”.
- Leave the “Key type” as “Authentication Key”.
- Paste the contents of the entire id_ed25519.pub file (opened in notepad) into “Key”. 4) Click on “Add SSH key”.
- Now, you can clone git repositories with git clone, as seen below:
PS: C:\Users\SIMITC> git clone git:github.com/MyVeryRealTotallyNotFakeUserName/MyRepositoryName.git
When prompted, enter your password which you have chosen for your SSH key. Afterwards, the repository which you have made on GitHub will be successfully cloned into your local directory, which would be my home folder for my case.
PS: C:\Users\SIMITC> cd MyRepositoryName
PS: C:\Users\SIMITC\MyRepositoryName> ls
Using the command `ls` will list all the files or directories I have in the folder. This is not a git command, it is built-in with Windows’ PowerShell, macOS’ Terminal, and all shells in Linux.
The only exception is Windows’ Command Prompt, which uses `dir`. Okay off topic, back to git and GitHub. Using a Graphical Interface instead of `git` or `github` in command line
Chances are, you would prefer to use a graphical user interface (GUI) to perform git operations. In this case, my personal recommendation is to use an integrated Git or GitHub plugin/extension with your text editor. As an example, there are official plugins for git on VS Code and IntelliJ IDEA, both of which require no installation besides installing git itself. If you want a standalone GUI tool for GitHub, look into installing GitHub Desktop or many of the other interfaces available for your operating system.
Working with Git and GitHub is definitely daunting at first, and it is likely that users will lean towards making repositories and commits directly from GitHub itself. There is no sole “correct” approach when users are working on individual projects. Thus, pick the approach that caters to your convenience and workflow best.
Nevertheless, it helps to understand the git command line workflow, as most fields in computer science and IT involve a large amount of collaborative coding or making changes to a remote system. The command line interface is portable, lightweight, and the syntax (for git) is the same across the three major platforms, namely Windows, macOS, and Linux distributions.
Take small steps with exploring the features available in Git and GitHub; there are many which have not been covered in this article! When in doubt, consult the official documentations for Git or GitHub, or Q&A sites like StackOverflow or Quora. Take it easy, stay safe, and happy VCS.