Master Git Rebase: Clean Commit History and Interactive Techniques
This guide explains how to use git rebase to produce a clear linear commit history, compares rebase with git merge, and demonstrates the interactive rebase mode for squashing and editing commits, including conflict resolution commands and best‑practice warnings.
1) Using git rebase for clearer commit history
The git rebase command rewrites a branch’s history by moving its commits onto another base, similar to git merge but without creating a merge commit. After rebase, the commit graph becomes linear, making the history easier to read.
Example scenario: a master branch and a feature/1 branch both start from an initial add readme commit. master adds 3.js and 4.js in two commits, while feature/1 adds 1.js and 2.js in two separate commits.
Switch to feature/1 and run git rebase master. Git replays the master changes first, then applies the feature/1 commits on top of the new base. The resulting log shows a straight line of commits without forks.
If conflicts arise during rebase, resolve them manually, then continue with git add and git rebase --continue. To skip a problematic commit, use git rebase --skip.
2) Differences between git merge and git rebase
When git merge cannot fast‑forward, it creates an extra merge commit (e.g., Merge branch 'xxx' into 'xxx'), preserving the parallel history. In contrast, git rebase rewrites history to a linear form.
During conflict resolution, a merge requires fixing the conflict once, while a rebase may require repeated conflict resolution for each rebased commit.
3) Interactive rebase mode
When a branch accumulates many small or unnecessary commits, interactive rebase can squash them into a single, clean commit. The command syntax is: git rebase -i <base‑commit> For example, to squash all commits up to ac18084 into one, run: git rebase -i ac18084 The editor opens a list of commits. Change the action from pick to squash (or s) for the commits you want to combine, then save and exit (e.g., :wq in Vim). After editing the combined commit message, the history is rewritten.
pick ... ...
s ... ...
s ... ...
s ... ...After the rebase, verify the new history with git log or git branch. Note: interactive rebase should only be performed on private feature branches, never on shared integration branches, to avoid rewriting shared 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.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
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.
