서버 측 Git 훅
저장소 전체의 정책 적용과 자동화를 위해 `pre-receive`와 `update` 같은 서버 측 훅을 살펴봅니다.
서버 측 Git 훅은(는) CoddyKit의 무료 Git Advanced: Monorepo, Submodules & Workflows 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Git Advanced: Monorepo, Submodules & Workflows 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Git Advanced: Monorepo, Submodules & Workflows 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Welcome to Server-Side Hooks
In the previous lesson, we learned about client-side Git hooks. Now, let's explore server-side hooks, which operate on the Git server itself.
These hooks are powerful tools for enforcing repository-wide policies and automating tasks before or after changes are accepted into the central repository.
Client vs. Server Hooks
The key difference between client-side and server-side hooks is where they run:
- Client-side hooks: Run on your local machine before actions like committing or pushing. They help enforce local best practices.
- Server-side hooks: Run on the central Git server after you push changes, but before they're fully integrated. They ensure repository-wide rules are met by everyone.
Server-side hooks provide a stronger guarantee that rules are followed by all contributors.
Where Server Hooks Live
Server-side hooks reside in the hooks directory of your bare repository on the server.
A bare repository is one that doesn't have a working directory – it only contains the Git metadata. When you push to a remote, you're pushing to a bare repository.
Just like client-side hooks, they are executable scripts that Git runs at specific points in the workflow.
The 'pre-receive' Hook
The pre-receive hook is one of the most commonly used server-side hooks. It executes once per push, before any references (like branches or tags) are updated.
It receives a list of all references being pushed and their old/new commit IDs. If this script exits with a non-zero status, the entire push is rejected, and no references are updated.
This makes it ideal for enforcing global policies.
'pre-receive' in Action
The pre-receive hook is perfect for:
- Enforcing branch naming conventions: E.g., all new branches must start with "feature/", "bugfix/", etc.
- Validating commit messages: Ensuring every commit message includes a ticket ID or follows a specific format.
- Preventing pushes to protected branches: Blocking direct pushes to
mainordevelop. - Checking code for sensitive information: Basic credential scanning.
Example: Block Direct Main Push
Here's a conceptual example of a pre-receive script that prevents direct pushes to the main branch. If someone tries to push to main, the push will be rejected.
This script would be placed as pre-receive in the server's bare repository hooks directory and made executable.
#!/bin/sh
while read oldrev newrev refname
do
if [ "$refname" = "refs/heads/main" ]; then
echo "ERROR: Direct pushes to 'main' branch are forbidden."
echo "Please use a pull request workflow."
exit 1
fi
done
exit 0The 'update' Hook
The update hook is similar to pre-receive but runs once for each reference being updated by a push.
It takes three arguments: the name of the reference, the old object name, and the new object name. If any update script exits with a non-zero status, only that specific reference update is rejected.
This allows for more granular control over individual branch updates.
'update' in Action
The update hook is useful for:
- Enforcing fast-forward merges: Preventing non-fast-forward pushes to specific branches, ensuring a linear history.
- Implementing fine-grained access control: Allowing certain users to push to specific branches only.
- Logging updates: Recording every branch update for auditing purposes.
- Preventing force pushes: Ensuring history isn't rewritten on critical branches.
Setting Up Server Hooks
To implement server-side hooks:
- Access the server: You need SSH access or similar to the Git server's repository.
- Locate the hooks directory: Navigate to the
.git/hooksdirectory within your bare repository. - Create/Modify scripts: Write your hook script (e.g.,
pre-receiveorupdate). - Make executable: Ensure the script has execute permissions (e.g.,
chmod +x pre-receive).
Remember, these changes affect everyone interacting with that repository.
Best Practices & Security
When working with server-side hooks:
- Keep them simple: Complex logic can be hard to debug and maintain.
- Version control hooks: Consider storing your hook scripts in a separate repository and deploying them to your Git servers for consistency.
- Test thoroughly: Ensure your hooks don't accidentally block legitimate workflows.
- Consider performance: Hooks run on every push, so avoid resource-intensive operations.
They are powerful, so use them wisely!
Quick Check
Which of the following scenarios would be best handled by a pre-receive hook rather than an update hook?
Recap: Server-Side Hooks
You've explored the power of server-side Git hooks! We learned:
- Server-side hooks enforce policies on the central Git server.
pre-receiveruns once per push, good for global checks.updateruns once per reference, good for granular control.- Implementing them involves placing executable scripts in the bare repository's
hooksdirectory.
These hooks are crucial for maintaining code quality and workflow consistency in team environments. Keep practicing!
자주 묻는 질문
“서버 측 Git 훅” 강의는 무료인가요?
네 — “서버 측 Git 훅” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Git Advanced: Monorepo, Submodules & Workflows 강의 전체를 잠금 해제할 수 있습니다. Git Advanced: Monorepo, Submodules & Workflows 강의에는 총 4개의 강의가 포함되어 있습니다.
“서버 측 Git 훅”에서 뭘 배우나요?
저장소 전체의 정책 적용과 자동화를 위해 `pre-receive`와 `update` 같은 서버 측 훅을 살펴봅니다. 브라우저에서 직접 실행하는 실습 코드로 Git Advanced: Monorepo, Submodules & Workflows을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Git Advanced: Monorepo, Submodules & Workflows을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Git Advanced: Monorepo, Submodules & Workflows은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“서버 측 Git 훅” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Git Advanced: Monorepo, Submodules & Workflows 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Git Advanced: Monorepo, Submodules & Workflows 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 클라이언트 측 Git 훅
- 서버 측 Git 훅
- Git 구성 및 별칭
- Husky로 팀과 훅 공유