HEAD
HEAD
HEAD points to the current branch, in other words, the commit that was checked out into the working directory.
We're already talked about branches in the previous chapter.
"The active branch (or HEAD) helps determine which files are checked out in your working directory. It reflects the state of a file currently in development."
The HEAD file has always just a single line with the name of the current branch. It is located in the ./git/HEAD path. There are two ways to see the content. You can cat the HEAD file or use git symbolic-ref HEAD command like this
if any time you receive the following output, it means you're in the git detached HEAD state
$ git symbolic-ref HEAD
fatal: ref HEAD is not a symbolic ref
Git detached state
The detached HEAD state occurs when the HEAD does not point to a branch, but instead points to a specific commit or the remote repository. This occurs when you perform git init --initial-branch=<branch-name> when you create a new repository) or when you perform git rebase <branch_name> but end up with merge conflicts, Git can put you in a git detached HEAD state. if you got to this state accidentally, just switch or check out to any of your existing branches on your local git repository. See here
Use the git detached state to time travel to a specific commit or make change experiments. Being in a git detached HEAD state allows you to explore any commit in your commit history.
In any other case, do not work in a detached HEAD state, unless you have a really significative reason. Working in a detached state carries the potential risk of losing revisions.
Change the current branch
In the previous chapter, we saw how to change the current branch with the command git checkout <branch-name>. Now, we're going to see what's happen internally. First, let's create a new repository.
git init .
Thereafter, we list all branches
git branch --all list all branches
git branch --all
ls .git/refs/heads
There are no branches in the repository.
Now I ask you, Why are there no branches in the repository?
The answer is simple. Because there are no commits!
A branch is just a wrapper for a specific commits.
So, let's create our first commit.
echo "file 1" > file1.txt
# Creating the first file for the first commit
git add .
git commit -m "First Commit"
The commit will create a new branch. Let's repeat the same step before.
As you can see, when we commit the file, the new main branch was created in the folder ./git/refs/heads
Now let's create a new branch to build a new feature, possibly using the same ticket ID from another tool (or service desk for bug fixing).
git branch feature-1058
git branch
The new branch `feature-1058` was created sucessfully!
One interesting thing is that the current branch `feature-1058` was created with the same SHA1 hash of the main branch.
```zsh
# same content
cat .git/refs/heads/main
cat .git/refs/heads/feature-1058
Remember in reason of your local time, e-mail and name there could be differences between mine and your SHA1 generated. The important thing here to note is that both branches have the same SHA1
Let's now change to the feature-1058 branch
git chekout -b <branch-name>git checkout feature-1058
git branch
git symbolic-ref HEAD
As you can see, the new branch now is feature-1058.
If you have a new feature or a bug fix, this is the moment that you should start developing the new functionality.
When you finish, you should merge the resul, we will see more about merge in the following chapter
For now, let's just return to the main branch
```zsh
# return to main branch
git checkout main
Script
git init .
git branch --all
ls .git/refs/heads
# There's no branches because there isn't any commit yet
echo "file 1" > file1.txt
# Creating the first file for the first commit
git add .
git commit -m "First Commit"
# The commit will create the new main branch
git branch feature-1058
ls .git/refs/heads
# same content
cat .git/refs/heads/main
cat .git/refs/heads/feature-1058
# changing the current branch feature-1058
git checkout feature-1058
# See where's HEAD is pointing
git symbolic-ref HEAD
cat ./.git/HEAD
# return to main branch
git checkout main