การรีเบสแบบโต้ตอบและการแก้ไขคอมมิต
เรียนรู้การเขียนประวัติใหม่ด้วย `git rebase -i` เพื่อให้คอมมิตสะอาดขึ้น และใช้ `git commit --amend` เพื่อแก้ไขคอมมิตล่าสุด
การรีเบสแบบโต้ตอบและการแก้ไขคอมมิต เป็นบทเรียน Git Advanced: Monorepo, Submodules & Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Git Advanced: Monorepo, Submodules & Workflows และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Clean Up Your Git History
Git history gets messy — a forgotten file, a typo'd message, ten tiny commits that should be one. Git lets you rewrite history to clean it up.
Amending Your Last Commit
git commit --amend fixes your most recent commit: change the message, add forgotten files, or tweak content. It replaces the last commit with a tidier one.
Amending a Commit Example
Forgot a file right after committing? Stage it, then git commit --amend folds it into the last commit. Here's the flow.
# 1. Make some changes and commit
echo "Initial content" > file1.txt
git add file1.txt
git commit -m "Add file1"
# 2. Realize you forgot file2.txt
echo "More content" > file2.txt
git add file2.txt
# 3. Amend the last commit to include file2.txt
git commit --amend --no-editIntroducing Interactive Rebase
Need to fix commits further back, not just the last one? Interactive rebase (git rebase -i) is a full editor for a series of commits.
Getting Started with Rebase -i
Start an interactive rebase by naming a commit before your target range. To edit the last three commits, use HEAD~3.
git rebase -i HEAD~3Rebase Commands: Pick, Reword, Squash
The rebase editor lists commits with commands you can swap: pick keeps it, reword edits the message, squash/fixup merge it up, edit pauses to amend.
Rewording a Commit Message
To change an older commit's message, switch its line from pick to reword in the editor. Git then prompts you for the new message.
# Original rebase editor content:
# pick 8a9b3c4 Add initial feature
# pick d1e2f34 Fix typo in docs
# Change 'pick' to 'reword' for the first commit:
# reword 8a9b3c4 Add initial feature
# pick d1e2f34 Fix typo in docsSquashing Commits Together
Got several small commits that belong together? squash merges them into one meaningful commit — change the lines below pick to squash.
# Original commits:
# pick abc1234 Add user registration form
# pick def5678 Add validation for email field
# pick ghi9012 Fix styling on form
# To squash the last two into the first:
# pick abc1234 Add user registration form
# squash def5678 Add validation for email field
# squash ghi9012 Fix styling on formEditing an Older Commit
The edit command pauses the rebase at a specific commit. Make changes, git add, git commit --amend, then git rebase --continue.
When to Be Careful with Rewriting
Rewriting changes commit IDs — safe for local-only commits. Never rewrite history that's already pushed and shared; it wrecks your collaborators' work.
Quick Check: History Rewriting
You've made a commit with the message "Fix bug". You then realize you forgot to include one small file in that commit. What is the most appropriate Git command to fix this, assuming you haven't pushed the commit yet?
Recap: Cleaner Commits
Recap: git commit --amend fixes the last commit, while git rebase -i rewrites a series — reword, squash, fixup, or edit. Just be careful on shared branches.
คำถามที่พบบ่อย
บทเรียน “การรีเบสแบบโต้ตอบและการแก้ไขคอมมิต” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การรีเบสแบบโต้ตอบและการแก้ไขคอมมิต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Git Advanced: Monorepo, Submodules & Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การรีเบสแบบโต้ตอบและการแก้ไขคอมมิต”
เรียนรู้การเขียนประวัติใหม่ด้วย `git rebase -i` เพื่อให้คอมมิตสะอาดขึ้น และใช้ `git commit --amend` เพื่อแก้ไขคอมมิตล่าสุด คุณปฏิบัติ Git Advanced: Monorepo, Submodules & Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Git Advanced: Monorepo, Submodules & Workflows หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Git Advanced: Monorepo, Submodules & Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การรีเบสแบบโต้ตอบและการแก้ไขคอมมิต” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Git Advanced: Monorepo, Submodules & Workflows นี้ได้ไหม
ได้ บทเรียน Git Advanced: Monorepo, Submodules & Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การรีเบสแบบโต้ตอบและการแก้ไขคอมมิต
- การเก็บและเลือกนำการเปลี่ยนแปลงมาใช้
- Reflog สำหรับการกู้คืน
- Bisect: ตามหาคอมมิตที่มีปัญหา