How the Git Working Tree Works by Example |Video upload date:  · Duration: PT6M37S  · Language: EN

Practical guide to how the Git working tree index and repository interact using clear example commands and explanations

Start with a clean sandbox

If you want to understand how changes travel from your messy laptop to sacred commit history you need three players in the drama. The working tree is your filesystem where you edit files. The index is the staging area that holds the snapshot you plan to commit. The repository stores committed snapshots and the HEAD pointer tells you which one is current. We will use tiny examples and actual git commands so you can stop guessing and start winning.

Boot the repo and peek around

Run git init then git status to prove the repo starts empty and lonely. Create a file named hello.txt and add some text. Run git status again and you will see the working tree has an untracked file. That means the file exists on disk but Git has not been told to care about it yet.

Commands to remember

  • git init to create a repo
  • git status to inspect the working tree
  • git add hello.txt to stage a file

Stage it like a pro

Use git add hello.txt then try git status --short or git ls-files --stage to inspect the index. Think of the index as a photo studio where files get their glamour shots before going public. The index records a snapshot that will become the next commit.

Make changes and compare states

Edit hello.txt again and use git diff to compare the working tree with the index. To see what you have staged versus the last commit use git diff --staged. These two diffs are your best friends when you want to avoid accidentally shipping half baked work.

Commit and inspect history

When the index looks right run git commit -m 'add hello'. Then run git log --oneline to view the repository history. The repository now stores the committed snapshot and HEAD moves with each commit. That is the life cycle from working tree to index to repository.

Quick checklist

  • Make edits in the working tree
  • Stage the snapshot in the index with git add
  • Commit to the repository with git commit

Tip Use git status --porcelain for machine friendly output and git restore --staged to unstage files if you change your mind. No drama required. Now go break something and then fix it like a champ.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.