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

15

我正在尝试在Heroku上创建一个staging分支,但有些东西我不是很明白。

假设我已经创建了一个Heroku应用程序并设置了远程指向staging-remote,如果我执行:

git checkout -b staging staging-remote/master

我有一个名为“staging”的本地分支,它跟踪着staging-remote/master - 或者至少我曾经这样认为...

但是:

git remote show staging-remote
给我这个:
remote staging
  Fetch URL: git@heroku.com:myappname.git
  Push  URL: git@heroku.com: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)

从上面可以看出,拉取看起来很合理,但默认的推送不是这样。 这意味着如果我执行以下操作:

git push staging-remote

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

git push staging-remote mybranch:master

上述操作并不难,但我希望避免意外地进行先前的推送并推送错误的分支...... 这对我想创建的生产分支来说尤为重要!

我尝试过更改git配置,但还没有弄清楚如何正确实现...

7个回答

25

我已经测试过了,@juba和@MatthewFord的版本都完美运行!

git config remote.staging.push staging:master

这将我的名为staging的本地主题分支推送到名为staging的远程存储库的远程分支master中。

@nickgrim按以下通用形式提出了问题:

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

更新:

此外,现代版的Git将在您使用-u选项进行git push时方便地为您运行上述配置命令:

git push -u staging staging:master

1
换句话说:
git config remote.[本地分支名称].push [远程名称]:[远程分支名称]
- David Alpert
2
@DavidAlpert:不是的,你反过来了;你想要: git config remote。[remoteName] .push [localBranchName]:[remoteBranchName] - nickgrim
对于简单的推送,可以使用以下命令:git push localbranch remotename:remotebranch - vinyll
@vinyll,那不正确。我认为你的意思是这样的:git push remotename localbranch:remotebranch - thekingoftruth

7

我有一个名为Heroku的分支,以下内容对我有效:

git config remote.heroku.push heroku:master

你面临的问题是Heroku忽略除主分支以外的所有分支。

3

来自书籍《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. ...

这就是为什么juba的例子会失败的原因。修正后的refspec应该是:
git config remote.staging-remote.push +refs/heads/local_branch_name:refs/heads/master

1

来自页面每天使用大约20个命令的Git

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

看起来你可以通过向本地git仓库添加一个配置指令来实现你想要做的事情,类似于:

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

然后,如果你从本地的mybranch分支执行git push,它应该被推送到staging-remote远程的master分支。

不过,在使用之前,请通过git remote show staging-remote进行验证并仔细测试,因为我离git专家还有很远的路要走...


0

这个可行。我已经多次使用它来为客户设置git-flow、heroku和备份git服务。

.git/config文件适用于该存储库:

[core]
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
  ignorecase = true
[heroku]
  account = youraccount
[remote "origin"]
  url = git@bitbucket.org: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 = git@heroku.com:your-prod-app.git
  push = master:master
[remote "staging"]
  pushurl = git@heroku.com:your-staging-app.git
  push = staging:master

所有工作正常:

git push origin

git pull origin

git push staging

git push production

将fetch和push视为类似于stdout和stdin,两者都可以重定向或关闭为单向。此外,如果有人知道如何在不破坏.git/config的情况下获取这些设置,请随意进行编辑,肯定会获得声望积分。


0

我也遇到了同样的问题,试图弄清楚如何处理Heroku忽略除'master'以外所有分支的策略。这有点打败了保持不同分支的整个目的,如果你只能在Heroku上测试主分支。

这种限制的后果是,无论我正在工作的本地主题分支是什么,我都希望有一种简单的方法将Heroku的主分支切换到该本地主题分支,并执行“git push -f”以覆盖Heroku上的主分支。不用说,最好有一个单独的远程存储库(如Github),以备份所有内容而没有此限制。我会称之为“origin”,并使用“heroku”来代表Heroku,以便“git push”始终备份所有内容。

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

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

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

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


1
git config remote.heroku.push HEAD:master - Chris Johnsen

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接