[英]How I can choose next branch via bash script
I've written a script to list all local branches in my repository. 我编写了一个脚本来列出存储库中的所有本地分支。
#!/bin/bash
clear
branches=()
menuposition=0
eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/heads/)"
for branch in "${branches[@]}"; do
menuposition=$((menuposition+1))
echo "$menuposition) $branch"
done
The output is ... 输出是...
1) master
2) foo
3) bar
I can use read command to get user input. 我可以使用读取命令来获取用户输入。 But, ... How can I, then, checkout the branch choosed by user?
但是,...然后,我该如何检出用户选择的分支?
#!/bin/bash
clear
options=()
#for bash version 4 or higher use mapfile.
#Fallback to while loop if mapfile not found
mapfile -t options < <(git for-each-ref \
--format='%(refname:short)' refs/heads/) &>/dev/null \
|| while read line; do options+=( "$line" ); done \
< <(git for-each-ref --format='%(refname:short)' refs/heads/)
options+=('Exit')
select opt in "${options[@]}"
do
if [[ "$opt" ]] && [[ "$opt" == 'Exit' ]]; then
echo "Bye Bye"
exit 0
elif [[ "$opt" ]]; then
git checkout "$opt"
else
echo "Wrong Input. Please enter the correct input"
fi
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.