BM: Baca dalam Bahasa Melayu
World 10: Deep Git
Hooks
You will learn: how Git can run your own checks before a commit, and how to share them.
The problem
You keep committing with console.log left in. Your teammates sigh. Can Git stop you before the commit?
Pick one answer.
Watch
A hook: A script Git runs by itself at a set moment, like just before a commit. is a script that Git runs at a set moment. Hooks live in .git/hooks. Tap each moment.
Tap each point on the timeline.
Watch
A hook must be executable: chmod +x. If it ends with exit 1, Git stops the commit. exit 0 lets it through.
Try it
Put the life of a pre-commit hook in order.
First step at the top. Put these in order. Use the arrows, or drag.
- The hook exits with 0, so the commit is made
- Make it executable with chmod +x
- Write the script in .git/hooks/pre-commit
- Run git commit
- Git runs the hook first
Watch
A commit-msg hook checks the message. Git hands it the message file as $1. Here, “stuff” does not get in.
Predict
You add a hook in .git/hooks. Mei clones the repo. Does the hook run on her computer?
Pick one answer.
Check
Good habit, or needs work? Swipe each one.
Swipe right for good, left for needs work. Or use the buttons.
Keep hooks fast
You did it!
You can now make Git check your work before every commit.
Go deeper (optional)
New repos get sample hooks in .git/hooks, ending in .sample. Remove the ending to try one. A hook can be any language: the first line names it. git commit --no-verify skips pre-commit and commit-msg. git hook run pre-commit runs a hook by hand.
Bahasa Melayu
Anda asyik commit (titik simpan) dengan console.log yang tertinggal. Rakan sepasukan mengeluh. Boleh Git halang anda sebelum commit?
Hook (cangkuk) ialah skrip yang Git jalankan sendiri pada saat tertentu. Hook tinggal dalam .git/hooks. Ketik setiap saat.
Hook mesti boleh dilaksanakan: chmod +x. Kalau ia tamat dengan exit 1, Git hentikan commit. exit 0 benarkan ia lalu.
Susun kitaran hidup hook pre-commit.
Hook commit-msg semak mesej. Git beri fail mesej kepadanya sebagai $1. Di sini, “stuff” tak lepas.
Anda tambah hook dalam .git/hooks. Mei clone repo itu. Adakah hook itu berjalan dalam komputernya?
Untuk kongsi hook, commit dalam folder seperti .githooks. Kemudian setiap orang tetapkan core.hooksPath. Baca hook itu, kemudian tetapkannya.
cat .githooks/pre-commit. Kemudian git config core.hooksPath .githooks.Tabiat baik, atau perlu dibaiki? Leret setiap satu.
Kini anda boleh suruh Git semak kerja anda sebelum setiap commit.
You finished this before. Practice is always welcome!
Official docs: githooks · git config (core.hooksPath) · git commit (--no-verify)
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?