11
4

4 回答 4

21
  • 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

于 2013-10-11T15:30:19.287 回答
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.

于 2013-10-11T15:25:56.817 回答
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.

于 2013-10-11T15:29:57.883 回答
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.

于 2013-10-11T15:38:54.597 回答