Search code examples
gitgit-pushgit-remote

Git push is not 'pushing' a new branch


I checked out a new branch from our repo. Everything is working fine for our develop branch. But on the new branch 'push' isn't doing anything.

Everything looks normal - there are 2 commits to push.

-> git branch -vv
  develop   8ab7ef1 [origin/develop] Merge branch 'develop' of git.example.com:core-platform into develop
* hotfix112 8521cef [origin/hotfix/1.1.2: ahead 2] CORE-1263 - Completed merging from dev into hot fix.

But Push doesn't do anything:

-> git push
Everything up-to-date

-> git status
# On branch hotfix112
# Your branch is ahead of 'origin/hotfix/1.1.2' by 2 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

I found that 'git pull' uses remote, but 'git push' doesn't:

-> git remote show origin
* remote origin
  Fetch URL: [email protected]:core-platform.git
  Push  URL: [email protected]:core-platform.git
  HEAD branch: develop
  Remote branches:
    core-platform-1.0.0 tracked
    develop             tracked
    hotfix/1.1.2        tracked
    master              tracked
    release/1.0.0       tracked
    release/1.1.1       tracked
  Local branches configured for 'git pull':
    develop   merges with remote develop
    hotfix112 merges with remote hotfix/1.1.2
  Local ref configured for 'git push':
    develop pushes to develop (up to date)
->

I cannot figure out why hotfix112 is not linked to the remote, but the pull is.

How do I fix this configuration?


Solution

  • Git acts as you've described because you have the following situation:

    • you have a local branch (hotfix112) which is tracking a differently-named remote branch
    • you have push.default option set to matching (which is default) or simple

    As usually the case with git, you have a few alternatives how to set up git to act as desired:

    1.) The easiest would be to change the name of your local branch to match the name of the remote branch. Afterwards, push starts working automatically. So just rename branch 'hotfix112' to 'hotfix/1.1.2':

    git branch -m hotfix112 hotfix/1.1.2
    

    2.) You can change the push behaviour via setting option push.default to 'tracking'. Because you already have branch 'hotfix112' set to track origin/hotfix/1.1.2, git push will work as desired. To set the local git option run:

    git config --local push.default tracking
    

    3.) You can manually edit your .git/config file and set push to match local refspec 'hotfix112' to remote 'hotfix/1.1.2'. You should add the push line below your [remote "origin"] section:

    [remote "origin"]                                                               
      url = ...
      fetch = ...
      push = hotfix112:hotfix/1.1.2
    

    First and third approach work only on the hotfix112 branch. The second works for all tracking branches in that repository (or globally if global option was used).