>当我运行以下命令时
git branch | grep solution- | cut -c 3- | xargs git checkout
我得到此错误输出
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
我得到这个正确的输出
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 版本上失败。
最后,只是为了证明 xargs 在我运行时会得到正确的参数git branch | grep solution- | cut -c 3- | xargs echo
我会得到solution-1 solution-2 solution-3
这就是我所期望的。
从你的问题中,我得出结论,你有一个 git 存储库,其中包含 3 个分支解决方案-1、解决方案-2 和解决方案-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 checkout 只执行一次,解决方案-1 作为分支名称,解决方案-2 和解决方案-3 作为文件名。
现在,您实际上希望为xargs的每个参数逐个切换分支。为此,您可以运行以下命令。
git branch | grep solution- | cut -c 3- | xargs -d 'n' -n1 git checkout