10 Essential Git Commands Visualized: Master Git Quickly
This article walks through ten core Git commands—including merge, rebase, reset, revert, cherry‑pick, fetch, pull, and reflog—using animated GIFs and concrete examples to illustrate how each command manipulates branches, history, and repository state, helping readers visualize and retain the concepts.
Merge
Git can combine changes from one branch into another using git merge. Two merge types exist: fast‑forward (‑ff) merges when the current branch has no extra commits, which simply moves the branch pointer, and no‑fast‑forward (‑no‑ff) merges that create a new merging commit when the current branch has diverging work.
When a conflict occurs—e.g., both branches edit the same line in README.md —Git prompts the user to choose which version to keep, after which the conflict must be resolved manually before committing.
Rebase
git rebasecopies the current branch’s commits onto another branch, producing new commit hashes. This creates a linear history without merge commits, avoiding most merge conflicts. However, it rewrites history, which can be problematic for shared branches.
Interactive Rebase
Before rebasing, git rebase -i lets you edit, reorder, squash, fixup, or drop commits. The six possible actions are reword, edit, squash, fixup, exec, and drop, giving fine‑grained control over the resulting history.
reword – change commit message
edit – modify the commit
squash – combine with previous commit
fixup – combine without keeping message
exec – run a command for each commit
drop – remove the commit
Reset
git resetmoves HEAD to a specified commit. A soft reset ( --soft) moves the pointer but leaves index and working‑tree changes intact, useful for un‑committing while keeping modifications. A hard reset ( --hard) discards all changes, resetting the working tree and index to the target commit.
Example: resetting from commits 9e78i and 035cc while preserving newly added files style.css and index.js demonstrates a soft reset.
Revert
git revertcreates a new commit that undoes the changes introduced by a specified earlier commit, preserving history. For instance, reverting commit ec5be removes the added index.js file without altering the commit graph.
Cherry‑pick
git cherry-pickcopies a single commit from another branch onto the current branch, creating a new commit with the same changes. This is useful when only a specific feature or fix is needed without merging the whole branch.
Fetch and Pull
git fetchdownloads objects and refs from a remote repository without altering local branches. git pull combines fetch and merge, automatically merging the fetched changes into the current branch.
Reflog
git reflogrecords every movement of HEAD, including merges, resets, and reverts, providing a safety net for recovering lost work. By inspecting the reflog, one can reset HEAD to a previous state (e.g., HEAD@{1}) to undo mistakes.
Linux Tech Enthusiast
Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical knowledge.
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.
