Automated Git Shell Script for Cloning, Branch Creation, and Remote Push
This article introduces a shell script that automates the typical Git workflow of cloning a repository, creating a new development branch from master, pushing the branch to the remote server, and establishing upstream tracking, allowing developers to focus solely on code changes.
The article explains a practical need in software development: repeatedly pulling the master branch from a remote Git repository, creating a feature branch, and pushing it back for build, deployment, and testing. To streamline these steps, the author provides a custom shell script that handles all Git operations automatically.
The script requires three arguments: the project name, the Git clone URL, and the desired feature‑branch name. It validates the argument count, prints the received parameters, and aborts if the count is incorrect.
It then determines the current working directory, generates a timestamp, and constructs a directory name combining the project name and date. The script removes any existing directory with that name, creates a fresh one, and changes into it.
Next, it performs git clone using the provided URL, checks for errors, and exits on failure. After cloning, it navigates into the newly created project folder, creates a new local branch from master with git checkout -b, and pushes the branch to the remote repository using git push origin. If the push fails, the script reports an error and exits.
Finally, the script sets the upstream tracking relationship with
git branch --set-upstream-to=origin/ {feature}, lists branch details with git branch -vv, and prints a friendly reminder that the user can now open IntelliJ IDEA to start coding.
The full script content is:
#!/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..."The article concludes with a brief note encouraging readers to try the script and a signature from the author, a Java community leader.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
