![](/img/trans.png)
[英]How to check if git has differences between 2 branch in a bash script
[英]How to Bash Script Git Local Branch Check?
尋求解決方案以實現以下目標:
到目前為止,我已經有了它的工作,但不是那里。 我的問題實際上是后者,但我想花點時間重新思考整個事情並獲得一些關於如何更好地寫這個的反饋。
變量existing_branch
在分支存在時提供SHA和refs / heads / branchName ,否則git將保持並提供預期的fatal:
check_for_branch() {
args=("$@")
echo `$branch${args[0]}`
existing_branch=$?
}
create_branch() {
current="git rev-parse --abbrev-ref HEAD"
branch="git show-ref --verify refs/heads/"
args=("$@")
branch_present=$(check_for_branch ${args[0]})
echo $branch_present
read -p "Do you really want to create branch $1 " ans
case $ans in
y | Y | yes | YES | Yes)
if [ ! -z branch_present ]; then
echo "Branch already exists"
else
`git branch ${args[0]}`
echo "Created ${args[0]} branch"
fi
;;
n | N | no | NO | No)
echo "exiting"
;;
*)
echo "Enter something I can work with y or n."
;;
esac
}
您可以避免提示分支是否已存在,並稍微縮短腳本,如下所示:
create_branch() {
branch="${1:?Provide a branch name}"
if git show-ref --verify --quiet "refs/heads/$branch"; then
echo >&2 "Branch '$branch' already exists."
else
read -p "Do you really want to create branch $1 " ans
...
fi
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.