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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Tips For Using a Git Pre-Commit Hook

Tips For Using a Git Pre-Commit Hook

David Winterbottom user avatar by
David Winterbottom
·
Jun. 24, 12 · Interview
Like (0)
Save
Tweet
Share
10.96K Views

Join the DZone community and get the full member experience.

Join For Free

Here's a few tips for using a Git pre-commit hook.

Keep your hook script in source control

Commit your hook script (say pre-commit.sh) at the root of your project and include the installation instructions in your README/documentation to encourage all developers use it.

Installation is nothing more than:

ln -s ../../pre-commit.sh .git/hooks/pre-commit

Then everyone benefits from running the same set of tests before committing and updates are picked up automatically.

Stash unstaged changes before running tests

Ensure that code that isn't part of the prospective commit isn't tested within your pre-commit script. This is missed by many sample pre-commit scripts but is easily acheived with git stash:

# pre-commit.sh
git stash -q --keep-index

# Test prospective commit
...

git stash pop -q

The -q flags specify quiet mode.

Run your test suite before each commit

Obviously.

It's best to have a script (say run_tests.sh) that encapsulates the standard arguments to your test runner so your pre-commit script doesn't fall out of date. Something like:

# pre-commit.sh
git stash -q --keep-index
./run_tests.sh
RESULT=$?
git stash pop -q
[ $RESULT -ne 0 ] && exit 1
exit 0

where a sample run_tests.sh implementation for a Django project may look like:

# run_tests.sh
./manage.py test --settings=settings_test -v 2

Skip the pre-commit hook sometimes

Be aware of the --no-verify option to git commit. This bypasses the pre-commit hook when committing, which is useful is you have just manually run your test suite and don't need to see it run again when committing.

I use a git aliases to make this easy:

# ~/.bash_aliases
alias gc='git commit'
alias gcv='git commit --no-verify'

Search your sourcecode for debugging code

At some point, someone will try and commit a file containing

import pdb; pdb.set_trace()

or some other debugging code. This can be easily avoided using the pre-commit.sh file to grep the staged codebase and abort the commit if forbiden strings are found.

Here's an example that looks for console.log:

FILES_PATTERN='\.(js|coffee)(\..+)?$'
FORBIDDEN='console.log'
git diff --cached --name-only | \
    grep -E $FILES_PATTERN | \
    GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n $FORBIDDEN && echo 'COMMIT REJECTED Found "$FORBIDDEN" references. Please remove them before commiting' && exit 1

 It's straightforward to extend this code block to search for other terms.

Hook Git

Published at DZone with permission of David Winterbottom, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • Create a REST API in C# Using ChatGPT
  • Java REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: