Git实用技巧与错误恢复指南
防止误推送的钩子
在使用Git进行版本控制时,有时候我们可能会不小心推送一些不希望推送的提交。这时,一个能阻止这类推送的钩子就非常实用。我们可以指定任何想要阻止的关键词,例如reword、temp、nopush、temporary或hack等。不过,有时候我们又希望这些包含特定关键词的提交能够通过推送。
我们可以编写一个小的检查器,它能检查特定的关键词,列出包含这些关键词的提交,并询问是否仍然要推送。以下是一个示例脚本:
#!/bin/bash COMMITS=$(git log --grep "nopush" --format=format:%H) if [ $COMMITS ]; then exitmaybe=1 fi if [ $exitmaybe -eq 1 ]; then while true do 'clear' for commit in "$COMMITS" do echo "$commit has no push in the message" done echo "Are you sure you want to push the commit(s) " read REPLY <&1 case $REPLY in [Yy]* ) break;; [Nn]* ) exit 1;; * ) echo "Please answ