What are the git concepts of HEAD, master, origin?
Asked Answered
S

2

258

As I'm learning about git, I keep coming across the terms HEAD, master, origin, and I'm not sure what the differences are. If I understand correctly, HEAD is always equal to the latest revision? And if so, is that the latest revision of the whole repository, or of a specific branch or tag? This is so confusing. I've read so many tutorials on this and things like branching/merging, but still can't wrap my head around it.

Stash answered 19/11, 2011 at 19:11 Comment(1)
“This is so confusing… but still can't wrap my head around it.” I see what you did there… 😉Lafferty
P
301

I highly recommend the book "Pro Git" by Scott Chacon. Take time and really read it, while exploring an actual git repo as you do.

HEAD: the current commit your repo is on. Most of the time HEAD points to the latest commit in your current branch, but that doesn't have to be the case. HEAD really just means "what is my repo currently pointing at".

In the event that the commit HEAD refers to is not the tip of any branch, this is called a "detached head".

master: the name of the default branch that git creates for you when first creating a repo. In most cases, "master" means "the main branch". Most shops have everyone pushing to master, and master is considered the definitive view of the repo. But it's also common for release branches to be made off of master for releasing. Your local repo has its own master branch, that almost always follows the master of a remote repo.

origin: the default name that git gives to your main remote repo. Your box has its own repo, and you most likely push out to some remote repo that you and all your coworkers push to. That remote repo is almost always called origin, but it doesn't have to be.

HEAD is an official notion in git. HEAD always has a well-defined meaning. master and origin are common names usually used in git, but they don't have to be.

Protestantism answered 19/11, 2011 at 19:18 Comment(7)
What do you mean by "HEAD really just means "what is my repo currently pointing at"." If there are multiple branch in a repository, which one is the HEAD according to you? if there are 3 branches(along with master), and a need commit was made in branchA, the HEAD can still be in the commit of the 'master'. So, which one is the revision that the repo is pointing now?Davison
unclear what you mean by repo (local or remote). In general your answer uses expressions, without explaining them before.Salesgirl
I just did a clone in a parallel directory in order to compare my working directory to the tip of the remote repo. This is how good all git answers are.Salesgirl
@JingHe You read the whole book? Is it really worth it? I mean we are talking about a freaking 500+ pages read, that seems a little too much to me...Drupelet
To verify that I understand the logic, I can call a branch in remote as featureA and when I push to that branch, I type "git push origin featureA". I can pull the same from the remote as well by saying "git pull origin featureA", make changes. And head is only referring to local copy. @Davison I believe HEAD is pointing to whichever branch you are checked out at the moment in the local. If the commit was made in branchA but currently you are on branchB, the HEAD is pointing to branchB. You have to move the HEAD to branchA back before you do a particular commit.Photosensitive
@Max, I think the most important thing to read would be Chapter 10: Git Internals. Having an understanding of how it all works under the hood was most helpful to me on how it all fits together, instead of just syntax of various commands.Chevron
@Chevron Thanks for the suggestion. I'll probably read into it. I hope it's gonna be understandable without having read the 9 chapters before that.Drupelet
R
62

HEAD is not the latest revision, it's the current revision. Usually, it's the latest revision of the current branch, but it doesn't have to be.

master is a name commonly given to the main branch, but it could be called anything else (or there could be no main branch).

origin is a name commonly given to the main remote. remote is another repository that you can pull from and push to. Usually it's on some server, like github.

Roulers answered 19/11, 2011 at 19:20 Comment(2)
Can you give an example of when HEAD is current but not latest? I've never seen that before. Or do you mean it might not be the latest because your branch is behind the remote branch it's tracking?Protestantism
@MattGreer: If you check out something older (such as a tag like git checkout v1.1) then your HEAD changes to the commit of that tag. It may not be the latest commit.Ojeda

© 2022 - 2024 — McMap. All rights reserved.