209

What is the difference between the HEAD and master in Git?

I did a clone of a project on GitHub and want to push my changes to the remote. But to which one should I push?

screenshot

Lee Netherton
  • 21,347
  • 12
  • 68
  • 102
Frederik Heyninck
  • 3,493
  • 4
  • 20
  • 19

3 Answers3

180

master is a reference to the end of a branch. By convention this is usually the main integration branch, but it doesn't have to be. (As of git v2.28.0, released in July 2020, the default name changed from master to main)

HEAD is actually a special type of reference that points to another reference. It may point to master/main or it may not (it will point to whichever branch is currently checked out). If you know you want to be committing to the master/main branch then push to this.

Here is a visual example:

alt text

On your own repository you can check where the HEAD is pointing to by running this:

$ git symbolic-ref HEAD
refs/heads/master

However, finding out where the remotes/origin/HEAD is pointing to is more tricky because it is on the remote machine.

There is a great little tutorial on git references here

Lee Netherton
  • 21,347
  • 12
  • 68
  • 102
  • 1
    +1 More precise answer than mine. See also http://stackoverflow.com/questions/3301956/git-correct-way-to-change-active-branch-in-a-bare-repository/3302018#3302018 and http://stackoverflow.com/questions/3301956/git-correct-way-to-change-active-branch-in-a-bare-repository for illustrations around those concepts. – VonC Dec 08 '10 at 13:31
  • Note: `HEAD` doesn't necessarily refer to a branch; the "dreaded" detached head occurs when `HEAD` refers to a commit that is *not* a branch head. – chepner Aug 09 '22 at 15:08
  • https://about.gitlab.com/blog/2021/03/10/new-git-default-branch-name/ It is now `main` not `master`. Too many pending edits to change the text. – bvargo Mar 13 '23 at 18:13
53

The simple answer is that HEAD is a pointer/label to the most recent commit of the branch you are currently on. master is the default branch created when you initialized a git repository (e.g. git init).

You can delete the master branch (e.g. git branch -D master). You cannot delete the HEAD pointer.

benhorgen
  • 1,928
  • 1
  • 33
  • 38
  • 11
    "`HEAD` is a pointer/label to the most recent commit of the branch you are currently on." I think this is misleading at best. If you checkout an older commit, then HEAD is now a pointer to that older commit, not to the most recent commit. Right? – LarsH Mar 17 '16 at 16:36
  • 3
    You are correct. HEAD is your latest checkout. But in my defense, for Git, the `checkout` command is the equivalent to switching branches in other common SCM systems. – benhorgen Apr 01 '16 at 19:38
  • 1
    I sympathize... I could easily have made the same mistake. The only reason I noticed was because I'm at the stage of trying to research what HEAD really means. Any chance you could edit your answer to be correct? I find HEAD to be a difficult concept to find accurate descriptions of, for git non-experts like myself. And having advice sitting out there on the web that gives *incorrect* information about HEAD makes it quite a bit harder. – LarsH Apr 01 '16 at 20:58
  • 2
    I think your comment is a fantastic clarification for anyone seeking a finer understanding of what the Git `HEAD` pointer really is. I appreciate your comment and think others will too. My original post's content along with your followup comment complement each other. Thanks. – benhorgen Apr 07 '16 at 01:53
  • 6
    A technicality, but if you checkout an older commit, you are no longer 'on' a branch. If you checkout a commit instead of a branch, you have what is called a "detached HEAD", you are no longer 'on a branch'. Being 'on a branch' means your HEAD is referencing a branch, and by definition you are on the most recent commit of that branch. Just because you have commit 'b54fe7' checked out, and master points to that commit, doesn't mean you're on the master branch. There could be several branches pointing to the same commit, you're 'on' the one that HEAD is pointing to, if any. – Jason Goemaat Nov 19 '16 at 06:05
  • "master" doesn't refer to the default branch name in this context. It's a reference. – DarkWingDuck Jun 23 '18 at 01:03
8

Simply push the changes of your current branch

git push origin

and it will push your branch 'B' changes to 'origin/B'.
If you are on your master branch, git will push to origin/master.
Actually it will push all changes on the local branches that have matching remote branches at origin. It is controlled by the config setting push.default.
See also pushing RefSpecs in the Pro Git book.


What you are seeing is the sidebar representing all the refspecs of your remote repo in the Experimental GitX fork of the GitX project.

alt text

The HEAD will designate the default branch for that remote.
See git remote set-head man page:

Having a default branch for a remote is not required, but allows the name of the remote to be specified in lieu of a specific branch.
For example, if the default branch for origin is set to master, then origin may be specified wherever you would normally specify origin/master.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250