Bash xargs与git签出失败,出现pathspec错误

Bash xargs与git签出失败,出现pathspec错误,bash,xargs,Bash,Xargs,当我运行下面的命令时 git checkout solution-1 && git checkout solution-2 && git checkout solution-3 git分支| grep解决方案| cut-c3 | xargs git签出 我得到这个错误输出 error: pathspec 'solution-2' did not match any file(s) known to git. error: pathspec 'solution-3

当我运行下面的命令时

git checkout solution-1 && git checkout solution-2 && git checkout solution-3
git分支| grep解决方案| cut-c3 | xargs git签出

我得到这个错误输出

error: pathspec 'solution-2' did not match any file(s) known to git.
error: pathspec 'solution-3' did not match any file(s) known to git.
当我运行下面的命令时

git checkout solution-1 && git checkout solution-2 && git checkout solution-3
git签出解决方案-1&&git签出解决方案-2&&git签出解决方案-3

我得到了正确的输出

Switched to branch 'solution-1'
Your branch is ahead of 'master' by 1 commit.
  (use "git push" to publish your local commits)
Switched to branch 'solution-2'
Your branch is ahead of 'master' by 1 commit.
  (use "git push" to publish your local commits)
Switched to branch 'solution-3'
Your branch is ahead of 'master' by 3 commits.
  (use "git push" to publish your local commits)
我不明白为什么git在xargs版本上失败


最后,如果我运行
git branch | grep solution-| cut-c3-| xargs echo
I得到
solution-1 solution-2 solution-3
,这就是我所期望的。

从你的问题中,我得出结论,你有一个包含3个分支的git回购协议solution-1、solution-2和solution-3

现在,当您运行下面的命令时

git checkout solution-1 && git checkout solution-2 && git checkout solution-3
您可以获得正确的输出,因为git签出每次都会获得一个有效的分支名称(它使用不同的分支名称运行了3次)

现在,让我们看看为什么在下面运行命令时出错

git branch | grep solution- | cut -c 3- | xargs git checkout
运行上述命令相当于运行

git checkout solution-1 solution-2 solution-3
这将提示您相同的错误。这是因为git签出只执行一次,分支名为solution-1,文件名为solution-2&solution-3

现在,您实际上想要为来自xargs的每个参数逐个切换分支。为此,您可以运行以下命令

git branch | grep solution- | cut -c 3- | xargs -d '\n' -n1  git checkout