Fundamentals 11 min read

Master Git: Visual Guide to the Most Essential Commands

This article provides a comprehensive visual tutorial on Git’s core commands—including add, commit, reset, checkout, diff, merge, rebase, and cherry‑pick—explaining their effects on the working directory, staging area, and repository while illustrating concepts such as HEAD, branches, and detached states.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Git: Visual Guide to the Most Essential Commands

Basic Usage

Git add files puts current files into the staging area.

git add files – add files to staging.

git commit – create a snapshot from staging and commit.

git reset – files – undo the last git add; git reset without arguments clears the staging area.

git checkout – files – copy files from staging to the working directory, discarding local changes.

You can enter interactive mode with git reset -p, git checkout -p, or git add -p. You can also skip the staging area and directly checkout from the repository or commit directly. git commit -a is equivalent to running git add for all files then committing. git commit files creates a commit that includes a snapshot of the working directory and adds the files to the staging area. git checkout HEAD – files rolls back to the last commit.

Conventions

In the diagrams, five‑character green strings represent commit IDs, orange branches point to specific commits, and the current branch is marked by HEAD. The picture shows the last five commits, with master pointing to the latest one.

Command Details

Diff

Various ways exist to view changes between two commits; examples are shown.

Commit

When committing, Git creates a new commit from the staged files, sets the current node as its parent, and moves the current branch to the new node.

Even if the current branch is an ancestor of the commit, Git still creates a new commit; merging or rebasing may be required.

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

Checkout

Checkout copies files from a historical commit (or the staging area) to the working directory and can also switch branches.

When a filename (or -p) is given, Git copies the file from the specified commit to both the staging area and the working directory without changing the current branch.

If a branch name is given, HEAD moves to that branch and the working tree is updated to match the commit it points to.

When no name is given, Git creates a detached HEAD, which can be used to explore history without affecting any branch.

Reset

Reset moves the current branch to another commit and optionally updates the working directory and index. Without options, it moves the branch; with --hard it also updates the working tree; with --soft it leaves both unchanged.

When a filename or -p is provided, the index is updated similarly to checkout.

Merge

Merge combines different branches. The index must match the current commit before merging. Fast‑forward occurs when the current commit is an ancestor of the other branch; otherwise a three‑way merge is performed.

Cherry‑Pick

Cherry‑pick copies a specific commit onto the current branch as a new commit.

Rebase

Rebase is an alternative to merge that replays commits from one branch onto another, producing a linear history; it is essentially an automated series of cherry‑picks.

Interactive rebase ( --interactive) allows reordering, editing, dropping, or squashing commits.

Technical Explanation

File contents are stored as blobs in .git/objects and identified by SHA‑1 hashes. The index ( .git/index) lists these blobs. Commits store a tree object that represents the directory structure, each identified by its own hash.

Detached HEAD commits are referenced by the reflog and may be garbage‑collected if not referenced later, similar to git commit --amend or git rebase.

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.

Backendsoftware developmentGitTutorialVersion Control
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.