git Github:将 gh-pages 镜像到 master

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5807459/
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 05:22:56  来源:igfitidea点击:

Github: Mirroring gh-pages to master

gitgithubgithub-pagesgit-tower

提问by Ben Everard

I'm developing a jQuery plugin that's being hosting on GitHub. It has a demo included of which I'm manually copying and pushing to the branch gh-pages, what I'd like to do is have it so when I push a change to masterit is automatically pushed to gh-pages, or at least a setup where they are mirrored.

我正在开发一个托管在 GitHub 上的 jQuery 插件。它包含一个演示,其中我手动复制并推送到分支gh-pages,我想做的是拥有它,以便在我推送更改master时自动推送到gh-pages,或者至少是镜像的设置.

I've already seen this questionbut not sure if it really answers my question with regard to these requirements:

我已经看过这个问题,但不确定它是否真的回答了我关于这些要求的问题:

  1. I use Tower, I don't mind using the terminal (Mac) to make changes to config, so long as the solution works with this GUI.
  2. I only want this 'mirroring' on certain repos, not on all of them on my machine.
  1. 我使用Tower,我不介意使用终端 (Mac) 更改配置,只要该解决方案适用于此 GUI。
  2. 我只希望在某些存储库上进行这种“镜像”,而不是在我的机器上的所有存储库上进行这种“镜像”。

Cheers

干杯

采纳答案by Steve

git checkout gh-pages
git merge master
git push origin gh-pages

回答by christianvuerings

Add the following 2 lines to the [remote "origin"]section of .git/config:

将以下 2 行添加到 的[remote "origin"]部分.git/config

push = +refs/heads/master:refs/heads/gh-pages
push = +refs/heads/master:refs/heads/master

Every time you pushit will automatically push master to gh-pages as well. I'm using this for the jQuery Lifestream project.

每次你push它也会自动将 master 推送到 gh-pages。我将它用于jQuery Lifestream 项目

回答by MCSDWVL

Do not do what denbuzze suggests above!! The + (plus sign) in the push makes it quietly accept non-fastforward updates. I found out the hard way that this can irrevocably cause work to be lost by leading to dangling commits. Simply removing the plus signs makes this a safer approach.

不要做上面denbuzze建议的事情!!推送中的 +(加号)使它安静地接受非快进更新。我发现这很难通过导致悬空提交而不可避免地导致工作丢失。简单地删除加号使这是一种更安全的方法。

push = refs/heads/master:refs/heads/gh-pages
push = refs/heads/master:refs/heads/master

now instead of causing a force update this will cause a warning & pull suggestion

现在而不是导致强制更新,这将导致警告和拉取建议

To https://github.com/someuser/repo.git
 ! [rejected]        master -> gh-pages (fetch first)
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/someuser/repo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

回答by raviolicode

I'm adding further explanation to @denbuzzeand @MCSDWVLanswers.

我正在为@denbuzze@MCSDWVL答案添加进一步的解释。

If you want to push both to masterand gh-pagesautomatically each time you run git push origin, you probably want to add a Refspec to the git config of your repo.

如果您想在每次运行时都推送到mastergh-pages自动git push origin,您可能希望将 Refspec 添加到您的存储库的 git 配置中。

So, according to the git-scm book, you can add two RefSpecs, by adding two pushvalues to the repo config file .git/config:

因此,根据git-scm book,您可以通过向repo 配置文件添加两个值来添加两个 RefSpecspush.git/config

[remote "origin"]
url = https://github.com/<github_user>/<repo_name>
      fetch = +refs/heads/*:refs/remotes/origin/*
      push = refs/heads/master:refs/heads/master
      push = refs/heads/master:refs/heads/gh-pages

That will cause a git push originto:

这将导致git push origin

  1. Push the local masterbranch to the remote masterbranch
  2. Push the local masterbranch to the remote gh-pagesbranch
  1. 将本地master分支推送到远程master分支
  2. 将本地master分支推送到远程gh-pages分支

by default.

默认情况下。

Note: using a +before the spec causes to force push to the repo. Use it with caution:

注意+在规范导致强制推送到 repo 之前使用 a 。谨慎使用:

The format of the refspec is an optional +, followed by <src>:<dst>, where <src>is the pattern for references on the remote side and <dst>is where those references will be written locally. The +tells Git to update the reference even if it isn't a fast-forward.

refspec 的格式是一个可选的+, 后跟<src>:<dst>, where<src>是远程端引用的模式,以及<dst>这些引用将在本地写入的位置。+告诉Git的更新,即使它不是一个快进的参考。

回答by dhulihan

I personally like to wrap this in an alias:

我个人喜欢用别名来包装它:

alias gpogh="git checkout gh-pages && git merge master && git push origin gh-pages && git checkout -"

This mirrors your master to gh-pages, pushes to github, then switches back the previous branch you were working on.

这将您的 master 镜像到gh-pages,推送到 github,然后切换回您正在处理的前一个分支。

回答by azl

commitand pushto master..

提交送到主..

then :

然后 :

git checkout gh-pages  // -> go to gh-pages branch
git rebase master // bring gh-pages up to date with master
git push origin gh-pages // commit the changes
git checkout master // return to the master branch

回答by DJ Jaiswal

OR you can just use the cmd below, this will push your local master branch to gh-pages master branch. git push -f origin master:gh-pages

或者您可以只使用下面的 cmd,这会将您的本地 master 分支推送到 gh-pages master 分支。 git push -f origin master:gh-pages

回答by Dan Dascalescu

UPDATE: GitHub now allows pages to be published from any branch and directory you want.

更新GitHub 现在允许从您想要的任何分支和目录发布页面。



It was much easier for me to use the gh-pagesbranch as master. There's nothing magical about "master"; it's just another branch name. There issomething magical about gh-pages, because that's where GitHub is looking for index.html to serve your page.

对我来说,将gh-pages分支用作主分支要容易得多。“大师”并没有什么神奇之处;这只是另一个分支名称。还有就是关于GH-页面神奇的东西,因为这是GitHub上寻找index.html的服务页面。

Read more in my other answer on this topic.

我关于此主题的其他答案中阅读更多内容

Using gh-pagesas master is also easier than subtrees, which are easier than mirroring. You could use git subtreeas described hereor here: if you have a directory which contains your demo, you can push that directory to the gh-branchwith one command. Let's say you name the directory gh-pagesto make things clear. Then after you've committed and pushed your changes to master, run this to update gh-pages:

gh-pages作主树也比子树更容易,子树比镜像更容易。您可以git subtree按照此处此处的描述使用:如果您有一个包含演示的目录,则可以gh-branch使用一个命令将该目录推送到。假设您命名目录gh-pages以使事情清楚。然后在您提交并将更改推送到 后master,运行此命令以更新 gh-pages:

git subtree push --prefix gh-pages origin gh-pages

The problem is if your files in gh-pagesrefer to files in other directories outside it. Symlinks don't work, so you'll have to copyfiles in the directory that serves as gh-pages.

问题是您的文件是否gh-pages引用了它之外的其他目录中的文件。符号链接不起作用,因此您必须文件复制到用作 gh-pages 的目录中。

If you use gh-pagesas master, this problem won't occur.

如果您使用gh-pagesmaster,则不会发生此问题。