Fundamentals 11 min read

Master Git: A Visual Guide to the Most Common Commands

This article provides a comprehensive, illustrated walkthrough of Git’s core commands—add, commit, reset, checkout, merge, rebase, and more—explaining how they move files between the working directory, index, and repository, and clarifying concepts such as HEAD, branches, and detached HEAD states.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Git: A Visual Guide to the Most Common Commands

Overview

The guide visualizes the most frequently used Git commands and explains their effects on the working directory, index (staging area), and repository. Understanding these basics helps users grasp Git’s internal workflow more deeply.

Basic Usage

Four fundamental commands move files among the three areas: git add <files> – stages the specified files in the index. git commit – creates a snapshot from the index and records it as a new commit. git reset – <files> – unstages the last git add for the given files; git reset without arguments unstages all. git checkout – <files> – restores files from the index to the working directory, discarding local changes.

Interactive modes are available via git reset -p, git checkout -p, or git add -p.

Conventions

Diagrams use five‑character green IDs to represent commit hashes, orange arrows for branches, and a HEAD label to indicate the current branch. The example shows the latest five commits, with master pointing to the newest one.

Command Details

Diff

Various commands can display changes between two commits; examples are illustrated in the accompanying images.

Commit

When committing, Git creates a new commit object from the staged files, sets the current node as its parent, and moves the current branch pointer to this new node. Even if the current branch is an ancestor of another branch, Git still creates a new commit, requiring a merge or rebase later.

To amend the most recent commit, use git commit --amend, which creates a new commit with the same parent and discards the old one.

Checkout

The git checkout command copies files from a specified commit (or the index) to the working directory and optionally stages them. When a branch name is given, HEAD moves to that branch, and the working tree is updated to match the branch’s tip.

A detached HEAD occurs when checking out a specific commit, tag, or SHA‑1 without a branch name; this creates an anonymous reference that can be used to explore history without moving any branch pointer.

Reset

git reset

moves the current branch pointer to another commit. With --hard, both the index and working directory are updated; with --soft, only the branch pointer changes. Without options, the index is reset to the specified commit, leaving the working tree untouched unless --hard is used.

Merge

git merge

combines two branches. If the other branch is an ancestor of the current one, the merge is a fast‑forward. Otherwise, a three‑way merge creates a new commit that records both parents.

Cherry‑Pick

git cherry-pick <commit>

copies a single commit from another branch onto the current branch, creating a new commit with the same changes.

Rebase

git rebase

rewrites history by replaying commits from one branch onto another, producing a linear history. The --onto option limits the range of commits to be rebased. Interactive rebase ( git rebase -i) allows dropping, reordering, editing, or squashing commits.

Technical Notes

Git stores file contents as blobs in .git/objects, identified by SHA‑1 hashes. The index ( .git/index) lists these blobs, and commits store a tree object that represents the directory hierarchy. Detached‑HEAD commits are referenced only by the reflog and may be garbage‑collected if not anchored.

Related Links

http://marklodato.github.io/visual-git-guide/index-zh-cn.html#merge

http://marklodato.github.io/visual-git-guide/index-zh-cn.html#rebase

http://marklodato.github.io/visual-git-guide/index-zh-cn.html#detached

http://en.wikipedia.org/wiki/Three-way_merge

http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html#_interactive_mode

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.

Gitmergerebasecommand-lineVersion Controlbranchingcommit
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.