Master Clean Git History with --amend, Interactive Rebase, and Rebase Pull
This guide explains how to keep your Git log tidy by using git commit --amend to modify the latest commit, interactive rebase to squash or fixup multiple commits, and pull with --rebase to maintain a linear history before merging feature branches.
Background
Developers often focus on writing clean code but overlook the importance of clean commit history. Because Git is highly flexible, improper workflow can produce a chaotic git log that is hard to read and maintain.
Tip 1 – Use git commit --amend
The --amend option lets you modify the most recent commit, changing its message, its file contents, and replacing the old commit‑id with a new one. --amend amend previous commit Example log before amendment:
* 98a75af (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1.2
* 119f86e feat: [JIRA123] add feature 1.1
* 5dd0ad3 feat: [JIRA123] add feature 1
* c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commitTo change the last commit message:
git commit --amend -m "feat: [JIRA123] add feature 1.2 and 1.3"The new commit replaces 98a75af with 5e354d1 without adding a new node.
If you forgot to add a file (e.g., config.yaml) after the original commit, you can amend without editing the message:
echo "feature 1.3 config info" > config.yaml
git add .
git commit --amend --no-editResulting repository:
.
├── README.md
├── config.yaml
└── feat1.txt
0 directories, 3 filesResulting log:
* 247572e (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1.2 and 1.3
* 119f86e feat: [JIRA123] add feature 1.1
* 5dd0ad3 feat: [JIRA123] add feature 1
* c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commitAdding --no-edit makes the amendment even smoother.
Tip 2 – Use Interactive Rebase ( git rebase -i )
Before merging a feature branch, you can clean up multiple commits by squashing or fixing them up. git rebase -i HEAD~3 The editor shows a list of recent commits:
1 pick 5dd0ad3 feat: [JIRA123] add feature 1
2 pick 119f86e feat: [JIRA123] add feature 1.1
3 pick 247572e feat: [JIRA123] add feature 1.2 and 1.3Replace pick with fixup (or squash) for the commits you want to merge into the previous one, then save and quit ( :wq).
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 rebasing, the log becomes concise:
* 41cd711 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1
* c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commitTip 3 – Rebase Instead of Merge When Updating Feature Branches
When the main branch has new commits, pull the latest changes into your feature branch using rebase to keep a linear history. git pull origin main --rebase This replaces the default merge (which would add a merge commit) with a rebase, preserving a straight line of commits.
* d40daa6 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1
* 446f463 (origin/main, origin/HEAD) Create main.properties
* c69f53d (origin/feature/JIRA123-amend-test, main) Initial commitAfter the rebase, the feature commits sit on top of the latest main commits, making the history linear and ready for a clean push and pull‑request.
Summary
By mastering git commit --amend, interactive rebase ( git rebase -i), and pulling with --rebase, you can keep your repository’s commit history clear, concise, and easy to navigate, which leads to healthier projects and smoother collaboration.
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.
