git自动提交脚本

bash脚本

可以用bsah写命令行脚本。

比如写了个git.sh,然后用bash git.sh来运行。

这个是我写来自动提交git的脚本:

#!/bin/bash
echo "================="
echo "auto git by JOEY"
echo "======= 🤪  ========="

echo -e  "
▶ \033[33;1mgit add .
\033[0m"
git add .

git status
echo -e "
▶ \033[33;1mcommit message:
\033[37;1m" 
read msg

echo -e "
▶ \033[33;1mgit commit -m '$msg'
\033[0m"
git commit -m "$msg"

echo -e "
▶ \033[33;1mgit push
"
echo -e "\033[37;1mstart pushing ...\033[0m
"
git push
echo -e "
\033[37;1mAll Done\033[0m"
  • echo,是用来打印在命令行上面的

image-20190807211753140
  • 然后是正常的git操作,也可以加点别的东西

  • echo -n的作用是,打印完这句话不换行。

  • 下一行的read会让命令行等待,直至用户输入信息。输入的信息在这里会被放进msg这个变量里面

    当然read -p也能做到,不过只能输入一个单词,多输入的内容会被执行,就很麻烦。

  • 然后把输入的msg加到commit的信息里面,就能上传了

  • 最后自动push到远端仓库

执行效果

image-20190807213516221

inquirer+simple-git

Simple-git能获取git的信息,比如有哪些没提交、哪些提交了。

const git = require('simple-git');

git()
  .status((err, status) => { console.log("status", status) })
image-20190809195423774

而inquirer能打造一个可交互的命令行,来让提交变得更友好。

console.log("=================")
console.log("auto git by JOEY")
console.log("====== 🤪========")

const inquirer = require('inquirer')
const promptList = [
  {
    type: 'checkbox',
    message: 'choose files to add',
    name: 'add',
    choices: [
      "file1.js",
      "file2.go"
    ]
  },
  {
    type: "input",
    message: "commit message:",
    name: "commit"
  }
]
inquirer.prompt(promptList).then((answers) => {
  console.log('结果为:')
  console.log(answers)
})
image-20190809195501733
image-20190809195516890

这种交互,总比手动输入git add的文件名要好多了吧

Last updated

Was this helpful?