Fundamentals 9 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Clean Git History with --amend, Interactive Rebase, and Rebase Pull

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 commit

To 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-edit

Resulting repository:

.
├── README.md
├── config.yaml
└── feat1.txt

0 directories, 3 files

Resulting 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 commit

Adding --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.3

Replace 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.3

After 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 commit

Tip 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 commit

After 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

workflowGitrebasecommitlog cleanupinteractive rebaseamend
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.