Git & GitHub for Beginners

Git & GitHub for Beginners

Learn Git from scratch with practical workflows, essential commands, branching strategies, and collaboration using GitHub.


What is Git?

Git is a distributed version control system used to:

  • Track code changes
  • Maintain project history
  • Collaborate with teams
  • Restore older versions of files
  • Manage software releases safely

Git stores snapshots of a project over time, allowing developers to experiment without losing previous work.


What is GitHub?

GitHub is a cloud platform built around Git.

It helps developers:

  • Host repositories online
  • Collaborate using Pull Requests
  • Review code changes
  • Manage issues and projects
  • Showcase portfolios

Git is the tool. GitHub is the hosting platform.


Installing Git

Download Git

https://git-scm.com/downloads

Verify Installation

git --version

Initial Git Configuration

Set the username and email associated with commits.

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

Verify configuration:

git config --list

Understanding the Git Workflow

Git works using three main areas:

Working Directory
       ↓
   git add
       ↓
 Staging Area
       ↓
 git commit
       ↓
 Local Repository
       ↓
   git push
       ↓
 Remote Repository

Key Concepts

Area Purpose
Working Directory Files currently being edited
Staging Area Changes prepared for commit
Repository Stored project history

Creating a Repository

Initialize a New Repository

git init

Clone an Existing Repository

git clone https://github.com/user/repo.git

Basic Git Workflow

Check Repository Status

git status

Add Files

git add file.txt
git add .

Commit Changes

git commit -m "Add login feature"

View Changes

git diff

Viewing Commit History

git log
git log --oneline
git log --graph

View a specific commit:

git show <commit_id>

Working with Branches

Branches allow independent development without affecting the main codebase.

Create a Branch

git branch feature-login

Switch Branches

git switch feature-login

Create and Switch Together

git switch -c feature-login

List Branches

git branch

Delete a Branch

git branch -d feature-login

Merging Branches

Merge a feature branch into main:

git switch main
git merge feature-login

Working with Remote Repositories

Add Remote Repository

git remote add origin https://github.com/user/repo.git

View Remote Repositories

git remote -v

Push Code

git push -u origin main

Pull Latest Changes

git pull origin main

Fetch Changes Without Merging

git fetch

Undoing Changes

Unstage a File

git restore --staged file.txt

Discard Local Changes

git restore file.txt

Amend the Last Commit

git commit --amend -m "Updated commit message"

Reset vs Revert

Soft Reset

Keeps changes locally.

git reset --soft HEAD~1

Hard Reset

Removes changes permanently.

git reset --hard HEAD~1

Revert Commit

Creates a new commit that undoes changes safely.

git revert <commit_id>

Stashing Changes

Temporarily store unfinished work.

git stash
git stash list
git stash apply
git stash drop

Resolving Merge Conflicts

When two branches modify the same lines, Git creates a conflict.

After resolving conflicts manually:

git add .
git commit

Using Tags

Tags are commonly used for software releases.

git tag v1.0
git push origin v1.0

Using .gitignore

Prevent unnecessary files from entering version control.

Example:

__pycache__/
.env
*.log
node_modules/

Essential Git Terminologies

Term Meaning
Repository Project managed by Git
Commit Snapshot of changes
Branch Independent line of development
Merge Combining branches
Clone Copying a repository
Pull Downloading latest changes
Push Uploading commits
Remote Online repository
HEAD Current commit reference
Stash Temporary storage for changes

Common Git Commands Cheat Sheet

git init
git clone <url>
git status
git add .
git commit -m "message"
git push
git pull
git branch
git switch -c feature
git merge feature
git log --oneline
git stash
git reset --hard

Best Practices

  • Commit small logical changes
  • Write meaningful commit messages
  • Pull before pushing changes
  • Use feature branches
  • Avoid committing secrets
  • Keep .gitignore updated
  • Review changes before committing

Example End-to-End Workflow

git clone <repo-url>

cd repo

git switch -c feature-login

git add .

git commit -m "Add login feature"

git push origin feature-login