Why do I need to explicitly push a new branch?

前端 未结 8 1524
遥遥无期
遥遥无期 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:04

    I just experienced a further permutation of this issue.

    I had a branch named feat/XYZ-1234-some-description because I was working on Jira issue 1234. During the work I created a new Jira issue to track a smaller piece of work, and when I came to push I decided to push to a branch name with this new issue number in:

    git push -u origin feat/XYZ-5678-a-different-description # failed
    

    This gave me the error being discussed in this SO thread. But since I was trying to push to a different branch name from my current branch, my problem was different to the one described here. I ended up renaming my local branch before I could push it:

    git branch -m feat/XYZ-1234-some-description feat/XYZ-5678-a-different-description
    git push -u origin feat/XYZ-5678-a-different-description # now works
    

    After a bit more reading around I realised that I could have set a src on the git push, either to the current branch name, or just HEAD if appropriate:

    git push -u origin feat/XYZ-1234-some-description:feat/XYZ-5678-a-different-description # also works
    
    0 讨论(0)
  • 2020-11-22 01:10

    Output of git push when pushing a new branch

    > git checkout -b new_branch
    Switched to a new branch 'new_branch'
    > git push
    fatal: The current branch new_branch has no upstream branch.
    To push the current branch and set the remote as upstream, use
    
        git push --set-upstream origin new_branch
    

    A simple git push assumes that there already exists a remote branch that the current local branch is tracking. If no such remote branch exists, and you want to create it, you must specify that using the -u (short form of --set-upstream) flag.

    Why this is so? I guess the implementers felt that creating a branch on the remote is such a major action that it should be hard to do it by mistake. git push is something you do all the time.

    "Isn't a branch a new change to be pushed by default?" I would say that "a change" in Git is a commit. A branch is a pointer to a commit. To me it makes more sense to think of a push as something that pushes commits over to the other repositories. Which commits are pushed is determined by what branch you are on and the tracking relationship of that branch to branches on the remote.

    You can read more about tracking branches in the Remote Branches chapter of the Pro Git book.

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

    I couldn't find a rationale by the original developers this quickly, but I can give you an educated guess based on a few years of Git experience.

    No, not every branch is something you want to push to the outside world. It might represent a private experiment.

    Moreover, where should git push send all the branches? Git can work with multiple remotes and you may want to have different sets of branches on each. E.g. a central project GitHub repo may have release branches; a GitHub fork may have topic branches for review; and a local Git server may have branches containing local configuration. If git push would push all branches to the remote that the current branch tracks, this kind of scheme would be easy to screw up.

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

    HEAD is short for current branch so git push -u origin HEAD works. Now to avoid this typing everytime I use alias:

    git config --global alias.pp 'push -u origin HEAD'

    After this, everytime I want to push branch created via git -b branch I can push it using:

    git pp

    Hope this saves time for someone!

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

    If you enable to push new changes from your new branch first time. And getting below error:

    *git push -f
    fatal: The current branch Coding_Preparation has no upstream branch.
    

    To push the current branch and set the remote as upstream, use

    git push -u origin new_branch_name
    
    
    ** Successful Result:** 
     git push -u origin Coding_Preparation
    Enumerating objects: 5, done.
    Counting objects: 100% (5/5), done.
    Delta compression using up to 4 threads
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 599 bytes | 599.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    remote:
    remote: Create a pull request for 'Coding_Preparation' on GitHub by visiting: ...
     * [new branch]      Coding_Preparation -> Coding_Preparation
    Branch 'Coding_Preparation' set up to track remote branch 'Coding_Preparation' from 'origin'.
    
    0 讨论(0)
  • 2020-11-22 01:27

    You don't, see below

    I find this 'feature' rather annoying since I'm not trying to launch rockets to the moon, just push my damn branch. You probably do too or else you wouldn't be here!

    Here is the fix: if you want it to implicitly push for the current branch regardless of if that branch exists on origin just issue this command once and you will never have to again anywhere:

    git config --global push.default current
    

    So if you make branches like this:

    git checkout -b my-new-branch
    

    and then make some commits and then do a

    git push -u
    

    to get them out to origin (being on that branch) and it will create said branch for you if it doesn't exist.

    Note the -u bit makes sure they are linked if you were to pull later on from said branch. If you have no plans to pull the branch later (or are okay with another one liner if you do) -u is not necessary.

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