Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Git Branches and Merging!
You have completed Git Branches and Merging!
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
HEADfile in the.gitdirectory is used to track which branch we're on.- Let's use a program that's on Mac, Linux, and some Windows computers called
catto look at the file's contents. - To view the contents of a file, you just type
catfollowed by the path to the file:cat .git/HEAD - On Windows, you can use the
typeprogram 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
HEADfile'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.gitdirectory holds the branches for our repo.- We can list its contents with the
lscommand:ls .git/refs/heads - On Windows, replace
lswithdir: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
testingbranch:cat .git/refs/heads/testing - Again, it's just one line, but this time it's a SHA checksum.
- If we run
git logwe'll see that it matches the checksum of the most recent commit on this branch. - If we look at the file for the
add-lettersbranch, 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/headsdirectory 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 logand note the SHA checksum for a particular commit you want to check out. - Then you can run
git checkoutagain, 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/HEADfile 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