Fundamentals 18 min read

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

This comprehensive guide explains Git’s core concepts—including its distributed advantages, file states, commit nodes, HEAD, remote repositories, branching strategies, and detailed command usage for adding, committing, merging, rebasing, cherry‑picking, detaching HEAD, and rolling back—so readers can confidently manage version control workflows.

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

1. Basic Concepts

Git is a distributed version‑control system that stores the entire history locally, unlike centralized systems such as SVN which require network access for every commit.

Centralized: All code lives on a central server; each commit must contact the server, leading to frequent merges and higher coordination cost. Distributed: Commits are created locally, automatically backed up, and each developer can clone the full repository, making Git the typical choice.

Because each commit creates a lightweight snapshot, rolling back to a previous state is simple and fast.

1.2 File States

Modified: Files changed in the working directory are detected by Git.

Staged: git add <path> moves modified files to the staging area, preparing them for a commit.

Committed: git commit records the staged changes as a new permanent node.

1.3 Commit Nodes

Each commit generates a unique SHA‑1 hash and forms a linear chain of nodes (unless merges create branches). The hash uniquely identifies the snapshot.

1.4 HEAD

HEAD is a pointer that references the current commit (the working directory). It can point directly to a commit or to a branch, which in turn points to a commit.

1.5 Remote Repository

While Git stores history locally, collaboration requires a remote repository. git clone copies the entire repository, including all branches and HEAD references, to the local machine.

Always ensure the remote repository contains only tested code before pushing.

2. Branches

2.1 What Is a Branch?

A branch is a lightweight pointer to a commit. Multiple branches can exist simultaneously, allowing parallel development streams such as feature branches, release branches, or hot‑fix branches.

Example: After releasing version 1.0, development continues on version 1.1 while a hot‑fix for 1.0 is made on a separate branch.

Creating a branch does not duplicate code; it only creates a new reference, keeping storage overhead minimal.

3. Command Details

3.1 Adding and Committing

Add a specific file: git add <file_path> Add all files: git add . Undo changes in the working directory: git checkout -- <file_name> Clear a file from the staging area: git reset HEAD <file_name> Commit staged changes:

git commit -m "commit message"

3.2 Branch Operations

Create a branch: git branch <branch_name> Switch to a branch: git checkout <branch_name> Create and switch in one step: git checkout -b <branch_name> Delete a branch:

git branch -d <branch_name>

3.3 Merging

Common merge commands: git merge <branch_or_commit> – creates a new merge commit unless a fast‑forward is possible.

Fast‑forward occurs when the target branch is strictly ahead of the current branch, resulting in both pointers moving to the same commit.

When histories diverge, Git creates a new merge commit (e.g., C5) that combines the changes.

If the same line is edited in both branches, a conflict occurs and must be resolved manually.

3.4 Rebase

Rebase rewrites history by moving a series of commits onto a new base: git rebase <branch_or_commit> Rebase produces a linear, clean history but can duplicate commits (new hashes) and may require resolving conflicts multiple times.

3.5 Cherry‑Pick

Cherry‑pick copies selected commits onto the current branch: git cherry-pick <commit_hash> Useful for applying isolated fixes without merging entire branches.

3.6 Detached HEAD and Rollback

Detach HEAD to point directly at a commit:

git checkout <commit_hash>
git checkout --detach

Relative references simplify this: git checkout <branch>/HEAD^ – one commit before HEAD. git checkout <branch>~N – N commits before HEAD.

After fixing a problem on a detached HEAD, amend the commit instead of creating a new one: git commit --amend Rollback to a previous state with git reset HEAD~N, which moves both the branch pointer and HEAD back.

3.7 Remote Operations

Clone a repository: git clone <repo_url> Fetch updates: git fetch <remote> <branch> Pull (fetch + merge): git pull <remote> <branch> Pull with rebase: git pull --rebase <remote> <branch> Push local commits: git push <remote> <branch> Push may fail if the remote has new commits; a prior git pull (or git pull --rebase) resolves conflicts before pushing.

Conclusion

HEAD and branches are merely references; together with commit nodes they form Git’s distributed model.

Merge preserves explicit chronological history, while rebase yields a linear, cleaner log and is preferred when possible.

Moving HEAD lets you inspect any snapshot in the repository.

Clone and fetch bring the full remote history and references into a local copy.

Pull is essentially fetch followed by merge (or rebase with --rebase).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitmergerebaseVersion Controlcommandsremotebranching
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.