Why do I need to explicitly push a new branch?

前端 未结 8 1544
遥遥无期
遥遥无期 2020-11-22 00:48

I am new in git and I am practicing. I created a local branch but I saw that when I did git push my branch was not uploaded to the repository. I ha

相关标签:
8条回答
  • 2020-11-22 01:28

    At first check

    Step-1: git remote -v
    //if found git initialize then remove or skip step-2

    Step-2: git remote rm origin
    //Then configure your email address globally git

    Step-3: git config --global user.email "youremail@example.com"

    Step-4: git initial

    Step-5: git commit -m "Initial Project"
    //If already add project repo then skip step-6

    Step-6: git remote add origin %repo link from bitbucket.org%

    Step-7: git push -u origin master

    0 讨论(0)
  • 2020-11-22 01:30

    The actual reason is that, in a new repo (git init), there is no branch (no master, no branch at all, zero branches)

    So when you are pushing for the first time to an empty upstream repo (generally a bare one), that upstream repo has no branch of the same name.

    And:

    • the default push policy was 'matching' (push all the branches of the same name, creating them if they don't exist),
    • the default push policy is now 'simple' (push only the current branch, and only if it has a similarly named remote tracking branch on upstream, since git 1.7.11)

    In both cases, since the upstream empty repo has no branch:

    • there is no matching named branch yet
    • there is no upstream branch at all (with or without the same name! Tracking or not)

    That means your local first push has no idea:

    • where to push
    • what to push (since it cannot find any upstream branch being either recorded as a remote tracking branch, and/or having the same name)

    So you need at least to do a:

    git push origin master
    

    But if you do only that, you:

    • will create an upstream master branch on the upstream (now non-empty repo): good.
    • won't record that the local branch 'master' needs to be pushed to upstream (origin) 'master' (upstream branch): bad.

    That is why it is recommended, for the first push, to do a:

    git push -u origin master
    

    That will record origin/master as a remote tracking branch, and will enable the next push to automatically push master to origin/master.

    git checkout master
    git push
    

    And that will work too with push policies 'current' or 'upstream'.
    In each case, after the initial git push -u origin master, a simple git push will be enough to continue pushing master to the right upstream branch.

    0 讨论(0)
提交回复
热议问题