BM: Baca dalam Bahasa Melayu
World 10: Deep Git
git worktree
You will learn: how to work on two branches at once, in two folders, with one repo.
The problem
You are halfway through a big change. Then a bug report: fix main right now! Which way leaves your half-done work untouched?
Pick one answer.
Watch
git worktree add makes a second working folder, on its own branch. Same repo, same history. Each folder is a worktree: An extra working folder for the same repo, on its own branch..
Try it
Make a worktree at ../diary-fix on a new branch called fix.
Hint
Typegit worktree add ../diary-fix -b fix.Predict
You commit a fix inside diary-fix. Then you go back to diary. Can you see that commit there?
Pick one answer.
Watch
Done? git worktree remove deletes the extra folder. The branch and its commits stay safe.
Check
Put the bug-fix trip in order.
First step at the top. Put these in order. Use the arrows, or drag.
- cd ../diary
- Fix the bug and commit
- git worktree add ../diary-fix -b fix
- git worktree remove ../diary-fix
- cd ../diary-fix
You did it!
You can now work on two branches at once, in two folders.
Go deeper (optional)
A branch can be checked out in just one worktree at once. Git refuses a second one. git worktree remove refuses if the folder has changes; --force drops them. Deleted a worktree folder by hand? git worktree prune cleans up Git’s notes about it.
Bahasa Melayu
Anda separuh jalan buat perubahan besar. Kemudian ada laporan pepijat: baiki main sekarang juga! Cara mana yang tak usik kerja separuh siap anda?
git worktree add buat folder kerja kedua, di branch (cabang) sendiri. Repo sama, sejarah sama. Setiap folder ialah worktree.
Buat worktree di ../diary-fix pada branch baru bernama fix.
git worktree add ../diary-fix -b fix.Anda commit pembaikan dalam diary-fix. Kemudian anda balik ke diary. Boleh nampak commit itu di sana?
Dah siap? git worktree remove padam folder tambahan itu. Branch dan commitnya kekal selamat.
Susun perjalanan baiki pepijat ini.
Kini anda boleh kerja di dua branch serentak, dalam dua folder.
You finished this before. Practice is always welcome!
Official docs: git worktree
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?