【提效系列】快速提交git代码

1,279 阅读2分钟

如果你没听说过“腱鞘炎”,那么恭喜你不用深受腱鞘炎的困扰。我得腱鞘炎3年了,尤其是最近,每到晚上躺倒床上休息时,就能明显感受到手腕隐隐作痛。腱鞘炎算不上什么大病,但是痛起来真的要命。这也是那么长时间没有更文的原因之一。

近一年的提交记录.jpg

最近无意看到我近一年的代码提交记录,发现平均下来,每周commit几十次不等。每次机械式的 git addgit commitgit push 手动敲命令,也没觉得有什么不妥,但是全年的提交记录,这个量还是很大的。我就在想,能不能减少git命令重复性常规动作,提升开发效率。于是就有了对现有脚手架的升级,一行命令搞定 npm run push '*****',减轻腱鞘压力 。

我们的项目形式呢,每个活动,每个产品都是一个独立项目,我更新了新脚手架,新项目可以直接使用。老项目需要手动添加配置命令和文件比较麻烦,于是我写了一个shell脚本,老脚手架可以一键升级。

新脚手架开箱既用

一行命令:

npm run push '*****'

代替原命令:

git add .
git commit -m '*****'
git push 

老脚手架一键升级

sh <(curl -s -L https:/****/autoGit.sh) https:/****/autoGit.sh 脚本的具体静态资源路径。

如遇代码冲突或者其它commit、push失败问题,需手动处理。

你想了解的 autoGit.sh脚本:

npm i shelljs -D 

touch autoPush.js

echo 'const shell = require("shelljs");' > autoPush.js

echo 'const { which, exec, echo, exit } = shell;' >> autoPush.js

echo 'if (!which("git")) {' >> autoPush.js
echo '	echo("Sorry, this script requires git");' >> autoPush.js
echo '  exit(1);' >> autoPush.js
echo '}' >> autoPush.js

echo 'exec(`git pull`);' >> autoPush.js

echo 'const commitDesc = process.argv[2] || "auto-commit";' >> autoPush.js
echo 'if (exec("git add .").code !== 0) {' >> autoPush.js
echo '    echo("Error: Git add failed");' >> autoPush.js
echo '    exit(1);' >> autoPush.js
echo '}' >> autoPush.js

echo 'if (exec(`git commit -am "${commitDesc}"`).code !== 0) {' >> autoPush.js
echo '  if (exec("git push").code !== 0) {' >> autoPush.js
echo '    echo("Error: Git push failed");' >> autoPush.js
echo '    exit(1);' >> autoPush.js
echo '  } else {' >> autoPush.js
echo '    echo("Error: Git commit failed");' >> autoPush.js
echo '    exit(1);' >> autoPush.js
echo '  }' >> autoPush.js
echo '}' >> autoPush.js

echo 'exec(`git pull`);' >> autoPush.js
echo 'if (exec("git push").code !== 0) {' >> autoPush.js
echo '  echo("Error: Git push failed");' >> autoPush.js
echo '  exit(1);' >> autoPush.js
echo '}' >> autoPush.js 

sed -i "" "8 a\   
   \ \ \ \ \"push\": \"node ./autoPush.js\",
" package.json


这种快捷式命令,一行命令代替三行命令,使用习惯后,容易遗忘基础命令,所以快速提交git代码并不强制组内同学使用,只是对脚手架基础命令做了扩展,有需要的同学自行“食用”。