[英]Github: Mirroring gh-pages to master
I'm developing a jQuery plugin that's being hosting on GitHub.我正在开发一个托管在 GitHub 上的 jQuery 插件。 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 master
it is automatically pushed to gh-pages
, or at least a setup where they are mirrored.它包含一个演示,我手动复制并推送到分支
gh-pages
,我想做的是拥有它,所以当我将更改推送到master
时,它会自动推送到gh-pages
,或者至少是它们被镜像的设置。
I've already seen this question but not sure if it really answers my question with regard to these requirements:我已经看过这个问题,但不确定它是否真的回答了我关于这些要求的问题:
Cheers干杯
Add the following 2 lines to the [remote "origin"]
section of .git/config
:将以下 2 行添加到
.git/config
的[remote "origin"]
部分:
push = +refs/heads/master:refs/heads/gh-pages
push = +refs/heads/master:refs/heads/master
Every time you push
it will automatically push master to gh-pages as well.每次
push
时,它也会自动将 master 推送到 gh-pages。 I'm using this for the jQuery Lifestream project .我将它用于jQuery Lifestream 项目。
git checkout gh-pages
git merge master
git push origin gh-pages
Do not do what denbuzze suggests above .. The + (plus sign) in the push makes it quietly accept non-fastforward updates.不要做上面 denbuzze 建议的事情。推送中的 +(加号)使它安静地接受非快进更新。 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.
简单地删除加号使这是一种更安全的方法。
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.
I'm adding further explanation to @denbuzze and @MCSDWVL answers.我正在为@denbuzze和@MCSDWVL 的答案添加进一步的解释。
If you want to push both to master
and gh-pages
automatically each time you run git push origin
, you probably want to add a Refspec to the git config of your repo.如果您想在每次运行
git push origin
时自动推送到master
和gh-pages
,您可能需要将 Refspec 添加到您的 repo 的 git 配置中。
So, according to the git-scm book , you can add two RefSpecs , by adding two push
values to the repo config file .git/config
:因此,根据git-scm book ,您可以通过将两个
push
值添加到 repo 配置文件.git/config
来添加两个RefSpecs :
[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 origin
to:这将导致
git push origin
送到:
master
branch to the remote master
branchmaster
分支推送到远程master
分支master
branch to the remote gh-pages
branchmaster
分支推送到远程gh-pages
分支by default.默认。
Note : using a +
before the spec causes to force push to the repo.注意:在规范之前使用
+
会导致强制推送到 repo。 Use it with caution:谨慎使用:
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.refspec 的格式是可选的
+
,后跟<src>:<dst>
,其中<src>
是远程端引用的模式,<dst>
是这些引用将在本地写入的位置。 The+
tells Git to update the reference even if it isn't a fast-forward.+
告诉 Git 更新参考,即使它不是快进。
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,然后切换回您正在处理的上一个分支。
OR you can just use the cmd below, this will push your local master branch to gh-pages master branch.或者您可以只使用下面的 cmd,这会将您的本地 master 分支推送到 gh-pages master 分支。
git push -f origin master:gh-pages
commit and push to master..提交并推送到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
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-pages
branch as master.将
gh-pages
分支用作 master 对我来说要容易得多。 There's nothing magical about "master"; “大师”没有什么神奇之处; it's just another branch name.
这只是另一个分支名称。 There is something magical about gh-pages, because that's where GitHub is looking for index.html to serve your page.
gh-pages有一些神奇之处,因为这是 GitHub 正在寻找 index.html 为您的页面提供服务的地方。
Read more in my other answer on this topic .在我关于此主题的其他答案中阅读更多内容。
Using gh-pages
as master is also easier than subtrees, which are easier than mirroring.使用
gh-pages
作为 master 也比子树更容易,子树比镜像更容易。 You could use git subtree
as described here or here : if you have a directory which contains your demo, you can push that directory to the gh-branch
with one command.您可以使用此处或 此处所述的
git subtree
:如果您有一个包含演示的目录,您可以使用一个命令将该目录推送到gh-branch
。 Let's say you name the directory gh-pages
to make things clear.假设您将目录命名为
gh-pages
以使事情清楚。 Then after you've committed and pushed your changes to master
, run this to update gh-pages:然后,在您提交更改并将更改推送到
master
之后,运行此命令以更新 gh-pages:
git subtree push --prefix gh-pages origin gh-pages
The problem is if your files in gh-pages
refer to files in other directories outside it.问题是如果您在
gh-pages
中的文件引用了它之外其他目录中的文件。 Symlinks don't work, so you'll have to copy files in the directory that serves as gh-pages.符号链接不起作用,因此您必须将文件复制到用作 gh-pages 的目录中。
If you use gh-pages
as master , this problem won't occur.如果您使用
gh-pages
作为 master ,则不会出现此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.