0
0
Gitdevops~5 mins

How branches are just files with hashes in Git - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
Branches in Git are simple files that store the hash of the latest commit. This means a branch is just a pointer to a specific commit in the project history.
When you want to understand how Git tracks changes behind the scenes
When you need to debug branch-related issues by looking at Git internals
When you want to manually inspect or manipulate branch pointers
When you want to explain Git branching to someone new using simple concepts
When you want to see how Git stores branch information in the .git directory
Commands
Create a new Git repository named example-repo to start tracking files and commits.
Terminal
git init example-repo
Expected OutputExpected
Initialized empty Git repository in /path/to/example-repo/.git/
Change directory into the new repository to run Git commands inside it.
Terminal
cd example-repo
Expected OutputExpected
No output (command runs silently)
Create a new file named file.txt with the content 'Hello'.
Terminal
echo "Hello" > file.txt
Expected OutputExpected
No output (command runs silently)
Stage the new file so Git knows to include it in the next commit.
Terminal
git add file.txt
Expected OutputExpected
No output (command runs silently)
Create the first commit with the staged file and a message describing the change.
Terminal
git commit -m "Add file.txt with Hello"
Expected OutputExpected
[master (root-commit) abcdef1] Add file.txt with Hello 1 file changed, 1 insertion(+) create mode 100644 file.txt
-m - Add a commit message inline
Show the content of the master branch file, which is the hash of the latest commit it points to.
Terminal
cat .git/refs/heads/master
Expected OutputExpected
abcdef1234567890abcdef1234567890abcdef12
Display the commit history in a short format to see the commit hash and message.
Terminal
git log --oneline
Expected OutputExpected
abcdef1 Add file.txt with Hello
--oneline - Show each commit in one line with short hash and message
Key Concept

If you remember nothing else from this pattern, remember: a Git branch is just a file storing the hash of the latest commit it points to.

Common Mistakes
Trying to edit branch files directly without understanding the hash format
Editing branch files incorrectly can corrupt the branch pointer and cause Git errors
Use Git commands like git branch or git reset to safely move branch pointers
Expecting branch files to contain commit messages or file contents
Branch files only store commit hashes, not the commit details or file data
Use git log or git show to view commit details instead
Summary
Initialize a Git repository and create a commit to have a branch pointer.
The branch is stored as a file in .git/refs/heads/ with the commit hash inside.
Use git log and cat on the branch file to see how the branch points to the latest commit.