DEV Community

Cover image for Git & GitHub Mastery: From Zero to Version Control Hero
Macphalen Oduor
Macphalen Oduor

Posted on

Git & GitHub Mastery: From Zero to Version Control Hero

A Complete Beginner's Guide to Installing Git Bash, Connecting to GitHub, and Mastering Version Control


Content Guide

  • Introduction: Why Git Matters
  • Part 1: Installing Git Bash
  • Part 2: Connecting Git to Your GitHub Account
  • Part 3: Understanding Version Control
  • Part 4: Pushing and Pulling Code
  • Part 5: Tracking Changes Like a Pro
  • Real-World Scenarios
  • Common Pitfalls and Solutions
  • Conclusion

Introduction: Why Git Matters

Imagine working on a project and accidentally deleting hours of work. Or picture collaborating with teammates where everyone's changes constantly overwrite each other's code. Sounds like a nightmare, right?

This is exactly why Git exists. Git is a version control system that tracks every change you make to your code, allowing you to travel back in time, collaborate seamlessly, and work without fear of losing your progress.

GitHub, on the other hand, is like social media for code. It's where developers store their projects, collaborate with others, and showcase their work to the world.

By the end of this guide, you'll be pushing code to GitHub like a seasoned developer. Let's dive in!


Part 1: Installing Git Bash

Git Bash is a terminal application that brings Git's power to your fingertips, especially if you're on Windows. Here's how to get started:

Step 1: Download Git Bash

Navigate to the official Git website and download the installer for your operating system:

Git Download Page
Fig: Git Download Page

Step 2: Install Git Bash

For Windows users, run the downloaded installer. You'll encounter several configuration screens. Here's what to choose:

  • Default editor: Select your preferred text editor (VS Code, Vim, Nano, etc.)
  • PATH environment: Choose "Git from the command line and also from 3rd-party software"
  • Line ending conversions: Select "Checkout Windows-style, commit Unix-style line endings"
  • Terminal emulator: Choose "Use MinTTY"
  • Leave other options at their defaults unless you have specific preferences

Click "Install" and let the magic happen!

Git Installation Page
Fig: Git Installation Page

Step 3: Verify Installation

Open Git Bash (search for it in your Start menu on Windows, or open Terminal on Mac/Linux) and type:

git --version
Enter fullscreen mode Exit fullscreen mode

You should see something like:

git version 2.52.0
Enter fullscreen mode Exit fullscreen mode

Congratulations! Git is now installed on your machine.


Part 2: Connecting Git to Your GitHub Account

Now that Git is installed, let's connect it to your GitHub account. This is like giving Git your credentials so it knows who you are.

Step 1: Create a GitHub Account

If you haven't already, head to https://github.com and sign up for a free account. Choose a professional username since this will be part of your developer identity!

Github Signup Page

Fig: Github Signup Page

Step 2: Configure Git with Your Identity

Open Git Bash and run these commands, replacing the placeholders with your actual information:

git config --global user.name "My Name"
git config --global user.email "my_email@gmail.com"
Enter fullscreen mode Exit fullscreen mode

Important: Use the same email address you used for your GitHub account.

To verify your configuration, run:

git config --list
Enter fullscreen mode Exit fullscreen mode

You should see your name and email listed.

Step 3: Authenticate with GitHub

GitHub has moved away from password authentication. Instead, we use either SSH keys or Personal Access Tokens. Let's set up SSH keys (the recommended method):

Generate an SSH Key

ssh-keygen -t ed25519 -C "my_email@gmail.com"
Enter fullscreen mode Exit fullscreen mode

Press Enter to accept the default file location. You can optionally add a passphrase for extra security (or just press Enter to skip).

Add Your SSH Key to the SSH Agent

Start the SSH agent:

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

Add your SSH private key:

ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

Copy Your SSH Public Key

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

This will display your public key. Copy the entire output (it starts with ssh-ed25519).

Add SSH Key to GitHub

  1. Go to GitHub and click on your profile picture (top right)
  2. Select Settings
  3. Click SSH and GPG keys in the left sidebar
  4. Click New SSH key
  5. Give it a title (e.g., "My Laptop")
  6. Paste your public key into the "Key" field
  7. Click Add SSH key

Adding SSH Key to Github Interface

Fig: Adding SSH Key to Github Interface

Test Your Connection

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

You should see a message like:

Hi YourUsername! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

Perfect! You're now connected to GitHub.


Part 3: Understanding Version Control

Before we start pushing code, let's understand what's actually happening under the hood.

What is Version Control?

Version control is like a time machine for your code. It tracks every change, who made it, and when. Think of it as creating save points in a video game—you can always go back to a previous state if something goes wrong.

Key Concepts

Repository (Repo)

A repository is a folder that Git tracks. It contains all your project files plus a hidden .git folder where Git stores all the version history.

Commit

A commit is a snapshot of your code at a specific point in time. Each commit has:

  • A unique ID (hash)
  • A message describing what changed
  • Information about who made the change and when

Think of commits as chapters in your project's story.

Branch

Branches allow you to work on different features simultaneously without affecting the main codebase. The default branch is usually called main or master.

Imagine you're writing a book. The main branch is your published manuscript, while feature branches are drafts where you experiment with new chapters.

Remote

A remote is a version of your repository hosted on the internet (like on GitHub). This allows you to backup your work and collaborate with others.

The Git Workflow

Here's the typical Git workflow:

  1. Working Directory: Where you edit files
  2. Staging Area: Where you prepare files for commit
  3. Repository: Where commits are stored
  4. Remote Repository: Where your code lives on GitHub

Git Workflow Illustration

Fig: Git Workflow Illustration


Part 4: Pushing and Pulling Code

Now for the exciting part—let's actually work with Git and GitHub!

Creating Your First Repository

Option A: Start Locally

Create a new folder for your project:

mkdir my-awesome-project
cd my-awesome-project
Enter fullscreen mode Exit fullscreen mode

Initialize Git:

git init
Enter fullscreen mode Exit fullscreen mode

This creates a .git folder in your directory, turning it into a Git repository.

Create a simple file:

echo "# My Awesome Project" >> README.md
Enter fullscreen mode Exit fullscreen mode

Option B: Clone an Existing Repository

If a repository already exists on GitHub:

git clone git@github.com:username/repository-name.git
cd repository-name
Enter fullscreen mode Exit fullscreen mode

The Sacred Git Workflow: Add, Commit, Push

This is the mantra you'll repeat hundreds of times in your developer career:

1. Check Status

Always start by checking what's changed:

git status
Enter fullscreen mode Exit fullscreen mode

This shows you:

  • Modified files (red)
  • Staged files (green)
  • Untracked files

2. Stage Your Changes

Add specific files to the staging area:

git add README.md
Enter fullscreen mode Exit fullscreen mode

Or add all changed files:

git add .
Enter fullscreen mode Exit fullscreen mode

Pro tip: The staging area is like a shopping cart. You're selecting which changes you want to include in your next commit.

3. Commit Your Changes

Create a snapshot with a descriptive message:

git commit -m "Add README with project description"
Enter fullscreen mode Exit fullscreen mode

Writing Good Commit Messages:

  • Use present tense ("Add feature" not "Added feature")
  • Be descriptive but concise
  • Start with a verb (Add, Fix, Update, Remove)

Examples:

  • ✅ "Add user authentication feature"
  • ✅ "Fix navigation bug on mobile devices"
  • ❌ "stuff"
  • ❌ "changes"

4. Push to GitHub

First, if this is a brand new repository, create it on GitHub:

  1. Go to GitHub.com
  2. Click the "+" icon (top right) → "New repository"
  3. Name it (same as your local folder)
  4. Don't initialize with README (we already have one)
  5. Click "Create repository"

Connect your local repository to GitHub:

git remote add origin git@github.com:yourusername/my-awesome-project.git
Enter fullscreen mode Exit fullscreen mode

Push your code:

git branch -M main
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • git branch -M main: Renames your default branch to "main"
  • git push -u origin main: Pushes your commits to GitHub and sets up tracking

For subsequent pushes, you can simply use:

git push
Enter fullscreen mode Exit fullscreen mode

Pulling Changes from GitHub

When collaborating with others or working on multiple machines, you'll need to pull changes:

git pull
Enter fullscreen mode Exit fullscreen mode

This fetches changes from GitHub and merges them into your local branch.

What's actually happening:

  1. git fetch: Downloads changes from GitHub
  2. git merge: Integrates those changes into your current branch

You can also do these separately:

git fetch origin
git merge origin/main
Enter fullscreen mode Exit fullscreen mode

Part 5: Tracking Changes Like a Pro

Git's real power lies in tracking changes. Let's explore the essential commands.

Viewing Your Commit History

See all commits:

git log
Enter fullscreen mode Exit fullscreen mode

This shows a detailed history with commit hashes, authors, dates, and messages.

For a condensed view:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

For a beautiful graphical view:

git log --graph --oneline --all
Enter fullscreen mode Exit fullscreen mode

Viewing Changes

See what you've modified (before staging):

git diff
Enter fullscreen mode Exit fullscreen mode

See staged changes (after git add):

git diff --staged
Enter fullscreen mode Exit fullscreen mode

Compare two commits:

git diff commit1-hash commit2-hash
Enter fullscreen mode Exit fullscreen mode

Checking File History

See who changed what in a file:

git blame filename.txt
Enter fullscreen mode Exit fullscreen mode

View all commits that modified a file:

git log -- filename.txt
Enter fullscreen mode Exit fullscreen mode

Undoing Changes

Unstage a File (Undo git add)

git restore --staged filename.txt
Enter fullscreen mode Exit fullscreen mode

Discard Changes in Working Directory

⚠️ Warning: This permanently deletes your local changes!

git restore filename.txt
Enter fullscreen mode Exit fullscreen mode

Undo the Last Commit (Keep Changes)

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

Your files remain changed, but the commit is undone.

Undo the Last Commit (Discard Changes)

⚠️ Warning: This permanently deletes your changes!

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

Working with Branches

Create a new branch:

git branch feature-login
Enter fullscreen mode Exit fullscreen mode

Switch to that branch:

git checkout feature-login
Enter fullscreen mode Exit fullscreen mode

Or do both at once:

git checkout -b feature-login
Enter fullscreen mode Exit fullscreen mode

List all branches:

git branch
Enter fullscreen mode Exit fullscreen mode

Merge a branch into main:

git checkout main
git merge feature-login
Enter fullscreen mode Exit fullscreen mode

Delete a branch:

git branch -d feature-login
Enter fullscreen mode Exit fullscreen mode

Real-World Scenarios

Let's walk through some practical situations you'll encounter.

Scenario 1: Daily Work Routine

# Start your day
git pull

# Create a feature branch
git checkout -b add-contact-form

# Make changes to your files...

# Check what changed
git status

# Stage your changes
git add .

# Commit with a descriptive message
git commit -m "Add contact form with validation"

# Push to GitHub
git push -u origin add-contact-form

# Create a Pull Request on GitHub for code review
Enter fullscreen mode Exit fullscreen mode

Scenario 2: Collaborating with a Team

# Pull latest changes before starting work
git checkout main
git pull

# Create your feature branch
git checkout -b fix-header-alignment

# Work on your feature...

# Meanwhile, your teammate merged their changes
# Pull the latest main branch
git checkout main
git pull

# Merge main into your feature branch to stay updated
git checkout fix-header-alignment
git merge main

# Resolve any conflicts, then continue...

# Push your changes
git push
Enter fullscreen mode Exit fullscreen mode

Scenario 3: Fixing a Mistake

You committed something wrong:

# Undo the commit but keep your changes
git reset --soft HEAD~1

# Fix the files...

# Stage and commit again
git add .
git commit -m "Correct implementation of user validation"
Enter fullscreen mode Exit fullscreen mode

You pushed bad code to GitHub:

# Create a new commit that reverses the changes
git revert HEAD

# Push the revert
git push
Enter fullscreen mode Exit fullscreen mode

⚠️ Common Pitfalls and Solutions

Problem 1: Merge Conflicts

What happened: Two people changed the same lines of code.

Solution:

git pull
# You'll see: CONFLICT (content): Merge conflict in filename.txt
Enter fullscreen mode Exit fullscreen mode

Open the file and look for conflict markers:

<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> branch-name
Enter fullscreen mode Exit fullscreen mode

Edit the file to keep the correct version, remove the markers, then:

git add filename.txt
git commit -m "Resolve merge conflict in filename.txt"
git push
Enter fullscreen mode Exit fullscreen mode

Problem 2: Accidentally Committed Secrets

Never commit passwords, API keys, or sensitive data!

Solution:

# Remove the file from Git (but keep it locally)
git rm --cached secrets.env

# Add it to .gitignore
echo "secrets.env" >> .gitignore

# Commit the changes
git add .gitignore
git commit -m "Remove secrets from version control"
git push
Enter fullscreen mode Exit fullscreen mode

Important: The secret is still in your Git history. For true security, rotate your credentials immediately.

Problem 3: Detached HEAD State

What happened: You checked out a specific commit instead of a branch.

Solution:

# Create a new branch from here
git checkout -b recovery-branch

# Or go back to main
git checkout main
Enter fullscreen mode Exit fullscreen mode

Problem 4: Pushing Rejected

Error: "Updates were rejected because the remote contains work that you do not have locally"

Solution:

# Pull the changes first
git pull

# Resolve any conflicts

# Push again
git push
Enter fullscreen mode Exit fullscreen mode

Conclusion: Your Git Journey Begins

Congratulations! You've just learned the fundamentals of Git and GitHub. You now know how to:

  • Install and configure Git Bash
  • Connect to GitHub with SSH
  • Understand version control concepts
  • Push and pull code confidently
  • Track changes and navigate history
  • Handle common scenarios and problems

Your Next Steps

  1. Practice Daily: Use Git for every project, even small ones
  2. Explore GitHub: Star repositories, contribute to open source
  3. Learn Advanced Topics: Rebasing, cherry-picking, Git hooks
  4. Read Documentation: https://git-scm.com/doc

Essential Commands Cheat Sheet

# Configuration
git config --global user.name "My Name"
git config --global user.email "my_email@gmail.com"

# Repository Setup
git init                    # Initialize a repository
git clone <url>            # Clone a repository

# Basic Workflow
git status                 # Check status
git add <file>            # Stage a file
git add .                 # Stage all changes
git commit -m "message"   # Commit changes
git push                  # Push to remote
git pull                  # Pull from remote

# Branching
git branch                # List branches
git branch <name>         # Create branch
git checkout <name>       # Switch branch
git checkout -b <name>    # Create and switch
git merge <branch>        # Merge branch

# History
git log                   # View commits
git log --oneline         # Condensed view
git diff                  # View changes

# Undoing
git restore <file>        # Discard changes
git reset --soft HEAD~1   # Undo last commit
Enter fullscreen mode Exit fullscreen mode

Remember: Everyone makes mistakes with Git. The difference between a beginner and an expert is that the expert has made more mistakes and learned from them. Don't be afraid to experiment in a test repository!

Happy coding, and welcome to the world of version control!


Connect with me on GitHub: @mac0duor0fficial-1028

Top comments (0)