rich22 rich22 - 4 months ago 15
Git Question

How can origin be a pointer and branch at the same time?

I am currently learning about Git, and have found many, in my opinion, conflicting definitions of what origin are.

My current understanding of origin based on my tutorial, is that every time a file is fetched from the local server, the origin "pointer" moves to the latest commit downloaded from there, and thus I consider it a pointer.

The problem is I've also heard the origin referred to as a branch (Git branching: master vs. origin/master vs. remotes/origin/master) . My question is how can it be considered a "pointer" and branch at the same time, when pointers "points" to an individual commit and branch refers to a change in direction of a branch that allows you to make alterations separate from the current branch, two completely different things.

A branch:

master --> a
\
\
branch1 --> b


A pointer:

master --> a --> b --> c
--> C is the HEAD "pointer" and capable of moving if point D created

Answer

There is nothing in git called an "origin". The label "origin" is simply the name given by default to the automatically configured "remote". Neither is there anything in git called a "pointer", although there are "references".

A remote is a named git repository other than the one you are working in. It may be another directory on the same computer or it may be located on a computer somewhere else in the world. Git knows what branches exist in the remote repository (you can see these when you run git branch -a, which shows both local and remote branches). You refer to remote branches as "remotename/branchname", so if you have a remote named origin and a branch named master, you can check that out by running:

git checkout origin/master

If you simply were to run:

git checkout master

Then (assuming it didn't already exist) git will create a local branch named master and configure to it track origin/master. "Tracking" means that you type git pull, git knows that you want to update origin/master from the remote repository and then merge those changes into your local branch. Similarly, when you type git push, git knows you want to send your changes to the master branch of the remote repository.

A branch is a type of reference that refers to a series of changes. When you make a new commit on a branch, the branch reference advances to the new commit. So if you started with two commits on branch master:

       master
      /
A -> B 

And you make a new commit on that branch, you get:

            master
           /
A -> B -> C

Note that in these examples, the branch name refers to the most recent commit.

A tag is another kind of reference. Unlike a branch, a tag changes only when you explicitly update it. So if you had a tag working_version pointing at commit B in the previous example, it would continue to point at commit B even after you made a new commit.

You can find details on the above and more in The Git Book.