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.
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 commitAmend 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 commitIf 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-editResulting repository:
.
├── README.md
├── config.yaml
└── feat1.txt
0 directories, 3 filesUse 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.3Resulting 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 commitUse 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 commitBy following these three techniques— git commit --amend, interactive git rebase -i, and git pull --rebase —you can maintain a clear, healthy Git history.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
