Why do I need to explicitly push a new branch?

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

提交回复
热议问题