Search code examples
gitgithubgit-branchchocolatey

How to see a newly created branch in a Chocolatey Fork on Github locally and subsequently push to this branch?


Summary

  • Github Chocolatey Repository has been forked to own Github account as Chocolatey-1
  • A new branch installchocolateyservice has been created
  • installchocolateyservice has been pulled
  • A push to installchocolateyservice branch fails, while it works to push to the master
  • git branch indicates that only the master is recognized

Verbose

Extra branch created in Chocolatey Fork

enter image description here

Other branch than master pulled

C:\chocolatey-1>git pull origin installchocolateyservice
remote: Reusing existing pack: 4611, done.
remote: Counting objects: 25, done.
remote: Compressing objects: 100% (25/25), done.
rRemote: Total 4636 (delta 11), reused 0 (delta 0)eceiving objects: 100% (4636/4
Receiving objects: 100% (4636/4636), 21.86 MiB | 276.00 KiB/s, done.

Resolving deltas: 100% (2792/2792), done.
From github.com:030/chocolatey-1
 * branch            installchocolateyservice -> FETCH_HEAD
 * [new branch]      installchocolateyservice -> origin/installchocolateyservice

Push to installchocolateyservice branch failed

C:\chocolatey-1>git push origin installchocolateyservice
error: src refspec installchocolateyservice does not match any.
error: failed to push some refs to 'git@github.com:030/chocolatey-1.git'

Push to master works

C:\chocolatey-1>git push origin master
Everything up-to-date

Only master branch is recognized

C:\chocolatey-1>git branch
* master

C:\chocolatey-1>

Solution

  • This command

    git branch
    * master
    

    doesn't show your new branch because it doesn't exist as a local branch yet, only as a remote-tracking branch. You'll be able to see it if you pass the --remotes/-r or the --all/-a option,

    git branch -a
    * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/installchocolateyservice 
      remotes/origin/master
    

    Creating local branches from remote branches

    If you wanted to create a local version of the branch, the correct way to do it would have been to use git fetch followed by a checkout, not git pull, because git pull is a fetch followed by a merge into your current branch.

    So the proper command sequence is

    git fetch origin
    git checkout -b installchocolateyservice origin/installchocolateyservice 
    

    Note that if you checkout a non-existent local branch X, and there is a remote-tracking branch called origin/X, then a new local branch X will be created from origin/X and set up to track it by default. So, in other words, instead of using the above commands, you can also do

    git fetch origin
    git checkout installchocolateyservice
    

    Now that you have a proper local branch, you should be able to push to the remote one:

    git push origin HEAD
    # Or
    git push origin installchocolateyservice