Fundamentals 18 min read

Master Git: From Basics to Advanced Branching and Merging Techniques

This comprehensive guide explains Git’s core concepts—including its distributed advantages, file states, commit nodes, HEAD, and remote repositories—while detailing practical commands for committing, branching, merging, rebasing, cherry‑picking, reverting, and synchronizing with remote servers, all illustrated with clear diagrams and examples.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Master Git: From Basics to Advanced Branching and Merging Techniques

Preface

Git is the most powerful code management tool today, yet many users only know clone, commit, pull, and push, avoiding rebase and relying on merge. This article shares a deep understanding of Git’s fundamentals, aiming to help readers use Git commands confidently and efficiently.

1. Basic Concepts

1.1 Advantages of Git

Git is a distributed version control system, unlike centralized systems that store all code on a single server and require network access for every commit.

Centralized: All code resides on a central server; commits depend on network connectivity and often cause frequent merges (e.g., SVN). Distributed: Commits are local, do not need network, and each developer clones the entire repository, preserving full history (e.g., Git).

Because commits are cheap and stored locally, rolling back to a previous state is straightforward, making Git more flexible than centralized tools.

1.2 File States

Git tracks files in three states:

Modified: Files changed in the working directory.

Staged: Files added to the index with git add, awaiting commit.

Committed: Files permanently recorded in the repository.

1.3 Commit Nodes

Each commit creates a node identified by a SHA‑1 hash. Linear commits form a chain of nodes (ignoring merges).

The hash appears above each node; later nodes contain the changes of earlier nodes.

1.4 HEAD

HEAD is a pointer that references a commit. The working directory reflects the commit HEAD points to. HEAD can also point to a branch, indirectly referencing the branch’s latest commit.

1.5 Remote Repository

Although Git stores history locally, developers push changes to a remote repository. git clone copies the remote code, history, branches, and HEAD to the local machine, but remote references are not updated automatically; they must be fetched manually.

2. Branches

2.1 What Is a Branch?

A branch is a lightweight pointer to a commit, similar to HEAD but multiple branches can exist simultaneously. Branches enable parallel development for features or versions.

Example: After releasing v1.0, development of v1.1 starts, but a critical bug in v1.0 must be fixed. By creating a branch from the v1.0 commit, the bug can be fixed without interfering with v1.1 work.
Left diagram: v1.0 (C2) is the base; a new branch ft-1.0 is created from C2. Right diagram: After v1.1 development (C3) and bug‑fix commits (C4), the bug‑fix branch is merged back, preserving both histories.

Branches also allow isolated development of uncertain features, which can be merged later when needed.

3. Command Details

3.1 Commit‑Related Commands

Add a specific file to the staging area: git add file_path Add all files: git add . Discard changes in the working directory: git checkout -- file_name Unstage a file: git reset HEAD file_name Commit staged changes with a message:

git commit -m "commit description"

3.2 Branch‑Related Commands

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 Merge‑Related Commands

merge

Merge another branch or commit into the current branch: git merge branch_name_or_commit_hash If the target branch is ahead, Git performs a fast‑forward merge, moving the pointer without creating a new commit.

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

rebase

Rebase replays commits onto another base, producing a linear history: git rebase branch_name_or_commit_hash Rebase copies commits, so the resulting hashes differ from the originals, but the history appears cleaner.

cherry‑pick

Select specific commits to apply onto the current branch:

git cherry-pick commit_hash

The selected commits are copied (e.g., C3' and C4') and placed after the current HEAD.

3.4 Revert‑Related Commands

Detach HEAD

Move HEAD to a specific commit without a branch: git checkout commit_hash Or detach directly: git checkout --detach Relative references simplify this:

git checkout branch_name/HEAD^   # previous commit
git checkout branch_name~N    # N commits before

After fixing a problem, amend the commit without creating a new node:

git commit --amend

Reset (rollback)

Rollback N commits: git reset HEAD~N Reset moves both the branch pointer and HEAD back to the specified commit.

3.5 Remote‑Related Commands

Clone a repository: git clone repository_url Fetch updates from the remote without merging: git fetch remote_name/branch_name Pull (fetch + merge) from a remote branch: git pull remote_branch_name Pull with rebase: git pull --rebase remote_branch_name Push local commits to a remote branch:

git push remote_branch_name

Conclusion

Both HEAD and branches are lightweight references; together with commit nodes they form Git’s distributed core.

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

Moving HEAD lets you inspect any commit’s code.

Clone and fetch store all remote commits and references locally.

Pull is essentially fetch + merge, optionally using rebase for a linear history.

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.

GitrebaseVersion ControlbranchingMergingcommitremote repository
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.