Master Git: Visualize Merge, Rebase, Reset, Revert, and More with GIFs
This article provides a visual, step‑by‑step guide to essential Git commands—including merge (fast‑forward and no‑ff), rebase (including interactive mode), reset (soft and hard), revert, cherry‑pick, fetch, pull, and reflog—explaining their behavior, use cases, and common pitfalls with animated illustrations.
Why visualizing Git helps
Understanding how each Git command manipulates branches and history reduces confusion and prevents mistakes. Visual diagrams illustrate the state changes caused by git merge, git rebase, git reset, git revert, git cherry-pick, git fetch, git pull, and git reflog.
Merge
Fast‑forward (--ff) : If the current branch has no divergent commits, the branch pointer is simply moved forward to the target commit. No new merge commit is created.
No‑fast‑forward (--no-ff) : When the current branch contains its own commits, Git creates a new merge commit with two parents, preserving both histories.
Merge conflicts arise when the same line in a file is edited differently on the two branches or when one branch deletes a file that the other modifies. Git stops the merge and asks you to resolve the conflict manually before committing.
Rebase
Rebasing copies the commits of the current branch onto another base branch, producing a linear history. The original commits are re‑created with new hashes, so the history is rewritten.
Typical usage:
git checkout feature
git rebase mainAfter a successful rebase, the feature branch appears as if it were developed directly on top of main, eliminating merge commits.
Interactive rebase
Run git rebase -i <upstream> to edit, reorder, squash, fixup, execute commands, or drop commits. The six actions are:
reword – change the commit message
edit – modify the commit’s content
squash – combine the commit with the previous one (keeps both messages)
fixup – combine the commit with the previous one and discard its message
exec – run an arbitrary shell command at that point in the rebase
drop – remove the commit entirely
Reset
Soft reset moves HEAD to a specified commit but leaves the index and working tree untouched. Changes remain staged, allowing you to amend or recommit them. git reset --soft HEAD~2 Hard reset moves HEAD and updates both the index and working directory to match the target commit, discarding all uncommitted changes.
git reset --hard abc123Revert
git revert <commit>creates a new commit that undoes the changes introduced by the specified commit, without rewriting history. This is safe for public branches.
git revert abcdefCherry‑pick
git cherry-pick <commit>applies a single commit from another branch onto the current branch, creating a new commit with the same changes.
git checkout main
git cherry-pick 76d12Fetch and Pull
git fetchdownloads new objects and refs from a remote repository without altering local branches. It updates remote‑tracking branches (e.g., origin/main).
git fetch origin git pullis a shortcut for git fetch followed by git merge (or git rebase when configured). It automatically merges the fetched changes into the current branch.
git pull --rebaseReflog
git reflogrecords every movement of HEAD, including commits, resets, merges, and reverts. It enables recovery from accidental changes by allowing you to reset to a previous reflog entry.
# Show recent HEAD movements
git reflog
# Reset to the state before the last merge
git reset --hard HEAD@{1}Summary
Visualizing the effect of each command clarifies how branches evolve, helps resolve conflicts, and supports a clean, maintainable history.
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.
