Git Tutorial 7: What is HEAD?

Sharing buttons:

dear friends in this tutorial we are

going to learn what is head in gate head

is a reference to the most recent commit

in the current branch in most of the

cases the reason I said most of the

cases is because there are some cases

where this is not the case and we will

cover them later on I have this gate

repository where I have a bunch of

commits which I can list using git log

ok so I have these three commits now

head is nothing but a reference to this

commit I can verify by running this

command called git show head this will

show me the head commit this is same as

doing good show

this particular commit

okay so they produce the same output so

I can say head is just a short form of

writing this committee okay now let me

make one more commit and see what

happens to get so I'm going to add one

more line here and I will commit that

change so I read this and I will now

commit this it can make minus M murder

okay when I do that and if I run git log

I see this commit and when I do get show

head it will now show me barber commit

so as you can see in the diagram the

head pointer keeps on moving and it

always not always but in majority of the

cases it refers to the most recent

commit okay now let me show you another

another case where her head could be

useful and that is to do the death now

we have been running this get diff to

command in our previous tutorial so this

command is useful to show the difference

between two comments okay

so I'm going to run give give lift to

Khan to commit so for example from this

commit to this commit this Pisa line was

added okay and I want to see the

difference between the snapshot of this

and this so the way you do it is by

running git diff to the older commit

which is this so I'm just going to copy

paste that commit and the newer commit

which is this so again I'll be copying

and pasting when I do that or the MELD

diff tool will open and it will show me

the difference from left and right you

see this Pisa line was added between

those two commits so this is my old code

this is my new code okay now the same

thing you can do by using a head so I

can say head

now remember a head is here

so this commit will be head tilde 1 and

this comment will be head tilde - so you

always put the older commits first so I

will do head tilde - and he'll tilde 1

you can just say like head - - you know

this is 1 and this is this is head this

is head - 1 head - 2 so head - - and

head - 1 ok and when you do that you

will get the same output you'll see that

in this particular commit Pisa line was

added ok so this command is just a short

form of running this but the benefit

here is that you don't have to remember

the commit or copy/paste this long

commit ID you can just use this short

form so head at ta D it refers to this

head my head tilde 1 refers to this head

Taylor 2 refers to this particular

commit ie

ok now let's see how good internally

manages this head pointer if you have

your code repository open and if you

have if you are showing the hidden repos

hidden directories then there is always

a dot get directory in your current code

directory if this is not visible you can

go to Windows Explorer option and choose

the option to show hidden files ok but

once you have dot get directory open use

a bunch of files here and head is one of

the files now when I open this head file

I will say this so it is referring to a

master branch now if I have two branches

for example I

when I do get plans I have second branch

called thirsty now when I check out the

second branch my head pointer will

actually move to thirsty so see now it

updated this file and now I have thirsty

in my head ok now let me discuss the

scenario where head doesn't point to the

most recent commit that scenario is

called detached head so sometimes when

you do get log you want to go to this

previous comment ok and based on that

previous commit you want to make some

changes so you want to make this

particular commit your base and then

want to add further changes because you

want to ignore this in that case you

will do get check out this commit ok

let's see what happens when we run this

command ok it is showing me this message

that you are in detached head state

meaning head is not pointing to the most

recent commit it is add some older

commit ok and you can see that reflected

here as well usually it's a branch name

but now it is this particular thing ok

so now your head is in a detached state

and when I open my head file head is

having this commit ID for BC etc and

that was this commit ID for BC etc so

you can see how it manages all of these

things by using a simple text file ok

all right so now I want to come out of

the detached head state in that case you

can do get check out master and now you

are at master the PISA is the most

recent commit and when you do that you

will come out of the detached head state

all right okay so that was all about

head thank you for watching this video

we'll see you guys in the next tutorial