Git 103: diff, add, commit, logGit 103: diff, add, commit, log
เรียน loop ที่ใช้บ่อยที่สุดใน Git: ตรวจ change ด้วย diff, เลือกด้วย add, save ด้วย commit และอ่าน history ด้วย logLearn the everyday Git loop: inspect changes with diff, stage with add, save with commit, and read history with log.
A commit should not be a blind save.
Before committing, inspect what changed.
See the exact change
Edit README.md:
# Git 10x Practice
This repository is for learning safe Git habits.
Then run:
git status
git diff
git diff shows unstaged changes.
You will see removed lines with - and added lines with +.
Read the diff before asking Git to save anything.
Stage the files
Staging means choosing what belongs in the next commit.
git add README.md .gitignore
Then check again:
git status
The files should now be staged.
If you want to inspect staged changes:
git diff --staged
Make the first commit
Commit with a clear message:
git commit -m "docs: add practice README"
A useful commit message says what changed and why it matters.
Weak:
update
Better:
docs: add setup notes for Git practice repo
The better message is searchable and reviewable.
Read history
Run:
git log --oneline
You should see your commit with a short hash and message.
That hash is a stable pointer to a saved point in history.
The safest daily loop
Use this loop whenever you or an AI assistant changes files:
git status
git diff
git add <files>
git diff --staged
git commit -m "type: message"
git log --oneline -5
The important part is the inspection before commit.
Do not use git add . blindly
git add . stages many changes at once.
It is useful when you already inspected the whole diff. It is risky when you have not.
Before git add ., ask:
- did I check
git status? - did I read
git diff? - are there generated files?
- are there secrets?
- are there unrelated changes from another task?
When in doubt, add specific files.
Practice: write one commit carefully
Add this section to README.md:
## Commands I know
- git status
- git diff
- git add
- git commit
Then run:
git status
git diff
git add README.md
git diff --staged
git commit -m "docs: list first Git commands"
git log --oneline -3
Checkpoint:
git statusshould be clean after the commit.git log --onelineshould show two commits.
commit ไม่ควรเป็นการ save แบบหลับตา
ก่อน commit ต้องตรวจว่าอะไรเปลี่ยน
ดู change แบบละเอียด
แก้ README.md:
# Git 10x Practice
This repository is for learning safe Git habits.
จากนั้นรัน:
git status
git diff
git diff แสดง change ที่ยังไม่ได้ stage
บรรทัดที่ถูกลบจะมี - และบรรทัดที่เพิ่มจะมี +
อ่าน diff ก่อนสั่งให้ Git save อะไรทั้งนั้น
Stage ไฟล์
staging คือการเลือกว่าสิ่งไหนควรเข้า commit ถัดไป
git add README.md .gitignore
จากนั้นเช็กอีกครั้ง:
git status
ไฟล์ควรอยู่ใน staged แล้ว
ถ้าอยากตรวจ staged changes:
git diff --staged
สร้าง commit แรก
commit พร้อม message ที่ชัด:
git commit -m "docs: add practice README"
commit message ที่ดีบอกว่าเปลี่ยนอะไรและสำคัญยังไง
อ่อน:
update
ดีขึ้น:
docs: add setup notes for Git practice repo
ข้อความที่ดี search ได้และ review ได้
อ่าน history
รัน:
git log --oneline
คุณควรเห็น commit พร้อม hash สั้นๆ และ message
hash นั้นคือ pointer ไปยังจุด save หนึ่งใน history
Loop รายวันที่ปลอดภัย
ใช้ loop นี้ทุกครั้งที่คุณหรือ AI assistant แก้ไฟล์:
git status
git diff
git add <files>
git diff --staged
git commit -m "type: message"
git log --oneline -5
จุดสำคัญคือการตรวจก่อน commit
อย่าใช้ git add . แบบไม่ดู
git add . stage หลาย change พร้อมกัน
มันมีประโยชน์เมื่อคุณตรวจ diff ทั้งหมดแล้ว แต่มันเสี่ยงถ้ายังไม่ตรวจ
ก่อนใช้ git add . ให้ถาม:
- เช็ก
git statusแล้วหรือยัง? - อ่าน
git diffแล้วหรือยัง? - มีไฟล์ generated ไหม?
- มี secret ไหม?
- มี unrelated change จากงานอื่นไหม?
ถ้าไม่แน่ใจ ให้ add เป็นไฟล์เฉพาะ
แบบฝึก: commit อย่างระวังหนึ่งครั้ง
เพิ่ม section นี้ใน README.md:
## Commands I know
- git status
- git diff
- git add
- git commit
จากนั้นรัน:
git status
git diff
git add README.md
git diff --staged
git commit -m "docs: list first Git commands"
git log --oneline -3
จุดเช็ก:
git statusควร clean หลัง commitgit log --onelineควรเห็นสอง commit
เข้าสู่ระบบเพื่อบันทึกความคืบหน้าSign in to track your progress
เข้าสู่ระบบSign in→