Master Git: Visual Guide to the Most Essential Commands
This article provides a comprehensive visual walkthrough of Git's most frequently used commands, explaining how files move between the working directory, index, and repository, and detailing each operation—from basic add/commit/reset to advanced concepts like detached HEAD, rebasing, and cherry‑picking.
Basic Usage
The four core commands move files among the working directory, staging area (index), and repository: git add <files> – stage files in the index. git commit – create a snapshot from the index and record it. git reset -- <files> – undo the last git add; git reset without arguments clears the entire index. git checkout -- <files> – discard local changes by copying files from the index back to the working directory.
Interactive modes are available via git reset -p, git checkout -p, or git add -p. You can also bypass the index entirely by committing directly from the repository.
Conventions
Images use a five‑character green identifier to represent commit IDs, orange branches to indicate branch pointers, and a HEAD label attached to the current branch. The diagram shows the last five commits, with ed489 as the newest; main points to it, while stable points to its grandparent.
Command Details
Diff
Multiple ways exist to view changes between two commits. Example commands are illustrated in the accompanying diagram.
Commit
When committing, Git creates a new commit from the staged files, sets the current node as the parent, and moves the current branch pointer to the new commit. If the branch points to an ancestor, the new commit still updates the branch, 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
git checkoutcopies files from a historical commit (or the index) to the working directory and can also switch branches. Specifying a file name copies that file from the chosen commit to both the index and working directory; omitting a file name leaves the current branch unchanged.
If a branch name is given, HEAD moves to that branch and the working tree is updated to match the commit pointed to by the new HEAD. If no name is supplied, Git creates a detached HEAD, allowing temporary navigation among historical versions.
Detached HEAD Commits
When HEAD is detached, commits are created normally but are not referenced by any named branch; they exist on an anonymous line and may be garbage‑collected if not anchored.
To preserve such a state, create a new branch with git checkout -b <name>.
Reset
git resetmoves the current branch pointer and optionally updates the index and working directory. Without options, only the branch moves. --hard updates both index and working tree; --soft leaves them unchanged.
When a path is supplied, the effect mirrors checkout for that file, but the index is also updated.
Merge
git mergecombines two branches. If the other branch is an ancestor, the command does nothing; 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 rebasereplays commits from one branch onto another, producing a linear history. It is essentially an automated series of cherry‑picks. Options like --onto limit the range, and --interactive enables editing, reordering, or squashing commits.
Technical Notes
File contents are stored as blobs in .git/objects and referenced by SHA‑1 hashes. The index ( .git/index) lists these blobs, while commits store a tree object that mirrors the directory structure. Each commit records the hash of its parent tree.
Commits made on a detached HEAD are recorded in the reflog but may be pruned over time, similar to the effects of git commit --amend or git rebase.
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.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.
