DEV Community

Daniel Mutwiri Mbabu
Daniel Mutwiri Mbabu

Posted on

The Paradox of Git and GitHub: Version Control

Often we get caught in the crossfire of big technological terminology, and we have so little to do but get blasted. For me, and I bet many others, Git and GitHub must be one of those! But for how long can we sit by and feel absolutely clueless, yet we can armour up at our own will? That's what I intend to do with this article. Let's break down this paradox together!

So What is Git and GitHub?

Are they branches of the same Franchise that chose different names? Certainly not.

Term Definition
Git It's the tool that tracks your changes to your projects and runs locally
GitHub It's a website that stores your Git history in the cloud for Collaboration

Git

Git is a version control system that runs on your local machine, tracking every change you make to your code over time. It allows you to go back to any previous state, and lets multiple people work on the same project at the same time without overwriting each other.

Without Git, teams share code by emailing files or editing the same folder. Both approaches lead to the same outcome, but in this case, someone's work gets lost. Git solves this permanently.

Every developer has the full project history on their own machine - not just the latest version.

GitHub

GitHub is the website that stores your git history/git repositories in the cloud. (It does not run locally on your machine). It stores repositories online to enable collaboration.

Mainly used to share code, review changes, manage issues, and Continuous Integration and Continuous Delivery (CI/CD)

In a nutshell
Git is like MS Word that tracks all the changes, while GitHub is the Google Drive where you upload the Word document for collaboration.

Common terminologies used in the Git/GitHub Environment...

Term What it means
Repository (repo) It's a Git Project Folder that Git is tracking.
Commit This is a saved snapshot/state of your project at a specific point in time.
Branch It's a parallel version of the project - you experiment here without touching the main code.
Remote It's the online stored copy of the repo, usually on GitHub.
Push It's to send your commits/changes to GitHub
Pull It's to bring others' commits down to your local machine.

So how do you get started on Git and GitHub?...

Installing Git

Linux (Ubuntu)

Run the commands below:

sudo apt update
sudo apt install git -y
git --version          # confirm: git version 2.x.x

Enter fullscreen mode Exit fullscreen mode

Windows

Download from https://git-scm.com/download/win

Always run Git commands inside Git Bash CLI, not on other CLIs such as Command Prompt or PowerShell.

macOS

xcode-select --install    # installs Git via Xcode CLI tools
# OR if you have Homebrew:
brew install git
Enter fullscreen mode Exit fullscreen mode

Note: After the installation. A first-time configuration is necessary to connect your local Git to GitHub (Remote).

Opening Git Bash CLI for Windows and first-time configuration.

  • Create a New Folder (Location? Upto you)

  • Right-click the newly created Folder to select more options if you cannot view "Open Git Bash Here".

  • The Git Bash will open, and you have to initialize Git to do any Configurations. To initialize Git, run the command: git init

  • Assuming you already have a GitHub account set up, run these commands in Git Bash for the first-time configuration.
git config --global user.name  "Your Full Name"
git config --global user.email "you@example.com"
Enter fullscreen mode Exit fullscreen mode

Where: "Your Full Name" = "GitHub Account Name"
"you@example.com" = "Email address to the GitHub Account Name"

Creating Your GitHub Account

  1. Go to https://github.com and click Sign up
  2. Choose a professional username
  3. Verify your email address
  4. Add a profile picture and a short bio

Quick Question

After configuring Git, how does the actual information flow between local (Git) and remote (GitHub)?

Answer.

SSH / HTTPS

The Communication Channels.

SSH (Secure Shell Protocol) uses a key pair to prove your identity without ever sending a password across the network.
HTTPS is an application layer protocol responsible for securely transporting the Git communication between your computer and GitHub.

SSH vs HTTPS.

Which one do I use? Both are secure communication methods. You should not think SSH is secure and HTTPS is not. With HTTPS, GitHub uses a personal access token (PAT) or a credential manager rather than your GitHub account password.

Ideally:

  • Use SSH if it's your own machine, and you use GitHub regularly.
  • Use HTTPS if you're on a machine/network where SSH setup is inconvenient, restricted, or temporary.
SSH HTTPS
Authentication Automatic - key pair handles it silently Requires a Personal Access Token (not your password - GitHub removed that in 2021)
Setup effort 15 minutes once per machine Token needed for each machine; expires and must be refreshed
Daily use Push and pull with no prompts at all Prompted for credentials unless you store the token
Best for Personal machines, servers, daily work One-off cloning; environments where SSH port is blocked

THE CORE FLOW

                   YOUR COMPUTER
            ┌─────────────────────┐
            │                     │
            │   Working Directory │
            │          ↓          │
            │        git add      │
            │          ↓          │
            │   Staging Area      │
            │          ↓          │
            │       git commit    │
            │          ↓          │
            │   Local Repository  │
            │                     │
            └─────────┬───────────┘
                      │
                   git push
                      │
                      ▼
            ┌─────────────────────┐
            │       GitHub        │
            │                     │
            │ Remote Repository   │
            └─────────────────────┘
                      │
                   git pull
                      │
                      ▼
            ┌─────────────────────┐
            │   Local Repository  │
            └─────────────────────┘
Enter fullscreen mode Exit fullscreen mode

To break this down,

1. Create/change a file locally
Suppose your GitHub repository is:

JCARS
Enter fullscreen mode Exit fullscreen mode

You clone it onto your computer:
git clone git@github.com:Danielmutwiri/JCARS.git

You will have all the files related to JCARS, e.g., the README.md file, etc

Git creates a local repository containing the project's history and also remembers where the remote repository is.

To verify,
git remote -v

You will see

origin git@github.com:Danielmutwiri/JCARS.git (fetch)
origin git@github.com:Danielmutwiri/JCARS.git (push)

Origin is the conventional name given to a GitHub repository.

2. You modify a file
Suppose you modify:

README.md
Enter fullscreen mode Exit fullscreen mode

Git will see README.md (Modified), but GitHub knows nothing about this change yet. Changing a file does not automatically change your Git repository or GitHub.

3. git add moves the change to staging

git add README.md

The change moves from the working directory to the staging area
It's like saying:

"Git, I want this particular change to be included in my next commit."

To verify, use this command:

git status

4. git commit stores the change locally
git commit -m "Update README.md"

Git creates a commit in your local repository. But GitHub still hasn't received the commit yet.

5. git push sends your commits to GitHub
git push origin main

This is where the actual communication with GitHub occurs. Git transfers the necessary Git commit information to the remote repository. GitHub then updates its remote branch, and now they are in sync.

6. What happens if some other developer changes GitHub?
On their computer, a change has happened, say 'change A'. Also, GitHub is updated, but your local PC is lagging. Use git fetch/git pull to retrieve information from GitHub and get your local PC up to speed.

git fetch origin

Git will contact GitHub and retrieve information about changes.

fetch is relatively safe: it downloads information but does not automatically modify your working files.git pull however, goes one step further.

git pull origin main

Git will perform: git fetch + git merge. Your local branch is then brought up to date. Modern Git can also be configured to use rebase instead of merge.

Finally

GitHub is not the "master copy" of Git itself. Your local repository contains the full Git history too. GitHub is a remote repository that provides collaboration, backup, hosting, and pull requests. Now you have the power to go and do some version control.

Top comments (0)