【问题标题】:Git push current branch to a remote with HerokuGit 使用 Heroku 将当前分支推送到远程
【发布时间】:2010-03-08 12:32:44
【问题描述】:

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

假设我已经创建了一个 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)

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

git push staging-remote

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

git push staging-remote mybranch:master

上面做起来不难,但是我想避免不小心做了之前的推送,推错了分支……这对于我想要创建的生产分支来说是双重重要的!

我试过弄乱 git config,但还没有弄清楚如何解决这个问题......

【问题讨论】:

    标签: git heroku production staging


    【解决方案1】:

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

    git config remote.staging.push staging:master
    

    这会将名为 staging 的本地主题分支推送到名为 staging 的远程存储库上的远程分支 ma​​ster

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

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

    更新:

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

    git push -u staging staging:master
    

    【讨论】:

    • 换句话说:> git config remote.[localBranchName].push [remoteName]:[remoteBranchName]
    • @DavidAlpert:不,你搞错了;你想要:git config remote.[remoteName].push [localBranchName]:[remoteBranchName]
    • 对于简单的推送,给出: git push localbranch remotename:remotebranch
    • @vinyll 不正确。我想你的意思是:git push remotename localbranch:remotebranch
    【解决方案2】:

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

    git config remote.heroku.push heroku:master
    

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

    【讨论】:

      【解决方案3】:

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

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

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

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


      这就是来自 juba 的示例应该失败的原因。更正后的 refspec 应该是:

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

      【讨论】:

        【解决方案4】:

        从页面 Everiday Git 有 20 条左右的命令

        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-remotemaster 分支em> 远程。

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

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

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

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

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

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

            我可能会提出一个新问题,看看是否有其他人知道如何做到这一点。

            【讨论】:

            • git config remote.heroku.push HEAD:master
            【解决方案7】:

            这行得通。我已经多次使用它来设置带有 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

            像 stdout 和 stdin 一样考虑 fetch 和 push,两者都可以被重定向或关闭为一种方式。此外,如果有人知道如何在不修改 .git/config 的情况下获得这些设置,请随时进行修改,业力点肯定会随之而来。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-09-10
              • 2016-07-08
              • 2013-04-16
              • 2015-12-15
              • 1970-01-01
              • 1970-01-01
              • 2020-12-31
              • 2012-07-25
              相关资源
              最近更新 更多