如何强制子树推送覆盖远程更改?

时间:2015-10-16 14:24:39

标签: git yeoman git-subtree

我们使用子树部署láthis Gist来部署我们Yeoman项目的子目录。在我们的例子中,分支称为生产,而不是 gh-pages

直到昨天Git服务器拒绝命令git subtree push --prefix dist origin production,说

时,这种情况一直很好
 ! [rejected]        9fe1683aa574eae33ee6754aad702488e0bd37df -> production (non-fast-forward)
error: failed to push some refs to 'git@gitlab.sdstate.edu:web-and-new-media/graduation2015.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart.

如果我在本地切换到生产分支(这是干净的),即使我使用git pull选项,Your branch is up-to-date with 'origin/production'.也会返回--rebase

我可以通过我们的Web UI在服务器上看到生产分支的内容,并且没有什么不应该是,只是我期望的dist目录的编译输出。

为此,似乎我应该能够安全地强制覆盖这些文件,但是如何? git subtree push没有--force选项。

3 个答案:

答案 0 :(得分:56)

诀窍是将子树拆分为强制推送:

git push origin `git subtree split --prefix dist master`:production --force

我从http://clontz.org/blog/2014/05/08/git-subtree-push-for-deployment/的附录中得到了这个,他实际上在Stack Overflow上引用了this个答案。我之前已经略过了这个,但Steve Clontz的帖子让我点击了它。

答案 1 :(得分:3)

有一个更简单的解决方案

这总是对我有用。

  1. 设置一个bash脚本,可帮助您执行以下操作:
cd dist
git init
git add .
git commit -am "deploy"
git push --force <remote URL> master
rm -rf .git
cd ..

这将在您指定的子目录中创建一个新的git存储库,并从此处强制推送所有内容。

  1. 利润

答案 2 :(得分:2)

实际上有一个更简单的解决方案。

来源:https://gist.github.com/cobyism/4730490#gistcomment-2337463

设置

$ rm -rf dist
$ echo "dist/" >> .gitignore
$ git worktree add dist gh-pages

进行更改

$ make # or what ever you run to populate dist
$ cd dist
$ git add --all
$ git commit -m "$(git log '--format=format:%H' master -1)"
$ git push origin production --force
$ cd ..
相关问题