Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Created February 16, 2013 21:22
Show Gist options
  • Save IQAndreas/4968787 to your computer and use it in GitHub Desktop.
Save IQAndreas/4968787 to your computer and use it in GitHub Desktop.
Shell function that requires that a GIT index is clean before running a command. The script allows modified or untracked files to exist in the repository, so long as the index is empty.
function require_clean_index()
{
# TODO: Allow to manually set which git directory
git diff-index --quiet --cached HEAD
if [ $? -ne 0 ]; then
echo "This command requires a clean index. Please reset or commit changes and try again.";
exit 2;
fi
}
# ----- Example use -----
require_clean_index &&
git add src/modified-file &&
git commit -m "Modified the file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment