Fundamentals 18 min read

Comprehensive Guide to Git: Concepts, Branches, Commands, and Best Practices

This article provides an in‑depth, step‑by‑step explanation of Git’s core concepts, including its advantages over centralized systems, file states, commits, HEAD, remote repositories, branching strategies, and detailed command usage for committing, branching, merging, rebasing, cherry‑picking, reverting, and interacting with remote servers.

Architect's Guide
Architect's Guide
Architect's Guide
Comprehensive Guide to Git: Concepts, Branches, Commands, and Best Practices

Git is the most powerful code management tool today, yet many developers only use basic operations like clone, commit, pull, and push, avoiding rebase or advanced features. This guide shares a thorough understanding of Git’s fundamentals, helping readers grasp its underlying principles and use its commands confidently.

Table of Contents

1. Basic Concepts 1.1 Advantages of Git 1.2 File Status 1.3 Commit Nodes 1.4 HEAD 1.5 Remote Repository

2. Branches 2.1 What Is a Branch?

3. Command Details 3.1 Commit‑related Commands 3.2 Branch‑related Commands 3.3 Merge‑related Commands 3.4 Revert‑related Commands 3.5 Remote‑related Commands

1.1 Advantages of Git

Git is a distributed version‑control system, unlike centralized systems (e.g., SVN) that require a network for every commit. With Git, commits are stored locally, allowing offline work and fast operations. Its distributed nature makes branching, merging, and history navigation much more efficient.

Centralized: All code lives on a central server; every commit depends on network access and can cause frequent merge conflicts. Distributed: Commits are local, automatically backed up, and each developer can clone the entire history.

Because each commit is cheap and stored locally, rolling back to a previous state is straightforward, giving Git a clear advantage over centralized tools.

1.2 File Status

Git tracks files in three states:

Modified : Files changed in the working directory.

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

Committed : Files permanently recorded in the repository.

1.3 Commit Nodes

Each commit creates a node identified by a SHA‑1 hash. Nodes form a linear chain (ignoring merges). The diagram below shows a simple chain where each node contains the cumulative changes of its predecessors.

The hash displayed above each node is calculated from its contents.

1.4 HEAD

HEAD is a reference that points to the current commit (or branch). It determines the state of the working directory. HEAD can point directly to a commit or indirectly to a branch, which in turn points to a commit.

1.5 Remote Repository

Although Git stores everything locally, collaboration requires a remote repository. git clone copies the code, history, branches, and HEAD references to your machine. Remote references are not updated automatically; you must fetch or pull to synchronize.

Always ensure the remote repository contains tested, stable 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, each representing an independent line of development. Creating a branch does not duplicate code; it merely creates another reference.

Use cases include fixing bugs on an older release while continuing development on the next version, or experimenting with a feature without affecting the main line.

Creating a branch does not copy files; it only adds a new pointer, keeping storage overhead minimal.

3. Command Details

3.1 Commit‑related Commands

Add a specific file to the staging area:

git add 文件路径

Add all changes:

git add .

Discard changes in the working directory:

git checkout -- 文件名

Clear the staging area for a file:

git reset HEAD 文件名

Commit staged changes with a message:

git commit -m "commit description"

3.2 Branch‑related Commands

Create a new branch:

git branch 分支名

Switch to an existing branch:

git checkout 分支名

Create and switch in one step:

git checkout -b 分支名

Delete a branch after it has been merged:

git branch -d 分支名

3.3 Merge‑related Commands

Merge another branch or commit into the current branch:

git merge 分支名/节点哈希值

Fast‑forward merges occur when the target branch is strictly ahead of the current branch.

If both branches have diverged, Git creates a new merge commit (e.g., C5) to combine histories.

If the same line of a file was modified in both branches, a merge conflict occurs and must be resolved manually.

Rebase

Rebase rewrites history by moving a series of commits onto a new base:

git rebase 分支名/节点哈希值

Unlike merge, rebase does not create an explicit merge commit, resulting in a linear history.

Rebase is useful for keeping a clean history but may require resolving the same conflicts multiple times.

Cherry‑pick

Select specific commits and apply them onto the current branch:

git cherry-pick 节点哈希值

3.4 Revert‑related Commands

Detach HEAD to point directly at a specific commit:

git checkout 节点哈希值
# or
git checkout --detach

Use relative references to move HEAD back N commits:

# HEAD to previous commit
git checkout branch/HEAD^
# HEAD back N commits
git checkout branch~N

Amend the most recent commit after fixing a mistake:

git commit --amend

Reset the current branch and HEAD to an earlier state:

# Roll back N commits
git reset HEAD~N

3.5 Remote‑related Commands

Clone a remote repository:

git clone 仓库地址

Fetch new objects and references without merging:

git fetch 远程仓库地址/分支名

Pull (fetch + merge) updates the current branch:

git pull 远程分支名

Pull with rebase integrates changes without a merge commit:

git pull --rebase 远程分支名

Push local commits to the remote repository:

git push 远程分支名

Push may fail if the remote has newer commits; resolve conflicts locally (often by pulling) before pushing again.

Conclusion

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

Merge preserves chronological history, while rebase creates a linear, cleaner log.

Detaching HEAD allows inspection or amendment of past commits.

Cloning or fetching stores the entire remote history locally.

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

gitmergerebaseversion controlCommandsBranching
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

login 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.