在开发过程中,我们在提交代码到远程仓库时,需要进行一系列git add, git commit,git pull, git push等命令,而且在git add部分修改文件时,更加麻烦。因此,将一系列命令用脚本来执行,会事半功倍。该脚本可以指定提交部分file或者是全部提交,并可以指定commit message(mac上测试已经通过)
1、使用方式:./submit.sh 1 2 4 5 "commit message"
其中,1 2 4 5 为git status需要add的file的行号,最后一个参数为commit message。
如:git status后显示:
modified: src/test/java/com/zsq/xxx/xxx/service/IEvaOperaServiceTest.java
modified: src/test/java/com/zsq/xxx/xxx/service/IMapServiceTest.java
……
可以选择任意的行号进行add 和 commit。
如果选择全部,则可以使用 ./submit.sh . "commit message"
注意:
(1)如果脚本放在项目目录,则:
./submit.sh . "all commit"
(2)如果脚本放在项目目录的父目录,则命令为:
cd .. && ./submit.sh . "all commit" && cd 项目dir
2、脚本命名为submit.sh 内容如下:
#!/bin/bash
#print the directory and file
# created by zsq on 2018/04/10
# modified these commands when project changes
# 进入项目目录
cd 项目dir
# 切换分支
git checkout dev-pinyin
# 拉取最新代码
git pull origin xxx
echo ${PWD}
# commit all files
if [[ "$1" == "." ]]; then
# add all files
git add .
git commit -m "$2"
echo "commit message: $2"
else
# choose files to add .
# please pay attention to the $# value when do shift operation,
# so a variable 'len' is nessisary to save the length of parameters in bash command tempororyly
echo "these files have been added to local git respository: "
len=$#
for ((i=0;i<$[${len}-1];i++));
do
# echo ${len}
a=$1
shift
var=$(git status | grep -E "new file|modified" | cut -d ' ' -f 4 | sed -n ${a}p)
echo " ${a}--${var}"
git add ${var}
done
# add commit message.
# in fact, there is only one parameter left which is the commit message
for (( i = $#; i < $[$#+1]; i++ )); do
a=$1
shift
# echo ${a}
echo "commit message: ${a}"
git commit -m "${a}"
done
fi
# push the commit to remote respository
git push
Tips: 黑体部分需要根据实际项目以及branch进行相应的变化