As promised this week is a sneak preview from one of the chapters in TechOneTwenty
If you’ve been on the fence about TechOneTwenty, this ones for you.
It’s all about…..Git & GitHub!
What is GitHub?
Core Knowledge
Getting Started
Real-World Example ~ Micro Project
With awesome diagrams like this:
Let’s get into it…
Psst… This post is probably to long for your email inbox.
What is GitHub?
Before we can understand what GitHub is, we need to internalise a new concept: Version Control. Learning this is essential if you plan to work in a team.
Right now, you’re probably working on projects at home by yourself. At the end of the day, you save your work, turn off your PC, and the next day, you come back to find nothing has changed, allowing you to carry on working. However, when you join any business, from start-ups to global enterprises, you’ll be collaborating on projects with others. Changes made by different team members to the project can conflict, important modifications might get lost in the shuffle, and tracing back to earlier versions becomes a headache.
With a Version Control System, like Git (not to be confused with GitHub), we can easily manage source code, branching, and merging changes across multiple contributors. Git operates locally on a developer's machine, enabling them to work offline and efficiently manage complex projects. That’s quite hard to grasp if you’re new, so let’s break it down.
Effectively, Git is software designed to monitor and document alterations made to project files. Breaking it down even further, Git tracks changes you make to code.
After implementing changes, users have the option to 'commit' these changes to a 'repository' (commonly referred to as a 'repo'), essentially serving as a comprehensive log of all adjustments made.
If you like games, I recommend checking out:
https://ohmygit.org/ - an open-source game about learning Git!
Now we can talk about GitHub…
Core Knowledge
GitHub is by far the most popular choice for this. It's a web-based platform used for version control and collaboration on software projects.
At a high level, it’s a secure cloud-based place for engineers and developers to store their source code. It has a ton of cool features like “Actions” (for CI/CD) and “Secrets Detection.” GitHub itself offers some incredible training on these features.
Chances are you’ve probably already looked at a repo:
Let’s take another look at the problem GitHub solves with these diagrams, again pay close attention to the explained bubbles:
In steps GitHub:
I remember in my first job, I was making a change to a security group in Terraform and I misspelled ‘security’ which, as a wanna-be Cybersecurity professional, was pretty embarrassing. GitHub and GitLab are the two most popular solutions for version control; they offer similar functionalities but differ in terminology. To keep things simple, we'll focus on GitHub.
Getting Started:
When you first start using Git and GitHub, it can be pretty frustrating. Everything has to be done in the correct order with the correct commands, but soon you’ll be pushing to production on a Friday afternoon.
To help you get started, here are the main terms you will hear and use over and over:
Clone: Copying a repository from a remote source to your local machine, enabling you to work on the code locally.
Pull: Fetching and integrating changes from a remote repository into your local repository.
Push: Uploading your local changes to a remote repository, making them accessible to others
Fork: Creating a personal copy of a repository within your own GitHub account, allowing you to make changes without affecting the original project. I’ll expand on this with a diagram; it confused me at first.
Pull Request: A request to merge changes from one branch or fork into another, often used for code review and collaboration.
Master Branch: The default branch in a Git repository, traditionally used to represent the stable version of the project.
Merge: Combining changes from one branch into another, typically used to incorporate completed work into the main codebase.
Main Branch: Similar to the master branch,
it's a default branch name that has gained popularity as an alternative term.
Feature Branch: A branch created to work on a specific feature or task, enabling isolation of changes from the main codebase until they're ready to be merged.
Dev-Branch: A branch used for ongoing development work, separate from the main or master branch.
Commit: A commit is a snapshot of the changes made to files in a repository at a specific point in time. When you commit changes in Git, you're essentially saving your work with a message describing the changes made. Commits serve as a record of the project's history and allow you to track and revert changes if needed.
Real-World Example ~ Micro Project
Okay, let’s get some hands-on. Follow these steps and you’ll have your repo up and running to store all your projects in no time.
First, we need Git. Check out the install instructions here:
For most Ubuntu and Debian-based distro’s, you want to run:sudo apt-get install git
For Windows, the set-up wizard will do most of the work and guide you through the setup process, it’s really simple!
https://www.git-scm.com/download/win
For macOS, Install homebrew, if you don't already have it, then:brew install git
Next, you need to sign up for a GitHub account which can be done here:
https://github.com/
After you’ve signed up we need to create anew repository: The green one in the top left that says “new”
Here, we need to give it a name and a short description, if you want a ReadMe file (think of these as documentation about the repo) and if you want it to be public or private - Pick Private for now.
We now have a fresh repo, with just the README file, notice this has the contents of your description.
Now that we have our new repo on Github we need a way of getting it onto our local machine so we can start adding files and building out our project.
Hit the <Code> drop-down and select the SSH tab. This should come up with a short explanation and link to “add a new public key” - follow that link and leave this tab open.
Back on your local terminal, we need to generate an SSH key.
--------------------------------------------------
Side Note: What is an SSH key?
An SSH key is a cryptographic tool used to securely access remote servers. It involves a private key (kept secret) and a public key (shared with the server) to authenticate users without needing passwords, enhancing security in remote connections. I HIGHLY recommend you get comfortable with SSH keys conceptually, proper use, generation and storage.
--------------------------------------------------
We can generate a new SSH key by typing the following:ssh-keygen -t rsa -b 4096
We then need to copy the value of the public key (NOT PRIVATE) into the tab we left open, into the space for our SSH key. You can open the public SSH key with:cat /<pathyouset>/.ssh/id_rsa.pub
Navigate back to your repo and you should now be able to copy & paste the text that appears when we select SSH.
Back on the terminal let’s configure Git:git config --global user.name “<your user name>”
git config --global user.email “<your email >”
Copy the text from GitHub into your terminal with “Git Clone” like this:git clone git@github.com:<yourname>/TechOneTwenty-GitProject.git
You should now see your project has been pulled locally. Move into your new project repo with the “cd” command. Here if you type “ls” you should see “README.md”. We are about to start work on our project so we need to create our own branch:git checkout -b scriptadd
Create a sample script:touch techonescript.sh
Populate it with something:
#!/bin/bash
for i in {1..5}
do
echo "Hello :) Hope you're enjoying the project"
done
You now want to add files to the staging area. Type the following to do this.git add .
I am using “.” here to denote everything the directory should be added but you could specify the individual file if you wanted to only push the single file (git add techonescript.sh
).
Followed by:git commit -am "feat: adding my script"
This will stage all modified tracked files and commit them with the message "feat: adding my script."
-----------------------------------------------------------------
Note: Understanding Conventional Commits
You're probably wondering what "feat" is. It's quite good practice to add conventional commits before your actual message.
feat: Adding a new feature.
fix: Indicates resolving a bug.
docs: Updating documentation.
wip: Work in progress.
-----------------------------------------------------------------
Almost done. We now need to use the following command to push the “scriptadd” branch to the remote repository named origin and set it as the upstream branch, meaning future git push and git pull commands will default to this branch.git push --set-upstream origin scriptadd
Back on Github, you will notice a pop-up informing you a branch has had a recent push. Click on “Compare & Pull Request”
Add a good description and click on “Create Pull Request”
Finally, you will have the ability to “Merge Pull Request”
This is what you would send to another engineer to review and approve. Since we are only testing, we can press this.
Back on your main repo page, you should now see your code/files that were local on this repo, in the main branch
This is how it looks all together:
Congrats you’ve just pushed your first protect up to Github to join over 98 Million other repositories!
Now a friend, dog or fellow engineer can add to your project.
Remember main branches - this is what another engineer will be on when they first grab your project. They will need to clone your project, checkout a branch, to the work and then merge back to the main branch, or they could branch off a branch.
That can be confusing so here’s how it looks, I’m going to refer to this picture again, now you have some more context:
Final Tips
Regularly Pull Changes: Frequently pull changes from the main branch to keep your branch up to date and avoid merge conflicts. You might be caught off guard by someone else's recent merge/PR.
Write Clear Commit Messages: Ensure your commit messages are clear and descriptive to help others understand the changes you've made.
First PR: The scariest part of your career, whatever field of tech you go into, will be your first PR.
Wrap up
GitHub is a web-based platform that facilitates version control and collaborative software development. Simply, people share code here. It’s 100% not as hard as it first seems It allows developers to store, manage, and track changes to their code through repositories. With features like branching, pull requests, and issues, GitHub simplifies collaboration, enabling teams to work efficiently and from anywhere in the world - Development would look very different if it didn’t exist.
GitHub, when used correctly, can serve as an excellent portfolio for all your projects. Make it something to be proud of.
This has been a sneak peak from my eBook
You can order it here: TechOneTwenty
Thank you for reading: Keep it secure, keep it light-hearted!
WJPearce - CyberBrew
Perfect. For years I’ve been frustrated and confused by Git Hub. The principle of a repository is easily understood but as an uninitiated always difficult to know where to start. Now I know!