Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
In this video, we'll be looking inside the hidden `.git` directory to see how branches work behind the scenes.
A quick warning: we are not supposed to be poking around in the .git
directory! The file and folder structure you see here might change in different versions of Git. And you mustn't change the file contents directly, or you could render your repo unusable! We're just going to peek at the file contents, though, so we'll be OK.
- The
HEAD
file in the.git
directory is used to track which branch we're on.- Let's use a program that's on Mac, Linux, and some Windows computers called
cat
to look at the file's contents. - To view the contents of a file, you just type
cat
followed by the path to the file:cat .git/HEAD
- On Windows, you can use the
type
program instead:type .git/HEAD
- There's not much to the file. It just contains a single line, saying
ref: refs/heads/master
. - Let's try checking out a different branch:
git checkout testing
- If we look at the
HEAD
file's contents again now, we'll see that they've changed:ref: refs/heads/testing
- Let's use a program that's on Mac, Linux, and some Windows computers called
- The
refs/heads/
subdirectory within the.git
directory holds the branches for our repo.- We can list its contents with the
ls
command:ls .git/refs/heads
- On Windows, replace
ls
withdir
:dir .git/refs/heads
- We can list its contents with the
- The
refs/heads/
directory contains one file for each branch in your repo.- Let's look at the file for the
testing
branch:cat .git/refs/heads/testing
- Again, it's just one line, but this time it's a SHA checksum.
- If we run
git log
we'll see that it matches the checksum of the most recent commit on this branch. - If we look at the file for the
add-letters
branch, we'll see the checksum of the most recent commit on that branch:cat .git/refs/heads/add-letters
- Like we said, a branch is just a pointer to a particular commit. We weren't kidding.
- There's no other data files for our branches or anything. Those one-line files in the
refs/heads
directory are the branches.
- Let's look at the file for the
We can also leave branches behind and check out an individual commit.
- You can run
git log
and note the SHA checksum for a particular commit you want to check out. - Then you can run
git checkout
again, but instead of a branch name, give it the SHA for a specific commit:git checkout <partial SHA>
- The output will say "You are in 'detached HEAD' state".
- Now that we're not on a branch, the
.git/HEAD
file contains the specific SHA that's checked out, not a branch name.
We can get back onto a branch by simply checking one out: git checkout testing
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up