Git push current branch to a remote with Heroku

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

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

  • git-svn branching: how to configure git config
  • git how to disable push
  • What happens to local code when we try to switch the branch in the sourcetree?
  • How does git detect similar files, for its rename detection?
  • Eclipse: Some or all projects can not be imported because they already exist in the workspace
  • Find the latest commit that had a particular word/line in GIT
  • 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….

    But:

    git remote show staging-remote
    

    Gives me this:

    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)
    

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

    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:

    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 repository to staging site to live site
  • git tag -l not displaying the most recent releases
  • Listing Local and Remote Git Repos using Git shell
  • Can tags be automatically moved after a git filter-branch and rebase?
  • Git exception in Eclipse when code not using git
  • git diff/log and the order of start/end commits specified
  • 7 Solutions collect form web for “Git push current branch to a remote with Heroku”

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

    git config remote.staging.push staging:master
    

    This pushes my local topic branch named staging into remote branch master on the remote repository named staging.

    @nickgrim put it in the general form like so:

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

    Update:

    Furthermore, modern git will conveniently run the above configuration command for you when you git push with the -u option:

    git push -u staging staging:master
    

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

    git config remote.heroku.push heroku:master
    

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

    From the book “O’Reilly – Version Control with Git” page 184 | Chapter 11: Remote Repositories

    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.


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

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

    From the page Everiday Git with 20 commands or so :

    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 config remote.staging-remote.push mybranch:refs/remotes/staging-remote/master
    

    Then, if you do a git push from your mybranch local branch, it should be pushed to the master branch of your staging-remote remote.

    Nevertheless, please verify with git remote show staging-remote and carefully test it before using it, as I’m far from a git expert…

    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

    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.

    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.

    What I got from reading the “Pushing Refspecs” section of http://progit.org/book/ch9-5.html is

    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.

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

    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/config for the repo:

    [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
    

    All working correctly:

    git push origin

    git pull origin

    git push staging

    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.

    Git Baby is a git and github fan, let's start git clone.