A Visual Guide to Core Git Commands and Their Usage
This article provides a visual, step‑by‑step guide to the most frequently used Git commands—add, commit, reset, checkout, diff, merge, rebase, cherry‑pick, and more—explaining their purpose, typical usage patterns, and underlying concepts such as the index, objects, and detached HEAD state.
Basic Usage
The article starts with a brief introduction from a senior architect and lists the fundamental Git operations that move files between the working directory, the staging area (index), and the repository. git add *files* – adds files to the staging area. git commit – creates a snapshot from the staged files and records a new commit. git reset -- *files* – undoes the last git add *files*; git reset without arguments discards all staged changes. git checkout -- *files* – copies files from the staging area back to the working directory, discarding local modifications.
Interactive modes are available via git reset -p, git checkout -p, or git add -p. The article also mentions that you can skip the staging area entirely by checking out directly from the repository.
Conventions
Images in the following sections use a consistent visual style: green five‑character strings represent commit IDs, orange branches point to specific commits, and the HEAD label indicates the current checkout.
Command Details
Diff
Various ways to view changes between two commits are illustrated.
Commit
When committing, Git creates a new commit object from the staged files, sets the current branch to point to this new node, and records the previous commit as its parent.
If the current branch points to an ancestor of the commit being made, Git still creates the new commit and the branch moves forward, potentially requiring a merge 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 can also switch branches.
When a file name is given, e.g., git checkout HEAD~ foo.c, the file is restored from the parent commit and staged. When a branch name is given, HEAD moves to that branch and the working tree is updated to match.
If neither a file nor a branch is specified, Git checks out a tag, remote branch, SHA‑1, or a relative reference like main~3, resulting in a detached HEAD state.
Detached HEAD Commits
When HEAD is detached, commits are created normally but no named branch is updated; the new commit is reachable only via the reflog and may be garbage‑collected if not referenced.
To preserve such a commit, create a new branch with git checkout -b *name*.
Reset
git resetmoves the current branch pointer and optionally updates the index and working directory. --hard updates both, while --soft leaves them unchanged.
If a path is given, reset behaves similarly to checkout for that file, updating the index.
Merge
git mergecombines two branches. If the current commit is an ancestor of the other, a fast‑forward occurs; otherwise a three‑way merge creates a new commit.
Cherry‑Pick
git cherry-pickcopies a specific commit onto the current branch, creating a new commit with identical changes.
Rebase
git rebasereapplies commits from one branch onto another, producing a linear history; it can be thought of as an automated series of cherry‑picks.
Options such as --onto allow fine‑grained control over the range of commits to reapply.
Technical Explanation
Git stores file contents as blobs in .git/objects, identified by SHA‑1 hashes. The index ( .git/index) records which blobs belong to the next commit. Commits are stored as tree objects that represent directory structures; each tree references sub‑trees or blobs, and each commit records the hash of its top‑level tree and its parent(s).
When committing from a detached HEAD , the new commit is reachable only via the reflog and may be garbage‑collected if not referenced by a branch.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
