This guide is created to empower developers, IT professionals, and learners to master Git and GitHub through real-world concepts, examples, and workflows. Whether you're building solo projects or leading enterprise teams, this book will help you work faster, safer, and smarter with version control.
Dedicated to all learners who believe in mastering their craft.
Git is a distributed version control system (VCS) used to track changes in code. Every developer has a full copy of the repository, enabling offline work and safe collaboration.
Use Git for software projects (web, mobile, APIs). Typical flow:
Working Directory -> Staging Area -> Local Repository -> Remote Repository
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git init # Initialize a repo
git clone # Clone a remote repo
git status β show changesgit add <file> / git add . β stage filesgit commit -m "message" β commit staged changesgit log β view commit historygit diff β view unstaged changesBranches isolate work.
git branch # list branches
git branch feature-x # create branch
git checkout feature-x # switch branch
git checkout -b feat/one # create & switch
git merge feature-x # merge into current
git remote add origin
git push -u origin main
git pull
git fetch
git restore <file> β discard unstaged editsgit reset <file> β unstagegit commit --amend β edit last commitgit revert <commit> β create a new commit that undoes specified commitMerge preserves history and creates merge commits. Use for public branches. Rebase rewrites commits to create a linear history; use on local feature branches before merging.
When conflicts occur, edit files to resolve markers (<<<<<< etc.), then git add and git commit.
git stash # save uncommitted work
git stash list
git stash apply # reapply stash
git stash pop # apply and remove
Tags mark releases.
git tag v1.0
git tag -a v1.0 -m "Release 1.0"
git push origin v1.0
Apply an individual commit from another branch:
git cherry-pick <commit_id>
git revert <id> creates a new commit to undo changes (safe). git reset moves HEAD (rewrites history, be careful).
git log --oneline
git log --graph --oneline --decorate
git show <commit>
.gitignore to keep secrets out of the repo.git reflog can recover lost commits.git bisect to find the commit that introduced a bug.GitHub hosts Git repositories and adds collaboration tools: PRs, Issues, Actions (CI/CD), Projects (kanban), and security features.
Common strategies:
Create feature branch β push β open PR β assign reviewers β merge after approval. Keep PRs small and focused. Use templates and checks.
Automate building, testing, and deploying. Example workflow for Laravel (YAML):
name: Laravel CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with: { php-version: '8.2' }
- run: composer install
- run: php artisan test
- uses: SamKirkland/FTP-Deploy-Action@v4.3.2
with:
server: ${{ secrets.FTP_SERVER }}
username: ${{ secrets.FTP_USERNAME }}
password: ${{ secrets.FTP_PASSWORD }}
Run unit tests, linters (ESLint), formatters (Prettier), and dependency checks (Dependabot) in your CI pipeline.
Use Issues for bugs, tasks, and feature requests. Use labels, assignees, and milestones. Reference issues in commits: closes #12 will auto-close the issue on merge.
Use Projects to manage progress visually: To Do β In Progress β Done. Link issues and PRs to cards for traceability.
Create releases to tag and document stable versions. Use annotated tags and attach build artifacts if needed.
Use Wiki for project docs and Discussions for broader community Q&A and idea brainstorming.
Define roles (Owner, Admin, Maintainer, Write, Read) and set team permissions. Use CODEOWNERS to auto-request reviews for specific paths.
Clone copies a repo you control locally. Fork copies someone else's repo to your account for contributions.
git checkout -b feature/xyzProvide a CONTRIBUTING.md describing branch naming, testing, commit messages, and how to open PRs.
/api/* @backend-team
/frontend/* @frontend-team
Use interactive rebase (git rebase -i main) and squash to keep commit history clean before merging.
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git checkout main
git merge upstream/main
Use Git LFS for large binaries (git lfs track "*.zip"). Remove unnecessary large files from history if needed.
Git empowers teams and individuals to manage code changes, collaborate, and automate workflows. This book covered fundamentals through advanced CI/CD, security, and open-source collaboration.
Example: Adding a leaderboard to a Laravel app.
git checkout -b feature/leaderboard
# implement UI and API
git add .
git commit -m "Added leaderboard UI"
git push origin feature/leaderboard
# open PR and merge after review
git reflog & git checkout -b branch <hash>git add β git commitgit revert <id> (safe), or git reset for local history editsQ: Difference between Git and SVN?
A: Git is distributed; SVN is centralized.
Q: Pull vs Fetch?
A: git fetch downloads; git pull downloads and merges.
Q: Rebase vs Merge?
A: Rebase rewrites history making it linear; Merge preserves branch history.