工作中常用的一个Git脚本

1200 篇文章 1 订阅

前言

在实际开发中,我们很频繁的需要从git远程仓库拉取master代码建立分支进行开发,开发完毕后,我们需要push到远程进行build、部署和测试。

一个自动化脚本

 

git脚本1-26行

运行这个git脚本,需要项目名/git clone url/你的开发分支名称(比如feature/xxx)

 

 

git脚本27-46行

上面脚本的意思,就是想在特定的目录中,进行git clone,并从master新建本地开发分支。

 

git脚本47-62行

把本地开发分支push到远程,并建立它们之间的关联关系,之后就可以打开idea进行开发啦~

 

运行结果

 

 

运行脚本参数不正确

 

 

运行正常

 

 

目录查看确认

 

脚本内容

#!/bin/sh

#脚本执行需要3个参数

if [ $# -eq 3 ]

then

echo "开始执行git脚本..."

echo "项目名:$1 , git克隆地址:$2 , 你的新建分支名称:$3"

else

echo "脚本执行需要3个参数:项目名 git克隆地址 你的新建分支名称"

exit -1

fi

#获取当前执行脚本路径

dir=`pwd`

#获取今天的日期,格式:yyyymmdd

time=`date +%Y%m%d`

#项目名

project=$1

#git clone 地址

gitcloneurl=$2

#你的本地分支名称

feature=$3

#删除目录,为新建目录做准备

rm -rf "${project}-${time}"

mkdir "$dir"/"${project}-${time}"

cd "$dir"/"${project}-${time}"

#git clone

git clone "$gitcloneurl"

if [ $? -ne 0 ]; then

echo "git clone url 错误"

exit -1

fi

#切换到项目根目录

cd "$dir"/"${project}-${time}"/"${project}"

#从master新建本地分支

git checkout -b "$feature"

#git push,创建远程分支

git push origin "$feature":"$feature"

if [ $? -ne 0 ]; then

echo "git push 错误"

exit -1

fi

#建立本地分支与远程分支的关联关系,为push做准备

git branch --set-upstream-to=origin/"$feature"

#查看分支建立情况

git branch -vv

echo "you can open IntelliJ IDEA to write Java code..."

 

到这里,就结束了,希望对你有帮助,^_^

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Git指令的Shell脚本,能够快速便捷地管理Git库,包括添加修改、提交修改、显示库状态、推送到远程库、从远程库更新到本地、版本恢复等操作。 使用方法: 1. 在Linux系统,将本文件放在Git库目录下,利用Shell运行本文件; 2.在windows系统,需下载安装与操作系统相对应的Git软件,并将本文件放在Git库目录下,双击即可运行。 运行示例: Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? h A: Add all changes to repository. C: Commit all changes to repository. D: Show differences between current version with HEAD->. E: Exit shell script. F: Fetch origin/master and merge. L: Show latest two-weeks logs of repository. P: Push commissions to origin/master. U: User command mode(Press ‘Enter’ to exit). R: Reset current version according version_id. S: Show status of repository. Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? s On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: Git.sh modified: PyNote/PyNote_2.md no changes added to commit (use "git add" and/or "git commit -a") Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? a On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD ..." to unstage) modified: Git.sh modified: PyNote/PyNote_2.md

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值