ตัวอย่างบทเรียน

First Repository: init, add, commit, and the Magic of the Index

We'll create our first repository and immediately encounter a concept that many struggle with for years - the staging area (index). We'll explore why this 'intermediate layer' between the working directory and history is essential, and through examples, we'll see how the two-step add → commit system results in meaningful, deliberate commits rather than random ones.

repository staging area commit
คุณจะเรียนรู้อะไรในบทเรียนนี้
  • You'll understand why the staging area exists and how it helps create atomic, meaningful commits.
  • You'll be able to create a repository, add files to the index, and make your first commit with a proper message.
  • You'll learn how to stage only specific parts of a file using git add -p.

จุดเริ่มต้นของบทเรียน

Creating Your First Repository

  • git init creates a hidden .git folder
  • .git stores the entire change history
  • You can create a repository in any directory
  • Existing files become untracked
  • Analogy: Starting a new journal in a fresh notebook

Think of your project as a book and the repository as its draft, where every edit is preserved. When you run git init, a hidden .git directory appears in your folder - this is your repository. Inside, Git will store all your change history, metadata, and settings. It's important to understand that you can create a repository at any time, even if the folder already contains files. They'll simply become untracked, and you can add them later. It's like starting a new journal - you have your notebook ready, but you haven't yet decided which entries will make the final cut.

Under the Hood of .git

  • objects: compressed file data and commits
  • refs: pointers to branches and tags like bookmarks
  • HEAD: current position in history
  • config: repository settings
  • hooks: automation scripts

Let's take a peek inside the .git folder. This is where Git's magic happens. The objects directory stores all your files and commits in compressed form. The refs folder contains pointers to branches and tags - much like bookmarks in a book. The HEAD file shows your current position in the history. And config stores repository-specific settings. There are also hooks - scripts that run automatically during certain events. Git creates all of this automatically during initialization. While you won't need to look here often, understanding this structure will help you navigate more complex situations.

Hands-on: Creating a Repository

  • Open terminal in your project folder
  • Run git init
  • Verify .git creation with ls -la
  • Windows: use dir /ah
  • Done! Your folder is now version-controlled

Open your terminal in the project directory. Type git init and press Enter. You'll see a message confirming that an empty Git repository has been initialized. To verify everything worked correctly, run the command ls -la — you should see a hidden .git folder. If you're using Windows, use the command dir /ah. Congratulations, your folder is now under version control! Git will be able to track all the changes you make from now on. Now that we've created the repository, it's time to understand how Git classifies files — this is key to understanding how it works.

# Create a project folder and navigate into it
mkdir my_first_repo
cd my_first_repo

# Initialize the repository
git init

# Verify .git creation
ls -la | grep .git

บทเรียนฉบับเต็มใช้ได้เมื่อสมัครสมาชิก

ฉบับเต็มมีสไลด์เพิ่มอีก 20 สไลด์

วิดีโอ สไลด์ทั้งหมด โน้ต แบบทดสอบ AI ติวเตอร์ และการสัมภาษณ์จะเปิดหลังเข้าสู่ระบบและสมัครสมาชิก

เข้าสู่ระบบหรือลงทะเบียน สมัครสมาชิก

เนื้อหาบทเรียน

  1. Creating Your First Repository
  2. Under the Hood of .git
  3. Hands-on: Creating a Repository
  4. The Three File States: Why It Matters
  5. Working Directory: Where the Magic Happens
  6. The Index: Staging Changes in Meaningful Chunks
  7. Commit: Preserving History Forever
  8. The Git Index: A Staging Area for Commits
  9. Adding a Specific File to the Staging Area
  10. Adding All Modified Files
  11. What is a Git Commit?
  12. Creating Your First Commit
  13. How to Write Good Commit Messages
  14. The Problem of Mixed Changes
  15. Git add -p in Action
  16. How to Read Patches
  17. Why Commit Without Add Doesn't Work
  18. One Task, One Commit
  19. Commit Messages That Make Sense a Year Later
  20. Creating Your First Repository
  21. The Staging Area: Between Code and Commit
  22. Crafting a Meaningful Commit
  23. Your First Commit: What's Next?

บทความเกี่ยวกับบทเรียนนี้

บทเรียนทั้งหมดในคอร์สนี้

  1. Introduction: Git from Scratch to Confident Developer
  2. Why Git is Essential - And Why It's a Job Requirement
  3. Installation and Setup: Getting Git Ready for First Use
  4. First Repository: init, add, commit, and the Magic of the Index
  5. Reading History: log, diff, show and Navigating the Past
  6. Branches: The Parallel Reality of Your Code
  7. Undoing Mistakes: restore, reset, revert — Doing It Right and the Dangers
  8. GitHub and Remote Repositories: Your Code in the Cloud
  9. Team Collaboration: Forks, Feature Branches, and Pull Requests
  10. Merge Conflicts: Don't Panic - Understanding and Resolving Them
  11. Git Workflows in Real Teams: Choosing the Right Approach
  12. Advanced Tools: Rebase, Stash, Cherry-pick and Tags
  13. Final Project: Simulating Real Team Development