Master Clean Git History with commit --amend and Interactive Rebase
Learn how to keep your Git log tidy by using git commit --amend to modify the latest commit, applying the --no-edit flag for quick fixes, and leveraging interactive rebase (git rebase -i) to squash or fixup commits, ensuring a linear and readable history.
Background
Many developers learn how to write clean code but rarely learn how to submit code in a tidy way. Using Git’s flexible workflows can lead to messy git log histories, which are hard to read and maintain.
Using git commit --amend
The command’s help describes it as “amend previous commit”. It lets you modify the most recent commit’s message, files, and commit ID. --amend amend previous commit If you missed a file in the last commit, you can add it and amend without creating a new commit ID:
echo "feature 1.3 config info" > config.yaml
git add .
git commit --amend --no-editAfter amendment, the old commit ID is replaced by a new one, and the log remains clean.
Using git rebase -i
Before merging a feature branch into main, you can rewrite the last few commits with interactive rebase: git rebase -i HEAD~3 The editor shows a list of commits. Replace pick with fixup (or squash) to combine commits without adding extra log entries.
1 pick 5dd0ad3 feat: [JIRA123] add feature 1
2 fixup 119f86e feat: [JIRA123] add feature 1.1
3 fixup 247572e feat: [JIRA123] add feature 1.2 and 1.3After saving and exiting, the log shows a single clean commit:
* 41cd711 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1
* c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commitUsing Rebase for Feature Integration
When the main branch has new changes, rebase your feature branch onto main instead of merging to avoid extra merge commits: git pull origin main --rebase This keeps the feature’s commits on top of the latest main commits, preserving a linear history.
Summary
By mastering three techniques— git commit --amend, interactive rebase with fixup / squash, and rebasing feature branches onto main —you can maintain a clear, linear git log that makes repository history healthy and easy to understand.
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.
