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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33172857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:27:48  来源:igfitidea点击:

How do I force a subtree push to overwrite remote changes?

gityeomangit-subtree

提问by ele

We use a subtree deployment a lá this Gistto deploy a subdirectory of our Yeomanproject. In our case, the branch is called production, not gh-pages.

我们使用子树部署和这个 Gist来部署我们Yeoman项目的子目录。在我们的例子中,分支被称为production,而不是gh-pages

This worked perfectly until yesterday when the Git server rejected the command git subtree push --prefix dist origin production, saying

直到昨天,当 Git 服务器拒绝命令时,这一切都运行良好git subtree push --prefix dist origin production,说

 ! [rejected]        9fe1683aa574eae33ee6754aad702488e0bd37df -> production (non-fast-forward)
error: failed to push some refs to '[email protected]:web-and-new-media/graduation2015.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart.

If I switch to the production branch locally (which is clean), git pullreturns Your branch is up-to-date with 'origin/production'.even if I use the --rebaseoption.

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

I can see the contents of the production branch on the server through our web UI and there's nothing there that shouldn't be, just the compiled output of our distdirectory like I'd expect.

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

To that end, it seems like I should safely be able to force an overwrite of these files, but how? git subtree pushdoesn't have a --forceoption.

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

回答by ele

The trick was to chain the subtree split into a forced push:

诀窍是将分裂的子树链接成强制推送:

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

I got this from the Addendum of http://clontz.org/blog/2014/05/08/git-subtree-push-for-deployment/, who actually references thisanswer on Stack Overflow. I had skimmed this one earlier but Steve Clontz's post made it click for me.

我从http://clontz.org/blog/2014/05/08/git-subtree-push-for-deployment/的附录中得到了这个,他实际上在 Stack Overflow 上引用了这个答案。我早些时候浏览过这个,但史蒂夫克朗茨的帖子让我点击了它。

回答by raphtlw

There is simpler solution

有更简单的解决方案

This always works for me.

这总是对我有用。

  1. Set up a bash script which helps you do the following:
  1. 设置一个 bash 脚本来帮助您执行以下操作:
cd dist
git init
git add .
git commit -am "deploy"
git push --force <remote URL> master
rm -rf .git
cd ..

This creates a new git repository in the subdirectory you specify and force pushes everything from there.

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

  1. profit
  1. 利润

回答by binarymason

There is actually a solution that is much more simple.

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

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

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

Setup

设置

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

Making changes

做出改变

$ 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 ..