A clean Git history improves readability, simplifies debugging, and makes collaboration more efficient. This page explains best practices and techniques for keeping your project’s commit history organized.
- Simplifies Debugging: Easier to identify when and where changes were introduced.
- Improves Collaboration: Clear commit messages help teammates understand the purpose of changes.
- Enhances Documentation: A clean history serves as a record of the project’s evolution.
Use descriptive and concise commit messages. Follow the format:
<verb> <description> [optional issue reference]
Example:
Fix null pointer exception in UserService (#123)
- Commit changes related to a single task or issue.
- Avoid combining unrelated changes in the same commit.
- Avoid temporary messages like
WIP
(Work in Progress) orFix stuff
. - Use Git stashing to save incomplete work instead.
- Use
git rebase
for feature branches to create a linear history. - Avoid frequent merge commits unless necessary for collaborative workflows.
Combine multiple commits into one before merging a feature branch to clean up the history:
git rebase -i main
Use git commit --amend
to edit or replace the last commit with a more meaningful one.
Use git rebase -i
to rewrite multiple commits:
git rebase -i HEAD~n
- Mark commits to squash, edit, or reorder.
- Save and close the editor to apply the changes.
Update the last commit message or add files to it:
git commit --amend
To merge a feature branch into main
with a single commit:
git merge --squash feature-branch
git commit -m "Add feature-name"
Rebase a feature branch onto main
to avoid unnecessary merge commits:
git checkout feature-branch
git rebase main
git checkout main
git merge feature-branch
Split a large commit into smaller, logical commits:
- Reset to the previous commit:
git reset HEAD~1
- Stage and commit changes in smaller chunks.
Use interactive rebase to remove unnecessary commits:
git rebase -i HEAD~n
Mark the unwanted commits as drop
.
Rewriting history (e.g., rebasing) is safe for local branches but can disrupt shared branches.
Run tests after rewriting history to ensure the changes didn’t break the codebase.
Notify your team before making significant changes to shared branches.
Command | Description |
---|---|
git rebase -i HEAD~n |
Interactively rebase the last n commits. |
git commit --amend |
Modify the last commit. |
git merge --squash |
Squash commits into a single merge commit. |
git reset HEAD~1 |
Uncommit the last commit while keeping changes. |
git log --oneline |
View a concise commit history. |
git log --oneline
git rebase -i HEAD~3
- Mark commits as
pick
,squash
, oredit
in the editor. - Save and close the editor to apply changes.
Force push the updated branch (only for local or feature branches):
git push --force
- Define Standards: Agree on commit message conventions and rebase/merge policies.
- Use Tools: Employ Git hooks or linters to enforce standards automatically.
- Review History: Regularly review the commit history for consistency.
Maintaining a clean Git history is crucial for efficient collaboration, debugging, and documentation. By following best practices and using Git’s powerful tools, you can ensure a clear and organized project history.