Creating branch off of remote master
I have problem understanding the logic behind the following command in Git:
If I type it then a local branch called hotfix is created that branches off of the master branch of my remote repository named origin. When I later push it to my remote repository the graph looks like this:
Say that a colleague makes changes and pushes them to the remote master branch and at some later point I decide to create another branch off of the remote master one called hotfix_example_2. When I push the new hotfix_example_2 branch to the remote repository I notice that the new branch I created is not branched off of the latest commit of remote master (the one that my colleague pushed earlier) but instead is branched off of the commit before that. If I do a
git pull origin master
on my local master branch and repeat the procedure, I can see that hotfix_example_2 is branched off of my colleague’s commit. What I don’t get is why do I have to do a git pull to get the graph I want even though I use origin/master in my checkout -b command. I am really sorry if it does not make much sense but english is not my native language.
2 Solutions collect form web for “Creating branch off of remote master”
What you explained makes sense, but all I can say is this is how Git is!
To update your ‘remote’ branch on server you need to talk to server atleast once. (Git pull does that).
Unless you did a git-pull your local repo did not know of the branch your friend had created. When you did a git-pull, it updated your local master(origin/master) also.
Hope this makes sense!
Pretty much only
fetch retrieve new commits and update remote remote refs. Other operations only work on your local copy. When you create a branch, it is created off the specified ref, but that ref is looked up in the local repository only. You can see what this ref currently is for each remote branch under
I would presume the goal behind this design is to allow completely disconnected operation. If
checkout -b were to attempt to base the new branch on the remote ref in the remote repository, the remote repository would have to be contacted. By storing the ref locally, the branch can still be created even when completely disconnected.