我有一个 GitHub 工作流程,可以从
main
分支构建静态站点,并将构建推送到 static
分支。基本上,它的作用是这样的:
# Clone the main branch
# ...
# Build the site to the `output/` directory
# ...
# Minimal git config required for push
git config --global user.name 'My Name'
git config --global user.email '[email protected]'
git config --global push.autoSetupRemote true
# Switch to the static branch, creating it if it doesn't exist
git switch -C static
# *
# Add and commit the output/ directory (use -f because it is in .gitignore)
git add -f output/
git commit -m "Automatic site build: $(date)"
# Push
git push
这个脚本在 first 运行时运行得非常好。然而,我运行它的第二次时, 它在
git push
上失败,并出现 fetch first
错误,因为远程 static
分支包含 last 构建的输出,而 main
分支不知道这一点。
我认为,解决此问题的一种方法是在标记为
git pull
的点插入 # *
,但是如果在另一个存储库中重用该脚本,这将导致脚本在 first 运行时失败,因为 static
分支刚刚创建,尚未设置其远程设置(请注意,git 配置中没有与 pull.autoSetupRemote
对应的 push.autoSetupRemote
)。
所以,我真正想做的是插入一个像
if [branch static exists in remote]; git pull; end
这样的命令。
注意:我的问题不是关于 GitHub 页面。
if git ls-remote --exit-code origin static; then
git pull
fi
请参阅文档。