Mark Mwangi logoMark
Mwangi
Return to home
Get in
touch
Admin
login
Version ControlJune 15, 202616 min read

Git: A Comprehensive Guide for Developers

From core concepts to advanced workflows — branching, rebasing, cherry-picking, merge conflicts, and clean history with hooks and PRs.

By Mark Mwangi
Git: A Comprehensive Guide for Developers cover coverImage

Git is the most widely used version control system, yet many developers only learn a handful of commands. This guide goes from the core model to advanced workflows so you can use Git with confidence rather than fear.

1. The core model

  • **Commit** — a snapshot of your files with a parent pointer.
  • **Branch** — a lightweight movable pointer to a commit.
  • **HEAD** — where you currently are.
  • **Index/Staging** — what will go into the next commit.
bash
git init
git add -p          # stage hunks interactively
git commit -m "message"

2. Branching and merging

Branches are cheap. Use one per feature:

bash
git switch -c feature/login
# ... work ...
git switch main
git merge feature/login

A fast-forward merge just moves the pointer; a merge commit records that two histories joined.

3. Rebasing

Rebase replays your commits onto another base, producing a linear history.

bash
git fetch origin
git rebase origin/main

Rule of thumb: rebase local commits; never rebase commits others have pulled.

4. Cherry-picking

Apply a single commit from another branch:

bash
git cherry-pick a1b2c3d

Useful for hotfixes you want in multiple branches without a full merge.

5. Merge conflicts

When Git can't auto-combine, it marks the file. Resolve, stage, and continue (see the dedicated merge-conflicts guide). The key is to understand both sides before choosing.

6. Keeping history clean

  • One logical change per commit.
  • Write messages that explain *why*, not just *what*.
  • Use `git commit --amend` and interactive rebase to tidy before sharing:
bash
git rebase -i HEAD~5

7. Hooks for automation

Hooks run on events. A pre-commit hook can lint and format so bad code never enters history:

bash
# .git/hooks/pre-commit
npm run lint && npm run format

Or use Husky + lint-staged for a managed setup.

8. Pull requests and collaboration

  • Push a branch and open a PR.
  • Keep PRs small and focused for fast review.
  • Use branch protection (required reviews, passing CI).

Quick reference

TaskCommand
New branchgit switch -c name
See statusgit status
Stage partgit add -p
Linearizegit rebase
Copy commitgit cherry-pick
Undo commitgit revert

Mastering Git isn't about memorizing commands — it's about understanding that commits form a graph you can reshape. Once that clicks, every workflow becomes a tool, not a mystery.

Version ControlDevelopmentBest Practices
Go back to all blogs →Go to all projects →Contact →

Related Articles

  • The case for monorepos in modern web development
  • The importance of documentation in software development
  • Implementing M-Pesa payment gateway in a web application

About

Mark Mwangi

Creating secure and meaningful digital experiences.

Software developer focused on secure, responsive, and high-quality digital experiences.

Socials

GitHubLinkedInXE-mailinstagramfacebookwhatsappMedium

© 2026 Mark Mwangi | All rights reserved

Terms of Service•Privacy Policy

Last updated: April 30, 2026 16:23:51 UTC