我有几个分支需要删除,为了完成此任务,我尝试在 Git Bash (Win Server 2016) 上运行 shell 脚本。该脚本读取包含分支列表的文本图 block 。这是脚本:
$ cat clean_up.sh
#!/bin/bash
cd $1
branch_list="$2"
git checkout master
while read -r branch
do
git push origin -d $branch
git branch -D $branch
done < "$branch_list"
$ cat branch_list.txt
service-integration
display
...
...
$ clean_up.sh repo_name /e/git/branch_list.txt
Already on 'master'
Your branch is up to date with 'origin/master'.
fatal: Invalid refspec ':service-integration?'
error: branch 'service-integration?' not found.
fatal: Invalid refspec ':display?'
error: branch 'display?' not found.
...
...
$ git --version
git version 2.18.0.windows.1
但是,如果我手动cd
进入存储库并执行git checkoutbranch_name,然后尝试运行上述命令来删除任何分支(远程和本地),它工作得很好。我完全困惑为什么它在脚本中不起作用。 :(
最佳答案
您可能在要导入的文件中将行结尾设置为 CRLF。当您在 shell 脚本中读取文件时,它会将 CR 作为留置权末尾的字符读取,并将其解释为分支名称的一部分。
关于git - 无法使用脚本删除 Git 分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51540828/