클라이언트 측 Git 훅
로컬에서 표준을 강제하도록 `pre-commit`과 `post-merge` 같은 클라이언트 측 훅을 이해하고 구현합니다.
클라이언트 측 Git 훅은(는) CoddyKit의 무료 Git Advanced: Monorepo, Submodules & Workflows 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Git Advanced: Monorepo, Submodules & Workflows 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Git Advanced: Monorepo, Submodules & Workflows 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Intro to Git Hooks
Welcome! In this lesson, you'll learn about Git hooks. These are scripts that Git automatically runs before or after events like committing, merging, or pushing.
Think of them as automated assistants for your Git workflow. They help enforce project standards and automate routine tasks.
Client-Side vs. Server-Side
Git hooks come in two flavors:
- Client-side hooks: Run on your local machine. They only affect your personal repository. Examples include `pre-commit` and `post-merge`.
- Server-side hooks: Run on the Git server. They affect all pushes to the repository. We'll cover these in a later lesson.
This lesson focuses on client-side hooks and how to use them locally.
Where Hooks Live
All Git hooks are stored in the .git/hooks directory within your repository. When you initialize a new Git repository, Git populates this directory with sample hook scripts.
Let's take a look inside a typical .git/hooks folder:
ls -F .git/hooks/Activating a Hook
By default, the sample hook scripts end with .sample, making them inactive. To activate a hook, you need to:
- Remove the
.sampleextension from its filename. - Make the script executable.
Let's activate the pre-commit hook by renaming and making it executable:
cd .git/hooks
mv pre-commit.sample pre-commit
chmod +x pre-commit`pre-commit`: Your Local Guard
The pre-commit hook is one of the most useful client-side hooks. It runs just before Git asks you for a commit message and before the actual commit is created.
If the script exits with a non-zero status, Git aborts the commit. This makes it perfect for:
- Running linters (e.g., ESLint, Flake8)
- Executing unit tests
- Checking for common mistakes (e.g., debug statements)
Example: `pre-commit` for Linting
Here's a simple pre-commit script that checks if any staged files contain the word "TODO". If it finds "TODO", the commit is blocked.
You can edit the .git/hooks/pre-commit file directly to add this content:
#!/bin/sh
# Check for 'TODO' in staged changes
if git diff --cached | grep -q "TODO"; then
echo "Error: 'TODO' found in staged changes. Please resolve before committing."
exit 1
fiTesting the `pre-commit` Hook
Now, let's try to commit a file that contains "TODO". The pre-commit hook should prevent the commit.
First, create a file with "TODO", then try to commit it:
echo "This is a file with a TODO item." > my_feature.txt
git add my_feature.txt
git commit -m "Attempt to commit with TODO"`post-merge`: After the Merge
The post-merge hook runs immediately after a successful git merge or git pull (which is essentially a fetch followed by a merge or rebase).
It's useful for automating tasks that should happen after your working directory has been updated by a merge, such as:
- Installing new dependencies
- Rebuilding your project
- Updating documentation or symlinks
Example: `post-merge` for Dependencies
Imagine you're working on a project with Node.js. After merging a branch that introduced new dependencies (updated package.json), you'd want to run npm install.
Here's a post-merge script that checks if package.json was changed by the merge and runs npm install if it was:
#!/bin/sh
# Run npm install if package.json changed
if git diff --name-only HEAD@{1} HEAD | grep -q "package.json"; then
echo "package.json changed, running npm install..."
# npm install # Uncomment in a real project
echo "(npm install simulated)"
fiHook Challenge
You've learned about client-side Git hooks! Which of the following statements are TRUE about them?
Client-Side Hooks: Recap
Great job! You've successfully explored client-side Git hooks. Here's what we covered:
- What they are: Scripts that run automatically at specific Git events.
- Where they live: In the
.git/hooksdirectory. - How to activate: Rename from
.sampleand make executable. - `pre-commit`: Runs before a commit, useful for validation and linting.
- `post-merge`: Runs after a merge, good for dependency updates or rebuilding.
Client-side hooks are powerful tools for personal workflow automation and local quality checks!
자주 묻는 질문
“클라이언트 측 Git 훅” 강의는 무료인가요?
네 — “클라이언트 측 Git 훅” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Git Advanced: Monorepo, Submodules & Workflows 강의 전체를 잠금 해제할 수 있습니다. Git Advanced: Monorepo, Submodules & Workflows 강의에는 총 4개의 강의가 포함되어 있습니다.
“클라이언트 측 Git 훅”에서 뭘 배우나요?
로컬에서 표준을 강제하도록 `pre-commit`과 `post-merge` 같은 클라이언트 측 훅을 이해하고 구현합니다. 브라우저에서 직접 실행하는 실습 코드로 Git Advanced: Monorepo, Submodules & Workflows을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Git Advanced: Monorepo, Submodules & Workflows을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Git Advanced: Monorepo, Submodules & Workflows은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“클라이언트 측 Git 훅” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Git Advanced: Monorepo, Submodules & Workflows 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Git Advanced: Monorepo, Submodules & Workflows 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 클라이언트 측 Git 훅
- 서버 측 Git 훅
- Git 구성 및 별칭
- Husky로 팀과 훅 공유