DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Understanding Git
  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer
  • Top 35 Git Commands With Examples
  • Git Reset HEAD

Trending

  • Building Enterprise-Ready Landing Zones: Beyond the Initial Setup
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • Why Database Migrations Take Months and How to Speed Them Up
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Developer Git Commit Hygiene

Developer Git Commit Hygiene

This article will demonstrate good developer Git commit hygiene as well as educate the developer audience to use short and frequent commits.

By 
Gaurav Shekhar user avatar
Gaurav Shekhar
·
Updated by 
Anil Kumar Moka user avatar
Anil Kumar Moka
·
Jun. 20, 24 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
5.5K Views

Join the DZone community and get the full member experience.

Join For Free

Maintaining good commit hygiene is crucial for keeping your Git repository clean, manageable, and understandable for everyone involved in the project. 

Here are some best practices for ensuring good commit hygiene as a Java developer.

Git Hygiene Best Practices

1. Meaningful Commit Messages

Write Clear and Descriptive Messages

Each commit message should clearly describe what the commit does: 

Example:
Add user authentication module - Implement login and logout functionality - Add JWT-based authentication - Update user model to include hashed passwords - Add unit tests for authentication logic


Use the Imperative Mood

Start commit messages with a verb in the imperative mood (e.g., "ADD", "FIX", "Update").

  • Example: "Fix bug in user authentication" instead of "Fixed bug in user authentication".

2. Small, Atomic Commits

Commit Small Changes

Make each commit a small, self-contained change that addresses a single issue or feature. This makes it easier to review and debug.

Avoid Large Commits

Large commits are harder to review and understand. Break down large changes into smaller, logical commits.

3. Commit Often, but Not Too Often

Frequent Commits

Commit frequently to save your progress and reduce the risk of losing work.

Avoid Noise

Don't commit excessively small changes that don't add meaningful value.

4. Separate Concerns

One Purpose Per Commit

Each commit should have a single purpose. Avoid mixing unrelated changes in a single commit.

  • Example: If you're fixing a bug and refactoring code, do them in separate commits.

5. Test Before Committing

Run Tests

Ensure all tests pass before committing. This prevents broken commits from entering the codebase.

Code Quality Checks

Use static analysis tools (e.g., Checkstyle, PMD) to ensure code quality before committing.

6. Use Branches Effectively

Feature Branches

Develop new features and bug fixes in separate branches rather than directly on the main branch.

Branch Naming

Use descriptive branch names (e.g., feature/add-authentication).

7. Rebase and Squash Commits

Rebase Instead of Merge

Use git rebase to keep a linear history and avoid unnecessary merge commits.

Squash Commits

Combine multiple small commits into one meaningful commit before merging to the main branch.

  • Example: Use git rebase -i HEAD~N to interactively rebase the last N commits and squash them.

8. Avoid Committing Generated Files

Git Ignore

Use a .git ignore file to prevent committing generated files, build artifacts, and other unnecessary files.

Example:

# Compiled class files *.class # Log files *.log # Build directories /target/


9. Document Important Changes

Commit Message Body

Provide additional context in the commit message body if the change is complex or requires explanation.

Example:
Refactor authentication module - Simplify token validation logic - Improve error handling in login process - Update documentation for the new authentication flow


10. Review Commits Before Pushing

Interactive Rebase

Use interactive rebase (git rebase -i) to review and clean up your commits before pushing.

Amend Last Commit

If you need to make small changes to the last commit, use git commit --amend.

Example of a Good Commit Workflow

1. Stage Changes

git add .


2. Commit With a Descriptive Message

git commit -m "Add user authentication module"


3. Review Commits

git log


4. Rebase and Squash Commits if Necessary

git rebase -i HEAD~N


5. Push To Remote Repository

git push origin feature/add-authentication


Example of a Good Commit Workflow

Git Commit Cheat Sheet

Here is a cheat sheet for quick reference to Git commands (source: GitLab).

Git Configuration

git config --global user.name “Your Name”
Set the name that will be attached to your commits and tags.
git config --global user.email “you@example. com”
Set the e-mail address that will be attached to your commits and tags.
git config --global color.ui auto
Enable some colorization of Git output.

Starting a Project

git init [project name]
Create a new local repository in the current directory. If [project name] is provided, Git will create a new directory named [project name] and will initialize a repository inside it.
git clone <Project URL>
Downloads a project with the entire history from the remote repository

Day-To-Day Work

git status
Displays the status of your working directory; Options include new, staged, and modified files. It will retrieve branch name, current commit identifier, and changes pending commit.
git add [file]
Add a file to the staging area. Use in place of the full file path to add all changed files from the current directory down into the directory tree.
git diff [file]
Show changes between working directory and staging area
git diff --staged [file]
Shows any changes between the staging area and the repository
git checkout -- [file]
Discard changes in working directory; This operation is unrecoverable.
git reset <path>...]
Revert some paths in the index (or the whole index) to their state in HEAD.
git commit
Create a new commit from changes added to the staging area. The commit must have a message.
git rm [file]
Remove file from working directory and staging area.

Storing Your Work

git stash
Put current changes in your working directory into stash for later use.
git stash pop
Apply stored stash content into working directory, and clear stash.
git stash drop
Delete a specific stash from all your previous stashes.

Git Branching Model

git branch [-a]
List all local branches in repository. With -a: show all branches (with remote)
git branch [branch_name]
Create new branch, referencing the current HEAD.
git rebase [branch_name]
Apply commits of the current working branch and apply them to the HEAD of [branch] to make the history of your branch more linear.
git checkout [-b] [branch_name]
Switch working directory to the specified branch. With -b: Git will create the specified branch if it does not exist.
git merge [branch_name]
Join specified [branch_name] branch into your current branch (the one you are on currently)
git branch -d [branch_ name]
Remove selected branch, if it is already merged into any other. -D instead of -d forces deletion.

Tagging Commits

git tag
List all tags
git tag [name] [commit sha]
Create a tag reference named name for current commit. Add commit sha to tag a specific commit instead of current one.
git tag -a [name] [commit sha]
Create a tag object named name for current commit
git tag -d [name]
Remove a tag from local repository.

Synchronizing Repositories

git fetch [remote]
Fetch changes from the remote, but not update tracking branches.
git fetch --prune [remote]
Delete remote Refs that were removed from the remote repository.
git pull [remote]
Fetch changes from the remote and merge current branch with its upstream.
git push [--tags] [remote]
Push local changes to the remote. Use --tags to push tags.
git push -u [remote] [branch]
Push local branch to remote repository. Set its copy as an upstream.

Inspect History

git log [-n count]
List commit history of current branch. -n count limits list to last n commits
git log --oneline --graph --decorate
An overview with reference labels and history graph; One commit per line
git log ref .
List commits that are present on the current branch and not merged into ref; A ref can be a branch name or a tag name.

Reverting Changes

git reset [--hard] [target reference]
Switches the current branch to the target reference, leaving a difference as an uncommitted change. When --hard is used, all changes are discarded. It's easy to lose uncommitted changes with --hard.
git revert [commit sha]
Create a new commit, reverting changes from the specified commit. It generates an inversion of changes.

Final Thoughts

By following these practices, you can ensure good commit hygiene, making your Git history more readable and maintainable for you and your team.

Git Branch (computer science) Commit (data management) Repository (version control)

Opinions expressed by DZone contributors are their own.

Related

  • Understanding Git
  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer
  • Top 35 Git Commands With Examples
  • Git Reset HEAD

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!