Skip to content

Commit 86b0b2d

Browse files
committed
new post added + folder rename + minor changes
1 parent b1ed039 commit 86b0b2d

File tree

7 files changed

+143
-10
lines changed

7 files changed

+143
-10
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# git-hooks-automating-your-workflow
2+
3+
Git hooks are powerful scripts that can automate various aspects of your development workflow. They allow you to execute custom scripts before or after important Git events, such as committing, pushing, or merging. This post will introduce you to Git hooks and show you how to leverage them effectively.
4+
5+
## What Are Git Hooks?
6+
7+
Git hooks are scripts that Git executes before or after events such as: commit, push, and receive. They reside in the `.git/hooks` directory of every Git repository and can be written in any scripting language.
8+
9+
## Types of Git Hooks
10+
11+
Git hooks can be categorized into client-side and server-side hooks:
12+
13+
### Client-side Hooks
14+
15+
1. **pre-commit**: Runs before a commit is created
16+
2. **prepare-commit-msg**: Runs before the commit message editor is fired up
17+
3. **commit-msg**: Runs after the commit message is saved
18+
4. **post-commit**: Runs after the commit is created
19+
5. **pre-push**: Runs before a push is executed
20+
21+
### Server-side Hooks
22+
23+
1. **pre-receive**: Runs when the server receives a push
24+
2. **update**: Similar to pre-receive, but runs once for each branch
25+
3. **post-receive**: Runs after a push has been accepted
26+
27+
## Setting Up a Git Hook
28+
29+
1. Navigate to your repository's `.git/hooks` directory.
30+
2. Create a new file with the name of the hook you want to implement (e.g., `pre-commit`).
31+
3. Remove the `.sample` extension if it exists.
32+
4. Make the file executable:
33+
34+
```bash
35+
chmod +x .git/hooks/pre-commit
36+
```
37+
38+
5. Edit the file with your desired script.
39+
40+
## Example: A Simple pre-commit Hook
41+
42+
Here's a simple pre-commit hook that checks for debugging statements:
43+
44+
```bash
45+
#!/bin/sh
46+
47+
if git diff --cached | grep -E '(console.log|debugger)' > /dev/null; then
48+
echo "Error: Debugging statements detected. Please remove them before committing."
49+
exit 1
50+
fi
51+
```
52+
53+
This script checks for `console.log` or `debugger` statements in your staged changes and prevents the commit if any are found.
54+
55+
## Best Practices for Using Git Hooks
56+
57+
1. **Keep hooks simple**: Complex hooks can slow down Git operations.
58+
2. **Use hooks consistently**: Ensure all team members use the same hooks.
59+
3. **Version control your hooks**: Store hooks in your repository and symlink them.
60+
4. **Make hooks configurable**: Allow developers to bypass hooks when necessary.
61+
5. **Document your hooks**: Ensure team members understand what each hook does.
62+
63+
## Sharing Hooks with Your Team
64+
65+
To share hooks with your team:
66+
67+
1. Create a `hooks` directory in your repository.
68+
2. Add your hook scripts to this directory.
69+
3. Create a setup script that symlinks these hooks to `.git/hooks`.
70+
4. Add instructions for running the setup script to your README.
71+
72+
## Advanced Git Hook Usage
73+
74+
### Linting
75+
76+
Use a pre-commit hook to run linters:
77+
78+
```bash
79+
#!/bin/sh
80+
81+
if ! npm run lint; then
82+
echo "Linting failed. Please fix errors before committing."
83+
exit 1
84+
fi
85+
```
86+
87+
### Automatic Formatting
88+
89+
Use a pre-commit hook to automatically format code:
90+
91+
```bash
92+
#!/bin/sh
93+
94+
if ! npm run format; then
95+
echo "Formatting failed. Please run formatting manually and try again."
96+
exit 1
97+
fi
98+
```
99+
100+
### Preventing Commits to Specific Branches
101+
102+
Use a pre-commit hook to prevent direct commits to protected branches:
103+
104+
```bash
105+
#!/bin/sh
106+
107+
branch="$(git rev-parse --abbrev-ref HEAD)"
108+
109+
if [ "$branch" = "main" ]; then
110+
echo "You can't commit directly to main branch"
111+
exit 1
112+
fi
113+
```
114+
115+
## Conclusion
116+
117+
Git hooks are a powerful tool for automating and enforcing development workflows. By implementing appropriate hooks, you can improve code quality, enforce coding standards, and streamline your development process.
118+
119+
Remember, while hooks are powerful, they should be used judiciously. Overly restrictive hooks can hinder productivity, so strike a balance between automation and flexibility.
120+
121+
Happy coding, and may your Git hooks serve you well!
122+
123+
> Written by **Sakib Ahmed** | [GitHub](https://github.com/devvsakib)

docs/index.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"title": "pull_request_tutorial_for_beginners"
4+
},
5+
{
6+
"title": "mastering_git_branching_strategies"
7+
},
8+
{
9+
"title": "git_hooks_automating_your_workflow"
10+
}
11+
]

posts/_a_guide_for_effective_collaboration.md renamed to docs/mastering_git_branching_strategies.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,6 @@ A well-implemented Git branching strategy can significantly improve your team's
9292

9393
Remember, the best strategy is one that your team can follow effectively. Start with a basic approach and evolve it as your project and team grow.
9494

95-
Happy branching!
95+
Happy branching!
96+
97+
> Written by **Sakib Ahmed** | [GitHub](https://github.com/devvsakib)

posts/pull_request_tutorial_for_beginners.md renamed to docs/pull_request_tutorial_for_beginners.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,6 @@ You may also delete the branch from your fork on GitHub.
119119

120120
Congratulations! You've successfully created and merged a pull request. This process helps maintain code quality and encourages collaboration among developers.
121121

122-
This tutorial covers the basics of creating a pull request and includes best practices to help beginners understand the process.
122+
This tutorial covers the basics of creating a pull request and includes best practices to help beginners understand the process.
123+
124+
> Written by **Sakib Ahmed** | [GitHub](https://github.com/devvsakib)

posts/index.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/pages/Doc/index.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ const DocList = () => {
1111
useEffect(() => {
1212
const fetchDocs = async () => {
1313
try {
14-
// const response = await fetch('/posts/index.json');
15-
const response = await fetch('https://github-error-solve.vercel.app/posts/index.json');
14+
const response = await fetch('https://github-error-solve.vercel.app/docs/index.json');
1615
if (!response.ok) {
1716
throw new Error(`HTTP error! status: ${response.status}`);
1817
}
@@ -41,7 +40,7 @@ const DocList = () => {
4140
<ul>
4241
{
4342
docs.map(item =>
44-
<Link to={item} className='capitalize'>{item.replace(/_/g, ' ')}</Link>
43+
<Link to={item.title} className='capitalize'>{item.title.replace(/_/g, ' ')}</Link>
4544
)
4645
}
4746
</ul>

src/pages/Doc/single doc/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const DocDetail = () => {
3131
useEffect(() => {
3232
const fetchContent = async () => {
3333
try {
34-
const response = await fetch(`/posts/${slug}.md`);
34+
const response = await fetch(`/docs/${slug}.md`);
3535
if (!response.ok) {
3636
throw new Error(`Failed to fetch content: ${response.statusText}`);
3737
}

0 commit comments

Comments
 (0)