Operations 9 min read

Master Clean Git History: 3 Powerful Tricks with commit‑amend and rebase

Learn how to keep your Git log tidy by mastering git commit --amend, interactive rebase, and pull with rebase, enabling you to edit commit messages, squash commits, and maintain a linear history without extra merge nodes, illustrated with practical examples and diagrams.

Programmer DD
Programmer DD
Programmer DD
Master Clean Git History: 3 Powerful Tricks with commit‑amend and rebase

Many developers learn to write clean code but rarely learn to keep their Git commits clean. Random commits lead to a tangled git log history that is hard to read.

Use git commit --amend

The command allows you to modify the most recent commit, including its message and files, without creating a new commit ID. --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

Amend the last commit message:

git commit --amend -m "feat: [JIRA123] add feature 1.2 and 1.3"

The new commit replaces the old ID and updates the message without adding a node.

* 5e354d1 (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

If you forget a file after committing, you can add it and 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

Use git rebase -i

Interactive rebase lets you reorder, squash, or drop commits before merging a feature branch. git rebase -i HEAD~3 Typical editor view:

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
 # ... comments ...

Replace pick with fixup (or squash) to merge commits without extra messages:

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

Resulting clean log:

* 41cd711 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1
* c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit

Use git pull --rebase before merging

Before merging a feature branch back to main, rebase it onto the latest main to keep a linear history and avoid extra merge commits. git pull origin main --rebase After rebase, the log remains linear:

* 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

By following these three techniques— git commit --amend, interactive git rebase -i, and git pull --rebase —you can maintain a clear, healthy Git history.

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.

Version Controlinteractive rebasecommit amendgit log
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.