Tower
Tower

Reputation: 102945

Is "refs/heads/master" same as "refs/remotes/origin/master" in Git?

The question is simple: is refs/heads/master the same thing as refs/remotes/origin/master? If it is not in some cases, how can I know when it is not and what it is then?

Upvotes: 43

Views: 50434

Answers (3)

Stokely
Stokely

Reputation: 15947

I still felt confused reading the posts here. So, maybe this clarifies things and provides a better answer. The short answer is no these two references you list are not the same. One stores your local repo master reference commit, the other stores your remote repo reference commit. But read on...

First Git documentation says the following:

A ref is a file in which you store a SHA-1 value under a simple name so you can use that simple name rather than the raw SHA-1 value.

A branch in Git is: a simple pointer or reference to the head of a line of work.

So, what are these strange paths in Git?

refs/ - This is simply a folder to references in the Git system that store assigned user-friendly names to hashed SHA-1 branches in your local repo.

refs/heads/ - This is folder of files that store a set of user-friendly assigned branch names that map back to Git commit hash values. These commit values represent branches in your local repo and are the last commit point where you saved some code in each branch. You will often see files inside this folder with the name of each branch in your repo and inside the SHA-1 hash value which connects it to the last commit for that branch. "head" means its a list of HEAD's, or the last commit reference in a branch. So this folder of files would list all names of branches in your project with the hash to the last known commit at the end of the string of commits for the branch.

refs/heads/master - This is actually a file reference to an individual branch file called "master", and holds a hash to the default or master branch reference Git assigns to every Git repo you create. As this "master" file is inside the "head" ref folder, that just means it stores the last known commit for the master branch in your project.

refs/remotes/ - This is simply a list of the remote repositories associated with to your local repository, if any exist. This is a folder of more folders and files.

refs/remotes/origin - This is simply a reference to the default remote reference associated with your local repo. This is a folder of files to your remote repo branches and could have a unique name other than the generic name. "origin" is created as the remote repo name the first time the remote repo is cloned.

refs/remotes/origin/master - This points to a file that contains a hash and indicates the default or "origin" remote repo associated with your project and its "master" or default branch. Master would as above, store the last commit hash inside that master file, indicating on the remote repo the last commit location.

Upvotes: 4

Mark Longair
Mark Longair

Reputation: 468191

The key difference to understand is that the branches under refs/heads/ are branches that, when you have one checked out, you can advance by creating new commits. Those under refs/remotes/, however, are so-called "remote-tracking branches" - these refs just point to the commit that a remote repository was at the last time you did a git fetch <name-of-remote>, or a successful git push to the corresponding branch in that remote repository. (I wrote a blog post that talks about this difference at some length here.)

Upvotes: 38

Ben Jackson
Ben Jackson

Reputation: 93920

They are two different symbolic names that can point to different things. refs/heads/master is a branch in your working copy named master. Frequently that is a tracking branch of refs/remotes/origin/master because origin is the default name for the remote created by git clone and its primary branch is usually also named master.

You can see the difference between them with git rev-list refs/heads/master..refs/remotes/origin/master which will be empty if they are the same and will otherwise list the commits between them.

Upvotes: 46

Related Questions