Skip to content

Git Commit Best Practices for PHP & Laravel Developers

43 people viewed this

Git Commit Best Practices for PHP & Laravel Developers

Git Commit Best Practices — Write Clean and Meaningful Commit Messages

When working on a PHP or Laravel project, writing code is only one part of development. Another important part is maintaining a clean and understandable Git history.

A Git commit should tell other developers what changed and why it changed. Good commits make code reviews easier, debugging faster, and project history much easier to understand.

In this article, we will learn practical Git commit best practices with PHP and Laravel examples.


1. What Is a Git Commit?

A Git commit is a snapshot of changes in your project at a particular point in time.

For example:

git add .
git commit -m "Add email verification to user registration"

This commit records the changes and gives them a meaningful description.

Later, when someone runs:

git log --oneline

they can quickly understand what happened in the project.


2. Why Are Good Commit Messages Important?

A good commit message helps with:

  • Code review
  • Debugging
  • Finding old changes
  • Understanding project history
  • Reverting specific changes
  • Team collaboration
  • Release management
  • Working with Git and Bitbucket

Compare these two commits:

Bad

git commit -m "fix"

Better

git commit -m "Fix incorrect payment amount calculation"

The second message immediately tells us what was fixed.


3. Best Practice #1 — Write Meaningful Commit Messages

A commit message should explain the actual change.

Instead of:

git commit -m "changes"
git commit -m "update"
git commit -m "done"
git commit -m "fix"

Use:

git commit -m "Add pharmacy invoice PDF generation"

git commit -m "Fix stock calculation for expired batches"

git commit -m "Update user authentication middleware"

A developer looking at the Git history should understand the purpose of the commit without opening every changed file.


4. Best Practice #2 — Keep Commits Focused

A commit should ideally represent one logical change.

For example, suppose you are working on a Laravel application and make these changes:

  • Add a payment API
  • Fix a login bug
  • Update README
  • Change button styling

Putting everything into one commit is not a good practice.

Bad

git commit -m "Add payment, fix login, update README and UI"

Better

git commit -m "Add Razorpay payment integration"

git commit -m "Fix login validation error"

git commit -m "Update project installation guide"

git commit -m "Improve invoice button styling"

Now each commit represents a specific logical change.


5. Best Practice #3 — Use Present Tense

A common Git convention is to write commit messages in the present/imperative style.

Prefer

Add user authentication

Fix payment calculation

Update Laravel dependencies

Remove unused controller

Improve database indexing

Avoid

Added user authentication

Fixed payment calculation

Updated Laravel dependencies

Removed unused controller

The important point is consistency. Your team should agree on one style and use it throughout the project.


6. Best Practice #4 — Make the First Line Short

The first line of a commit message should provide a concise summary.

For example:

git commit -m "Fix invoice total calculation"

If more explanation is necessary, Git also supports a longer commit message.

git commit

This opens your configured editor where you can write a detailed message.

A longer commit can look like:

Fix invoice total calculation

The invoice total was calculated before applying
the product-level discount.

Move discount calculation before the final total
calculation to ensure the correct invoice amount.

7. Best Practice #5 — Explain Why, Not Just What

Sometimes the code itself tells us what changed, but not why it changed.

For example:

git commit -m "Change order query"

This does not tell us much.

A better message is:

git commit -m "Optimize order query for admin dashboard"

For complex changes, the commit body can explain the reason.

Optimize order query for admin dashboard

The dashboard was loading unnecessary relationships
for every order.

Load only the required customer and payment fields
to reduce database queries.

8. Best Practice #6 — Avoid Huge Commits

A very large commit containing hundreds of unrelated changes is difficult to review.

For example:

git commit -m "Complete application changes"

This could contain:

  • 20 controller changes
  • 15 model changes
  • Database migrations
  • Frontend changes
  • Bug fixes
  • Configuration changes

Instead, divide the work into meaningful commits.

git commit -m "Add customer registration API"

git commit -m "Add customer database migration"

git commit -m "Add customer validation rules"

git commit -m "Add customer API tests"

9. Best Practice #7 — Use Conventional Commit Types

For larger teams, using a consistent commit format can make Git history easier to read. One popular approach is Conventional Commits.

The general structure is:

: 

Example:

feat: add customer registration API

Common Commit Types

Type Meaning Example
feat New feature feat: add invoice export
fix Bug fix fix: correct invoice total
refactor Code restructuring without changing intended behavior refactor: simplify payment service
docs Documentation docs: update API installation guide
test Add or update tests test: add invoice service tests
style Formatting or style-only changes style: format invoice controller
chore Maintenance tasks chore: update Laravel dependencies
perf Performance improvement perf: optimize product search query

For example:

git commit -m "feat: add pharmacy invoice generation"

git commit -m "fix: prevent duplicate invoice numbers"

git commit -m "refactor: move payment logic to service"

git commit -m "test: add payment service tests"

git commit -m "docs: update API documentation"

10. Best Practice #8 — Use Scope When Your Team Needs More Detail

You can make Conventional Commit messages more descriptive by adding a scope.

Format:

(): 

Laravel examples:

feat(auth): add email verification

fix(payment): correct Razorpay amount calculation

refactor(order): simplify order service

test(invoice): add invoice calculation tests

docs(api): update authentication documentation

The scope tells the team which part of the application was affected.


11. Best Practice #9 — Use git diff Before Committing

Never blindly commit everything. Before creating a commit, inspect your changes.

git status

git diff

You can also inspect staged changes:

git diff --staged

This is especially useful in Laravel projects because you may accidentally include:

  • Debugging code
  • Temporary files
  • Environment files
  • Log files
  • Generated files
  • Unrelated changes

12. Best Practice #10 — Do Not Commit .env

Laravel applications commonly contain sensitive configuration inside the .env file.

For example:

DB_PASSWORD=secret_password
AWS_SECRET_ACCESS_KEY=xxxxxxxx
RAZORPAY_SECRET=xxxxxxxx

Do not commit these secrets to Git.

Laravel projects normally include .env in .gitignore.

.env
/vendor/
/node_modules/
/storage/logs/

Instead, commit the example configuration:

.env.example

13. Best Practice #11 — Don't Commit Debugging Code

Before committing Laravel code, check for temporary debugging statements.

For example:

dd($request->all());

dump($user);

var_dump($data);

logger($data);

These may be useful while debugging, but they should not accidentally enter production code.

Always inspect your changes before committing:

git diff

14. Best Practice #12 — Use .gitignore Properly

Your repository should not contain files that should remain local or generated automatically.

A Laravel project commonly ignores:

/vendor
/node_modules
/public/build
.env
.phpunit.result.cache
/storage/logs

The exact entries depend on the project and deployment process.


15. Best Practice #13 — Commit After a Logical Unit of Work

Do not wait until the end of the entire feature to create one massive commit.

Suppose you are implementing a pharmacy payment feature.

A reasonable sequence could be:

git commit -m "feat(payment): add payment service"

git commit -m "feat(payment): integrate Razorpay API"

git commit -m "test(payment): add payment service tests"

git commit -m "fix(payment): handle failed payment response"

Each commit represents a logical step in the development process.


16. Best Practice #14 — Don't Mix Unrelated Changes

Suppose you are fixing a Laravel authentication issue and also notice a typo in the README.

Don't necessarily create:

git commit -m "Fix login and update README"

Instead:

git commit -m "fix(auth): correct login validation"

git commit -m "docs: fix README installation typo"

This makes the history easier to understand and allows changes to be reverted independently.


17. Best Practice #15 — Use git add Carefully

This command:

git add .

stages many changes at once. It is convenient, but you should understand what you are staging.

Instead, you can stage specific files:

git add app/Services/PaymentService.php
git add tests/Feature/PaymentTest.php

Then check:

git status

git diff --staged

Finally:

git commit -m "feat(payment): add payment service tests"

18. Best Practice #16 — Use Interactive Staging

Git provides interactive staging when only some parts of a file should be committed.

git add -p

Git will show individual changes and ask whether you want to stage them.

This is useful when one file contains multiple logical changes.

For example, a controller may contain:

  • A payment bug fix
  • An unrelated formatting change

You can stage only the payment fix and commit it separately.


19. Best Practice #17 — Write Commits That Can Be Reverted Safely

Small, focused commits are easier to revert.

For example:

git log --oneline

might show:

a82d921 feat(payment): add payment gateway
b91c441 fix(auth): correct login validation
c72d123 docs: update installation guide

If the payment gateway change needs to be removed, you can identify that specific commit.

git revert a82d921

This is much safer than trying to undo a huge commit containing unrelated work.


20. Best Practice #18 — Don't Rewrite Shared History Carelessly

Commands such as:

git reset
git rebase
git commit --amend

can rewrite commit history depending on how they are used.

 

Be especially careful when the commits have already been pushed to a shared branch.

 

For shared branches such as main or develop, avoid rewriting history unless your team's workflow explicitly allows it.

 

 

21. Git Commit Example for a Laravel Feature

 

Imagine you are adding customer registration to a Laravel application.

 

Step 1 — Create the migration

 
php artisan make:migration create_customers_table
 

After completing the migration:

 
git add database/migrations/
git commit -m "feat(customer): add customers table"
 

Step 2 — Create the model

 
php artisan make:model Customer
 

Then:

 
git add app/Models/Customer.php
git commit -m "feat(customer): add customer model"
 

Step 3 — Add registration logic

 
git add app/Http/Controllers/CustomerController.php
git commit -m "feat(customer): add customer registration"
 

Step 4 — Add tests

 
git add tests/
git commit -m "test(customer): add registration tests"
 

Now the Git history clearly describes how the feature was developed.

 

 

22. Example of a Bad Git History

 
git log --oneline

91abc22 changes
72def31 update
61abc11 fix
51abc92 final
42abc21 latest changes
 

These messages don't provide useful information.

 

 

23. Example of a Good Git History

 
git log --oneline

91abc22 test(payment): add failed payment tests
72def31 fix(payment): handle gateway timeout
61abc11 feat(payment): integrate Razorpay payment API
51abc92 feat(order): add order payment status
42abc21 docs(payment): update payment integration guide
 

This history is much easier for another developer to understand.

 

 

24. Good vs Bad Commit Messages

 
Bad Better
fix fix(auth): correct password validation
update code refactor(order): simplify order calculation
changes feat(invoice): add PDF invoice generation
bug fixed fix(payment): prevent duplicate payment requests
API work feat(customer): add customer registration API
final changes test(invoice): add invoice calculation tests
 

 

25. A Practical Commit Workflow

 

A good daily workflow can be:

 
git status

git diff

git add -p

git diff --staged

git commit -m "fix(payment): correct payment amount"

git log --oneline -5

git push origin feature/payment
 

The important idea is:

 
Check → Select → Review → Commit → Push
 

 

26. Git Commit Workflow for Bitbucket

 

Suppose your team uses Bitbucket and your branch is:

 
feature/pharmacy-payment
 

After completing your changes:

 
git status

git diff

git add .

git diff --staged

git commit -m "feat(payment): add pharmacy payment integration"

git push origin feature/pharmacy-payment
 

Then the branch can be reviewed through a Bitbucket Pull Request.

 

A clean commit history makes that Pull Request easier for reviewers to understand.

 

 

27. Should Every Small Change Be a Commit?

 

Not necessarily.

 

The goal is not to create as many commits as possible. The goal is to create meaningful, logical commits.

 

For example, these could reasonably belong together:

 
feat(auth): add password reset functionality
 

if the migration, controller, routes, mail notification, and tests are all parts of the same logical feature.

 

The problem is not commit size alone. The problem is unrelated changes being mixed together.

 

 

28. When Should You Use git commit --amend?

 

If you just created a commit and notice a small mistake, you can use:

 
git commit --amend
 

For example:

 
git commit -m "feat(auth): add email verfication"
 

Suppose you notice the typo in verification. You can correct the message using:

 
git commit --amend -m "feat(auth): add email verification"
 

Be careful with --amend if the commit has already been pushed and other developers are working from that history.

 

 

29. Commit Message Template

 

A simple professional format is:

 
(): 
 

Examples:

 
feat(auth): add Google login

fix(invoice): correct tax calculation

refactor(payment): extract payment service

test(order): add order creation tests

docs(api): update API authentication guide

perf(product): optimize product search query
 

 

30. Common Git Commit Mistakes

 

❌ Mistake 1 — Vague messages

 
git commit -m "update"
 

❌ Mistake 2 — One huge commit

 
git commit -m "complete project"
 

❌ Mistake 3 — Committing secrets

 
.env
 

❌ Mistake 4 — Mixing unrelated changes

 
git commit -m "Fix login, update README, redesign dashboard"
 

❌ Mistake 5 — Committing debug code

 
dd($data);
 

❌ Mistake 6 — Never checking staged files

 
git add .
git commit -m "feature"
 

Instead, inspect your staged changes before committing.

 

 

31. Git Commit Best Practices Cheat Sheet

 
Practice Recommendation
Commit message Clear and meaningful
Commit size Keep it logically focused
Message style Use consistent present/imperative wording
Commit types Use feat, fix, refactor, docs, test, etc.
Review changes Use git diff and git diff --staged
Secrets Never commit .env or credentials
Debugging code Remove temporary debug statements
Unrelated changes Separate them into different commits
Shared history Avoid careless history rewriting
Team workflow Follow the team's commit convention
 

 

32. Important Git Commands for Commits

 
# Check working tree
git status

# See unstaged changes
git diff

# Stage a file
git add filename

# Stage all changes
git add .

# Stage changes interactively
git add -p

# Review staged changes
git diff --staged

# Create commit
git commit -m "feat(auth): add email verification"

# Modify the latest commit
git commit --amend

# View commit history
git log

# View compact history
git log --oneline

# View recent commits
git log --oneline -10

# Revert a commit
git revert 

# Push changes
git push origin feature/my-feature
 

 

33. Git Commit Interview Questions

 

Q1. What is a Git commit?

 

A Git commit is a recorded snapshot of changes in a repository with a unique commit ID and commit message.

 

Q2. What makes a good commit message?

 

A good commit message is clear, concise, meaningful, and explains the purpose of the change.

 

Q3. Why should commits be small?

 

Focused commits are easier to review, understand, revert, cherry-pick, and debug.

 

Q4. What is a Conventional Commit?

 

It is a standardized commit-message format such as:

 
feat: add customer registration
fix: correct invoice calculation
 

Q5. What is the difference between git diff and git diff --staged?

 

git diff shows unstaged changes, while git diff --staged shows changes that have already been added to the staging area.

 

Q6. What is git commit --amend?

 

It modifies the most recent commit, such as correcting its message or adding a missed change.

 

Q7. Why should .env not be committed?

 

Because it can contain sensitive information such as database passwords, API keys, and application secrets.

 

Q8. Should unrelated changes be included in one commit?

 

Generally, no. Unrelated changes should normally be separated into logically focused commits.

 

Q9. What is git add -p?

 

It interactively stages selected parts of files, which is useful when a file contains multiple logical changes.

 

Q10. Why is a clean Git history useful?

 

It makes code review, debugging, reverting, understanding project history, and team collaboration easier.

 

 

34. Recommended Commit Style for PHP/Laravel Projects

 

For a professional Laravel project, a format like this works well:

 
feat(auth): add email verification
fix(order): prevent duplicate order creation
feat(payment): integrate Razorpay gateway
fix(invoice): correct tax calculation
refactor(user): move validation to form request
test(payment): add payment failure tests
docs(api): update authentication documentation
perf(product): optimize product search
chore(deps): update Laravel dependencies
 

The exact convention should be agreed upon by the development team.

 

 

35. Easy Way to Remember Git Commit Best Practices

 

Remember these five rules:

 
1. Clear
2. Focused
3. Consistent
4. Reviewed
5. Safe
 

Clear — Explain what changed.

 

Focused — One logical change per commit.

 

Consistent — Follow the team's commit format.

 

Reviewed — Check staged changes before committing.

 

Safe — Never commit secrets or unnecessary files.

 

 

36. Final Laravel Example

 

Imagine you developed a new pharmacy payment module. A clean commit history could look like this:

 
git log --oneline

d31a82f test(payment): add payment failure tests
b91c441 fix(payment): handle gateway timeout
a82d921 feat(payment): integrate Razorpay payment gateway
c72d123 feat(order): add payment status tracking
b51c912 feat(order): add order payment fields
 

Compare this with:

 
d31a82f final changes
b91c441 updates
a82d921 payment work
c72d123 changes
b51c912 done
 

The first history immediately communicates the development story. The second history requires developers to inspect the code to understand what happened.

 

 

Conclusion

 

Git commits are more than checkpoints. They are part of your project's documentation and development history.

 

For PHP and Laravel developers, good commit practices make team development, Bitbucket Pull Requests, debugging, code reviews, and future maintenance much easier.

 

The most important rule is simple:

 
Write commits that another developer can understand months after you created them.
 

A practical workflow to remember is:

 
git status
git diff
git add
git diff --staged
git commit -m "meaningful message"
git push
 

 

What's Next?

 

Now that we understand how to create clean and meaningful commits, the next important Git topic is working with remote repositories.

 

Next Article: Git Remote, Fetch, Pull and Push Explained

 

We will cover:

 
  •  
  • What is Git remote?
  •  
  • What is origin?
  •  
  • git fetch
  •  
  • git pull
  •  
  • git push
  •  
  • Remote-tracking branches
  •  
  • Upstream branches
  •  
  • Fetch vs Pull
  •  
  • Push vs Pull
  •  
  • Practical Bitbucket workflow
  •  
  • Common interview questions
  •  
 

Was this useful?

No ratings yet — yours can be the first.

Discussion

Notes from readers. Comments appear after a short review.

No comments yet. Start the thread below.