Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

background color dark increased #209

Merged
merged 19 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions docs/git_hooks_automating_your_workflow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# git-hooks-automating-your-workflow

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.

## What Are Git Hooks?

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.

## Types of Git Hooks

Git hooks can be categorized into client-side and server-side hooks:

### Client-side Hooks

1. **pre-commit**: Runs before a commit is created
2. **prepare-commit-msg**: Runs before the commit message editor is fired up
3. **commit-msg**: Runs after the commit message is saved
4. **post-commit**: Runs after the commit is created
5. **pre-push**: Runs before a push is executed

### Server-side Hooks

1. **pre-receive**: Runs when the server receives a push
2. **update**: Similar to pre-receive, but runs once for each branch
3. **post-receive**: Runs after a push has been accepted

## Setting Up a Git Hook

1. Navigate to your repository's `.git/hooks` directory.
2. Create a new file with the name of the hook you want to implement (e.g., `pre-commit`).
3. Remove the `.sample` extension if it exists.
4. Make the file executable:

```bash
chmod +x .git/hooks/pre-commit
```

5. Edit the file with your desired script.

## Example: A Simple pre-commit Hook

Here's a simple pre-commit hook that checks for debugging statements:

```bash
#!/bin/sh

if git diff --cached | grep -E '(console.log|debugger)' > /dev/null; then
echo "Error: Debugging statements detected. Please remove them before committing."
exit 1
fi
```

This script checks for `console.log` or `debugger` statements in your staged changes and prevents the commit if any are found.

## Best Practices for Using Git Hooks

1. **Keep hooks simple**: Complex hooks can slow down Git operations.
2. **Use hooks consistently**: Ensure all team members use the same hooks.
3. **Version control your hooks**: Store hooks in your repository and symlink them.
4. **Make hooks configurable**: Allow developers to bypass hooks when necessary.
5. **Document your hooks**: Ensure team members understand what each hook does.

## Sharing Hooks with Your Team

To share hooks with your team:

1. Create a `hooks` directory in your repository.
2. Add your hook scripts to this directory.
3. Create a setup script that symlinks these hooks to `.git/hooks`.
4. Add instructions for running the setup script to your README.

## Advanced Git Hook Usage

### Linting

Use a pre-commit hook to run linters:

```bash
#!/bin/sh

if ! npm run lint; then
echo "Linting failed. Please fix errors before committing."
exit 1
fi
```

### Automatic Formatting

Use a pre-commit hook to automatically format code:

```bash
#!/bin/sh

if ! npm run format; then
echo "Formatting failed. Please run formatting manually and try again."
exit 1
fi
```

### Preventing Commits to Specific Branches

Use a pre-commit hook to prevent direct commits to protected branches:

```bash
#!/bin/sh

branch="$(git rev-parse --abbrev-ref HEAD)"

if [ "$branch" = "main" ]; then
echo "You can't commit directly to main branch"
exit 1
fi
```

## Conclusion

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.

Remember, while hooks are powerful, they should be used judiciously. Overly restrictive hooks can hinder productivity, so strike a balance between automation and flexibility.

Happy coding, and may your Git hooks serve you well!

> Written by **Sakib Ahmed** | [GitHub](https://github.com/devvsakib)
11 changes: 11 additions & 0 deletions docs/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"title": "pull_request_tutorial_for_beginners"
},
{
"title": "mastering_git_branching_strategies"
},
{
"title": "git_hooks_automating_your_workflow"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ A well-implemented Git branching strategy can significantly improve your team's

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.

Happy branching!
Happy branching!

> Written by **Sakib Ahmed** | [GitHub](https://github.com/devvsakib)
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,6 @@ You may also delete the branch from your fork on GitHub.

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

This tutorial covers the basics of creating a pull request and includes best practices to help beginners understand the process.
This tutorial covers the basics of creating a pull request and includes best practices to help beginners understand the process.

> Written by **Sakib Ahmed** | [GitHub](https://github.com/devvsakib)
4 changes: 0 additions & 4 deletions posts/index.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/pages/Doc/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const DocList = () => {
useEffect(() => {
const fetchDocs = async () => {
try {
// const response = await fetch('/posts/index.json');
const response = await fetch('https://github-error-solve.vercel.app/posts/index.json');
const response = await fetch('https://github-error-solve.vercel.app/docs/index.json');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
Expand Down Expand Up @@ -41,7 +40,7 @@ const DocList = () => {
<ul>
{
docs.map(item =>
<Link to={item} className='capitalize'>{item.replace(/_/g, ' ')}</Link>
<Link to={item.title} className='capitalize'>{item.title.replace(/_/g, ' ')}</Link>
)
}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Doc/single doc/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const DocDetail = () => {
useEffect(() => {
const fetchContent = async () => {
try {
const response = await fetch(`/posts/${slug}.md`);
const response = await fetch(`/docs/${slug}.md`);
if (!response.ok) {
throw new Error(`Failed to fetch content: ${response.statusText}`);
}
Expand Down