
How to Work with Branches in Git — Under the Hood
How to Work with Branches in Git — Under the Hood 관련
Just as we’ve created a repository and a commit without using git init
, git add
or git commit
, now we will create and switch between branches without using porcelain commands (git branch
or git checkout
).
It’s perfectly understandable if you are excited, I am too 🙂
Let’s start:
So far we only have one branch, named master
. To create another one with the name test
(as the equivalent of git branch test
), we would need to simply create a file named test
within .git\refs\heads
, and the contents of that file would be the same commit’s hash as the master
points to.

If we use git log
, we can see that this is indeed the case

master
and test
point to this commitLet’s also switch to our newly created branch (the equivalent of git checkout test
). For that, we should change HEAD
to point to our new branch:

test
by changing HEAD
As we can see, both git status
and git log
confirm that HEAD
now points to test
, which is, therefore, the active branch.

Using the commands above, we have created a file named test.txt
, with the content of Testing
, created a corresponding blob, and added it to the index. We also created a tree representing the index.
It’s now time to create a commit referencing this tree. This time, we should also specify the parent of this commit — which would be the previous commit. We specify the parent using the -p
switch of git commit-tree
:


Will git log
show us the new commit?

As we can see, git log
doesn’t show anything new. Why is that? 🤔 Remember that git log
traces the branches to find relevant commits to show. It shows us now test
and the commit it points to, and it also shows master
which points to the same commit.
That’s right — we need to change test
to point to our new commit. We can do that by simply changing the contents of .git\refs\heads\test
:

It worked! 🎉🥂
git log
goes to HEAD
, which tells it to go to the branch test
, which points to commit 465...5e
, which links back to its parent commit 80e...8f
.
Feel free to admire the beauty, we git you. 😊