Git 使用 Heroku 将当前分支推送到远程

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

Git push current branch to a remote with Heroku

githerokuproductionstaging

提问by cmaughan

I'm trying to create a staging branch on Heroku, but there's something I don't quite get.

我正在尝试在 Heroku 上创建一个暂存分支,但我不太明白。

Assuming I've already created a heroku app and setup the remote to point to staging-remote, If I do:

假设我已经创建了一个 heroku 应用程序并将遥控器设置为指向 staging-remote,如果我这样做:

git checkout -b staging staging-remote/master

I get a local branch called 'staging' which tracks staging-remote/master - or that's what I thought....

我有一个名为“staging”的本地分支,它跟踪 staging-remote/master - 或者这就是我的想法......

But:

但:

git remote show staging-remote

Gives me this:

给我这个:

remote staging
  Fetch URL: [email protected]:myappname.git
  Push  URL: [email protected]:myappname.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    staging-remote merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

As you can see, the pull looks reasonable, but the default push does not. It implies that if I do:

如您所见,pull 看起来很合理,但默认 push 不合理。这意味着如果我这样做:

git push staging-remote

git push staging-remote

I'm going to push my local master branch up to the staging branch. But that's not what I want.... Basically, I want to merge updates into my staging branch, then easily push it to heroku without having to specify the branch like so:

我要把我本地的 master 分支推送到 staging 分支。但这不是我想要的......基本上,我想将更新合并到我的暂存分支中,然后轻松地将其推送到 heroku 而不必像这样指定分支:

git push staging-remote mybranch:master

The above isn't hard to do, but I want to avoid accidentally doing the previous push and pushing the wrong branch... This is doubly important for the production branch I'd like to create!

以上不难做到,但我想避免不小心做了之前的推送并推送了错误的分支......这对于我想创建的生产分支来说是加倍重要的!

I've tried messing with git config, but haven't figured out how to get this right yet...

我试过搞乱 git config,但还没有想出如何做到这一点......

回答by thekingoftruth

I have tested it and @juba and @MatthewFord's versions work perfectly!

我已经对其进行了测试,@juba 和 @MatthewFord 的版本运行良好!

git config remote.staging.push staging:master

This pushes my local topic branch named staginginto remote branch masteron the remote repository named staging.

这推动我的本地命名的主题分支升级到远程分支在指定的远程仓库暂存

@nickgrim put it in the general form like so:

@nickgrim 把它写成这样的一般形式:

git config remote.[remoteRepositoryName].push [localBranchName]:[remoteBranchName]

Update:

更新:

Furthermore, modern git will conveniently run the above configuration command for you when you git pushwith the -uoption:

此外,当您git push使用以下-u选项时,现代 git 将方便地为您运行上述配置命令:

git push -u staging staging:master

回答by MatthewFord

I have a branch called heroku, and this worked for me:

我有一个名为 heroku 的分支,这对我有用:

git config remote.heroku.push heroku:master

the problem you're facing is heroku ignores all branches other than master.

您面临的问题是 heroku 忽略了除 master 之外的所有分支。

回答by mcloud79

From the book "O'Reilly - Version Control with Git" page 184 | Chapter 11: Remote Repositories

来自“O'Reilly - 使用 Git 进行版本控制”一书第 184 页 | 第 11 章:远程存储库

During a git push operation, you typically want to provide and publish the changes you made on your local topic branches. To allow others to find your changes in the remote repository after you upload them, your changes must appear in that repository as topic branches. Thus, during a typical git push command, the source branches from your repository are sent to the remote repository using a refspec such as:

+refs/heads/*:refs/heads/*

This refspec can be paraphrased as: From the local repository, take each branch name found under the source namespace refs/heads/and place it in a similarly named, matching branch under the destination namespace refs/heads/in the remote repository. The first refs/heads/refers to your local repository (because you're executing a push), and the second refers to the remote repository. The asterisks ensure that all branches are replicated. ...

在 git push 操作期间,您通常希望提供和发布您在本地主题分支上所做的更改。要让其他人在您上传后在远程存储库中找到您的更改,您的更改必须作为主题分支出现在该存储库中。因此,在典型的 git push 命令期间,您的存储库中的源分支使用 refspec 发送到远程存储库,例如:

+refs/heads/*:refs/heads/*

此 refspec 可以解释为:从本地存储库中,获取在源命名空间下找到的每个分支名称 refs/heads/,并将其放置refs/heads/在远程存储库中目标命名空间下的名称相似的匹配分支中。第一个refs/heads/是指您的本地存储库(因为您正在执行推送),第二个是指远程存储库。星号确保复制所有分支。...



That's why the example from juba should fail. the corrected refspec should be:

这就是为什么来自朱巴的例子应该失败的原因。更正后的 refspec 应该是:

git config remote.staging-remote.push +refs/heads/local_branch_name:refs/heads/master

回答by juba

From the page Everiday Git with 20 commands or so:

带有 20 个左右命令的 Everiday Git页面:

http://www.kernel.org/pub/software/scm/git/docs/everyday.html

http://www.kernel.org/pub/software/scm/git/docs/everyday.html

It seems that you can achieve what you want to do by adding a config directive to your local git repository, something like :

似乎您可以通过向本地 git 存储库添加配置指令来实现您想要做的事情,例如:

git config remote.staging-remote.push mybranch:refs/remotes/staging-remote/master

Then, if you do a git pushfrom your mybranchlocal branch, it should be pushed to the masterbranch of your staging-remoteremote.

然后,如果您git pushmybranch本地分支执行 a ,则应将其推送到staging-remote远程的master分支。

Nevertheless, please verify with git remote show staging-remoteand carefully test it before using it, as I'm far from a git expert...

尽管如此,请git remote show staging-remote在使用之前验证并仔细测试它,因为我远非 git 专家......

回答by juba

This works. I have used it more than a few times for setting up clients with git-flow, heroku, and a backup git service.

这有效。我已经多次使用它来设置带有 git-flow、heroku 和备份 git 服务的客户端。

.git/config for the repo:

.git/config 用于 repo:

[core]
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
  ignorecase = true
[heroku]
  account = youraccount
[remote "origin"]
  url = [email protected]:youruser/yoursite.heroku.com.git # or github, etc.
  fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
  remote = origin
  merge = refs/heads/master
[branch "staging"]
  remote = origin
  merge = refs/heads/staging
[branch "develop"]
  remote = origin
  merge = refs/heads/develop
[remote "production"]
  pushurl = [email protected]:your-prod-app.git
  push = master:master
[remote "staging"]
  pushurl = [email protected]:your-staging-app.git
  push = staging:master

All working correctly:

一切正常:

git push origin

git push origin

git pull origin

git pull origin

git push staging

git push staging

git push production

git push production

Think about fetch and push as like stdout and stdin, where both can be redirected or closed to be one way. Also if anyone knows how to get these settings without hacking .git/config, please feel free to amend with an edit, karma points are sure to follow.

把 fetch 和 push 想象成 std​​out 和 stdin,两者都可以被重定向或关闭为一种方式。此外,如果有人知道如何在不破解 .git/config 的情况下获得这些设置,请随时通过编辑进行修改,因果点肯定会随之而来。

回答by Lawrence I. Siden

I'm having the same problem trying to figure out how to deal with Heroku's policy of ignoring all branches but 'master'. It kinda defeats the whole point of keeping separate branches if you can only ever test the master branch on Heroku.

我在试图弄清楚如何处理 Heroku 忽略除“master”之外的所有分支的策略时遇到了同样的问题。如果您只能在 Heroku 上测试 master 分支,那么保持单独分支的意义就大打折扣了。

The consequence of this restriction is that whatever local topic branch I may be working on, I'd like an easy way to switch Heroku's master to that local topic branch and do a "git push -f" to over-write master on Heroku. Needless to say, it would be a very good idea to have a separate remote repository (like Github), to back everything up without this restriction. I'd call that one "origin" and use "heroku" for Heroku so that "git push" always backs up everything.

这种限制的结果是,无论我正在处理什么本地主题分支,我都想要一种简单的方法将 Heroku 的 master 切换到该本地主题分支,并执行“git push -f”以覆盖 Heroku 上的 master。不用说,拥有一个单独的远程存储库(如 Github)来备份所有内容而不受此限制,这将是一个非常好的主意。我将其称为“起源”并为 Heroku 使用“heroku”,以便“git push”始终备份所有内容。

What I got from reading the "Pushing Refspecs" section of http://progit.org/book/ch9-5.htmlis

我从阅读http://progit.org/book/ch9-5.html的“Pushing Refspecs”部分中得到的是

git push heroku local-topic-branch:refs/heads/master

git push heroku local-topic-branch:refs/heads/master

What I'd really like is a way to set this up in the config file so that "git push heroku" always does the above, replacing "local-topic-branch" with the name of whatever my current branch happens to be.

我真正想要的是一种在配置文件中进行设置的方法,以便“git push heroku”始终执行上述操作,将“local-topic-branch”替换为我当前分支的名称。

I may ask this as a new question, to see if anyone else has figured out how to do this.

我可能会将此作为一个新问题提出,看看其他人是否已经想出了如何做到这一点。

回答by cmaughan

I couldn't figure out a way to do this, but in the end I found a handy rake task to make it easy: http://www.jbarnette.com/2009/11/10/deploying-to-heroku.html

我想不出办法来做到这一点,但最后我找到了一个方便的 rake 任务来简化它:http: //www.jbarnette.com/2009/11/10/deploying-to-heroku.html