Git push current branch to a remote with Heroku
Asked Answered
U

7

15

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 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: [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:

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...

Uziel answered 8/3, 2010 at 12:32 Comment(0)
K
25

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
Kalikalian answered 2/10, 2010 at 9:8 Comment(4)
in other words: > git config remote.[localBranchName].push [remoteName]:[remoteBranchName]Achromatous
@DavidAlpert: nope, you've got that backwards; you want: git config remote.[remoteName].push [localBranchName]:[remoteBranchName]Amidst
for simple push, that gives: git push localbranch remotename:remotebranchStamina
@Stamina that is not correct. I think you mean this: git push remotename localbranch:remotebranchKalikalian
A
7

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.

Autumnautumnal answered 19/5, 2010 at 16:45 Comment(0)
M
3

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
Mangrum answered 17/6, 2010 at 15:5 Comment(0)
C
1

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...

Cavalierly answered 7/4, 2010 at 9:57 Comment(0)
S
0

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 = [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 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.

Svensen answered 8/3, 2010 at 12:32 Comment(0)
U
0

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

Uziel answered 9/3, 2010 at 10:20 Comment(0)
S
0

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.

Smilacaceous answered 4/6, 2010 at 2:47 Comment(1)
git config remote.heroku.push HEAD:masterAnacreon

© 2022 - 2024 — McMap. All rights reserved.