12

I wanted to add this question as a comment to the answer of @KevinBallard here What's the difference "origin master" vs "origin/master", but my comment was to long.

So my question is: If I am in a branch called topic, is it possible to just write git rebase master instead of git rebase origin/master? Or are there really two different local master branches? One being a copy of the remote master branch and one being my own master branch? If so: When I git pull are both local master branches (one called origin/master and the other just called master) updated? I am very confused …


Or maybe it is like this: origin/master is a local copy of the real remote master branch to which the remote was fetched (copied, i.e. just overwritten), and my local branch called master is only changed, when I git merge origin/master (or git rebase …). That is: When I git pull origin master both my local copy origin/master and master are updated/merged. Of course assuming that I am currently in the master branch (i.e. git checkout master was my last checkout).

4 Answers 4

22
  • master is your local branch.
  • origin master is the branch master on the remote repository named origin.
  • origin/master is your local copy of origin master.

When you do git pull (what I consider as evil, anyone else?), it automatically does :

  • git fetch : it copies origin master into origin/master. (and all other origin xxx into origin/xxx).
  • git merge : it merges origin/master into master.

When you want to rebase master, you must do :

  • git fetch
  • git rebase origin/master

extract of git help pull:

More precisely, git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch

7
  • and all other origin xxx into origin/xxx: Ah, really? That is interesting. It fetches everything but only merges the master automatically. Thank you.
    – erik
    Oct 11, 2013 at 15:49
  • git merge: it merges origin/master into master, but only if I am currntly in the branch master, isn’t it?
    – erik
    Oct 11, 2013 at 15:55
  • @erik I added an extract of the man page which explains that without args, you call git fetch (which fetches all branches) then does the merge in the current branch. Oct 11, 2013 at 16:04
  • @erik Personally, I never use git pull. I always use git fetch to fetch all the branches, then I rebase/merge with git merge origin/xxx or git rebase origin/xxx/ Oct 11, 2013 at 16:07
  • 1
    FWIW git pull --rebase Nov 9, 2015 at 19:32
6

<remote>/<branch> named branch are managed automatically by git.

When you do git pull, what git really does is

git fetch  
git merge origin/master

git fetch automatically updates the origin/master local branch to point at the last commit of the origin remote's master branch.

So yes, when you call git pull, both are updated. That's because fetch updates origin/master and merge updates master.

If I am in a branch called topic, is it possible to just write git rebase master instead of git rebase origin/master?

You can, but master is not necessarily the same as origin/master - though most of the time they are. So it is really up to you.

Or are there really two different local master branches?

Yes they are two different local branches. They're just usually pointing to the same commits and share a common tree.

1

When you do git pull on any branch, actually, two operations are performed: git fetch and git [merge|rebase].

The fetching just downloads all objects from remote repo, that is by default called origin. Among downloaded objects there are refs - they are pointers to some concrete commits. Some of those pointers are called branches. If there is master branch in a remote repo origin, then it will saved as origin/master inside your local repo - just to not interfere with your local branch with the same name.

After fetching, git looks at your config and, by default, tries to merge or rebase your current branch onto the branch from origin with the same name.

In config you can specify which local branch will be rebased upon which remote branch - so you can simply run git pull without additional params.

1
  • what do you mean by the same name? Do you mean, that if I am in a branch called topic1 and I pulled just via git pull in effect it does git pull origin topic1 (if origin is my default remote)? And git pull origin topic2 pulls the branch topic2 into my current branch (no matter how my current branch is called)?
    – erik
    Oct 11, 2013 at 15:44
1

Branches are just references to commit points. Tags are also refs to commit points, but branches differ from tags because git, in certain situations, updates branch references automatically to point to another commit. These automatic updates happen, let say, when you create new commit point (git commit), then the branch that is current HEAD gets updated to refer to the newly created commit point.

Git maintains two types of branches: local and remote. Local branch gets updated as described above. Remote branches are updated when you do: git fetch.

Also, you can have local branch to track remote branch, in this case git pull is just a convenience for the following two operations: git fetch; git merge origin/<tracked branch>.

Note that the local branch and the remote branch that tracks can have different names.

So, in your case, when you say git merge master, you are merging your local master. when you say git merge origin/master, you are merging the remote branch (that, eventually, can point to the same commit as local master)

Also note that you really merge commit point that the branch point to (you can say git merge <some commit>), not branch itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.