git - 无法使用脚本删除 Git 分支

标签 git git-bash

我有几个分支需要删除,为了完成此任务,我尝试在 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/

相关文章:

git - 如何仅为特定分支创建 Git 标签?

git - 什么是 git checkout -b <branchname> ?

git - 无法将文件添加到 Git 存储库

windows - 带有 yarn 的终端中的不可见文本

bash - 如何使用带有自定义 bashrc 的 Git bash 运行脚本?

git - 如何从不同分支上的本地提交复制文件?

node.js - 使用 git 部署到 digitalocean 上的 dokku

windows - 为新的 2.32.0.windows.1 版本的 git 克隆私有(private) Repo 挂起

git-bash - mkdir : cannot create directory 'example' : Permission denied

git - 如何将子node_modules添加到.gitignore?