Master Git: Visual Guide to Core Commands and Workflows
This article provides a comprehensive, illustrated walkthrough of Git’s most essential commands—add, commit, reset, checkout, merge, cherry‑pick, and rebase—explaining how they move files among the working tree, index, and repository, handling branches, HEAD states, and the underlying object model.
Introduction
Git is a ubiquitous version‑control system, but many developers only use it without understanding its internal mechanics. This guide visualizes the most common Git commands and explains the underlying concepts such as the working directory, index (staging area), repository, branches, and HEAD.
Basic Commands
git add <files>– places the specified files into the staging area. git commit – creates a snapshot from the staged files and records it as a new commit. git reset – <files> – undoes the last git add; git reset without arguments clears the entire staging area. git checkout – <files> – copies files from the staging area back to the working directory, discarding local changes.
These four commands move files between the working directory, index, and repository.
You can enter interactive mode with git reset -p, git checkout -p, or git add -p to select hunks manually.
Advanced Commit Variants
git commit -a– stages all modified files automatically before committing. git commit <files> – commits the specified files and adds them to the index. git checkout HEAD – <files> – restores files to their state in the latest commit.
HEAD and Detached HEAD
When HEAD points to a branch, it moves with each new commit. If you check out a specific commit, tag, or SHA‑1 without a branch name, HEAD becomes detached. In this state you can still commit, but the new commit is not referenced by any named branch and may be garbage‑collected unless you create a new branch with git checkout -b <name>.
Checkout Command
git checkoutcopies files from a given commit (or the index) to the working directory and optionally updates the index. When a branch name is supplied, HEAD switches to that branch and the working tree is updated to match the branch’s tip.
Reset Command
git resetmoves the current branch pointer to another commit. With --hard it also updates the working directory; with --soft it leaves both the index and working tree untouched. Without options, only the branch pointer moves.
Merge Command
git mergecombines two branches. If the target branch is an ancestor of the current branch, Git performs a fast‑forward merge; otherwise it creates a new merge commit after a three‑way merge of the two branch tips and their common ancestor.
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 rebaserewrites history by replaying commits from one branch onto another, producing a linear history. The interactive mode ( git rebase -i) lets you reorder, edit, squash, or drop commits.
Technical Details
Git stores file contents as blobs in .git/objects, identified by SHA‑1 hashes. The index ( .git/index) lists the blobs that belong to the next commit. Commits are stored as tree objects that represent directory structures; each tree points to blobs (files) or other trees (sub‑directories). When a detached‑HEAD commit is made, it is referenced only by the reflog and will eventually be pruned if not attached to a branch.
Illustrations
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.
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.)
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.
