Search code examples
githerokuconfigstaging

Git config to push <foo_local_branch>:<bar_remote_branch> by default?


I'm looking to set up a clean deploy approach for both production and staging on heroku and was wondering about a fairly subtle git behavior. We have two heroku apps up and running, and my .git/config looks like:

[remote "prod"]
     url = https://git.heroku.com/<appname>.git
     fetch = +refs/heads/*:refs/remotes/heroku/*
[remote "staging"]
     url = https://git.heroku.com/<appname>.git
     fetch = +refs/heads/*:refs/remotes/heroku/*

So if I want to push my main repo’s master branch to staging, the command would be:

$ git push staging master

But in our workflow, we use a staging branch to consolidate features that are in development. Deploying the staging branch to the staging app then requires:

$ git push staging staging:master

Question: is there any configuration setting for git that would allow me to push the local staging branch to the remote master branch by default? Ie, I would like to run:

 $ git push staging master

or even better

$ git push staging

and what would be executed would have the same effect as git push staging staging:master.


Solution

  • You can change the upstream branch of your local staging branch to stating/master

    git branch --set-upstream-to=staging/master
    

    If you now run git push from your local staging branch, you will push to staging/master

    You can also tell git to track a particular remote branch when creating your local branch

    git checkout -b staging staging/master
    

    And you can see which remote branch your local branch is tracking

    git branch -vv