Fundamentals 17 min read

Master Git: From Basics to Advanced Branching, Merging, and Rebase

This comprehensive guide demystifies Git by explaining its distributed architecture, file states, commit nodes, HEAD, remote repositories, and essential commands for adding, committing, branching, merging, rebasing, cherry-picking, and reverting, empowering developers to confidently manage code history and collaborate effectively.

Architect's Guide
Architect's Guide
Architect's Guide
Master Git: From Basics to Advanced Branching, Merging, and Rebase

Basic Concepts

Git Advantages

Git is a distributed version‑control system. Unlike centralized systems such as SVN, Git stores the full history locally, allowing commits without network access and enabling fast, flexible workflows.

File States

Modified : Files changed in the working directory.

Staged : Files added to the index with git add.

Committed : Files permanently recorded in the repository.

Commit Nodes

Each commit creates a node identified by a SHA‑1 hash. Nodes form a linear chain (ignoring merges). The diagram below illustrates a simple commit chain.

HEAD

HEAD is a reference that points to the current commit (the working directory). It can point directly to a commit or to a branch, in which case it follows the branch’s tip.

Remote Repository

While commits live locally, a remote repository stores the shared history. git clone copies the repository and its references. The remote state is not updated automatically; git fetch must be run to refresh remote branches.

Branching

What Is a Branch?

A branch is a lightweight pointer to a commit. Multiple branches can exist simultaneously, each representing an independent line of development.

Example: after releasing v1.0, development continues on v1.1 while a hot‑fix for v1.0 is made on a separate branch.

Branches allow parallel work without duplicating code; they are merely references, so storage overhead is minimal.

Command Details

Commit‑Related Commands

git add <file>

– stage a specific file. git add . – stage all changes. git checkout -- <file> – discard changes in the working directory. git reset HEAD <file> – unstage a file. git commit -m "message" – create a new commit.

Branch‑Related Commands

git branch <name>

– create a new branch pointing to the current commit. git checkout <name> – switch to an existing branch. git checkout -b <name> – create and switch to a new branch in one step. git branch -d <name> – delete a branch that is no longer needed.

Merge‑Related Commands

git merge <branch|hash>

– integrate another branch or commit into the current branch.

Fast‑forward merges occur when the current branch is an ancestor of the target; otherwise a new merge commit is created. Conflicts must be resolved manually when the same lines are changed in both branches.

Rebase

git rebase <branch|hash>

– replay commits onto another base, producing a linear history.

Rebase yields a cleaner, linear commit graph but may require repeated conflict resolution. It does not preserve the original timestamps of the rebased commits.

Cherry‑Pick

git cherry-pick <hash>

– apply a specific commit onto the current branch.

This is useful for selectively copying changes without merging entire branches.

Revert / Reset

git checkout <hash>

or git checkout --detach – detach HEAD to a specific commit. git checkout <branch>/HEAD^ – move HEAD to the previous commit using a relative reference. git reset HEAD~N – move the current branch and HEAD back N commits. git commit --amend – modify the most recent commit without creating a new node.

Remote‑Related Commands

git clone <repo‑url>

– copy a remote repository locally. git fetch <repo> <branch> – download new commits and references without merging. git pull <branch> – fetch then merge the remote branch into the current branch. git pull --rebase <branch> – fetch then rebase instead of merging. git push <remote> <branch> – upload local commits to the remote repository.

Push may fail if the remote has diverged; a preceding git pull (or git pull --rebase) is typically required to resolve conflicts before pushing.

Conclusion

HEAD and branches are lightweight references; together they enable Git’s distributed nature.

Merge preserves chronological history, while rebase creates a linear, cleaner log; choose based on workflow needs.

Moving HEAD lets you inspect any commit without altering branch pointers.

Cloning or fetching stores the full remote history locally. git pull is essentially fetch followed by merge, optionally using --rebase for a linear integration.

mergerebaseVersion Controlbranchingresetcherry-pick
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.