Book Review: How to Implement Design Patterns in PHP
Getting Set up with Ogre 3D on Ubuntu
Symfony 2 Crash Course
JQuery Venetian Blinds Transition Effect
Enforce Coding Standards with PHP_CodeSniffer and Eclipse IDE on Ubuntu Linux
Scrollable Tables with Floating Header using CSS

Git Pre-commit Hook to Block Accidental Commits

Saturday, 1 December 18, 6:56 pm
Here's a simple pre-commit hook to prevent you commiting files you don't want to:
#!/bin/bash git diff --cached --diff-filter=AM | grep -q gitblock if [ $? -eq 0 ] then echo gitblock comment detected exit 1 fi
Save it as .git/hooks/pre-commit in the repo you want to protect, make it executable, and it will prevent you commiting any file that contains the text "gitblock" in a comment.

The hook uses git diff's --cached argument to get all the changes in the files currently staged for commit, with the --diff-filter argument to restrict it to added files and modified files. The diff is piped into grep which exits with zero if the search string is found. If it is found, a message is printed and the hook itself exits with a non-zero code.

I made this pre-commit hook so I could make changes for test purposes without worrying about accidentally commiting them. Whenever I make such a change, I just comment it like this:
$prices = $this->shopService->fetch30DaysPrices($this->store->getCurrencyCode($currencyId)); if (!count($prices) && false) { // gitblock . . .
All git hooks are stored in the same folder, .git/hooks, and if you look in there, you will see a *.sample file for each available hook. Some of these examples might be useful, I haven't really looked at them. The idea is straightforward though - any executable script with the appropriate name will be run at the expected time. Thus you can write your hooks in anything - bash, python, PHP or even C. You can run whatever checks you want, just make your exitcode non-zero to abort the commit.

Global Hooks

You can create hooks just as above but which apply to all your git repos by designating a directory that contains all such global hooks, using git config like so:
git config --global core.hooksPath /path/to/global/hooks

Undo a Pushed Commit

Even in spite of this, you might find you inadvertently commit code you didn't want to - for instance, if you're on the wrong branch. If you've pushed your commit to a central repository, it is still possible to undo without making a mess of the commit history. Obviously this should be done with care, and only when you know for sure that no one else has already pulled down your commit. The following two commands do the magic:
git reset --hard HEAD~ git push origin +<branch name>
The first line restores your local repo to the state it was one commit ago - if you want to undo more than one commit, append the number of commits you want to undo to the end of the line e.g. to undo the 2 most recent commits use git reset --hard HEAD~2. The second command above pushes the current state of your local repo to origin: note the + in front of the branch name, which does a force push and thus removes the unwanted commits from the remote.

Please enter your comment in the box below. Comments will be moderated before going live. Thanks for your feedback!

Cancel Post

/xkcd/ Kedging Cannon