DEV Community

Wycliffe A. Onyango
Wycliffe A. Onyango

Posted on

100 Days of DevOps: Day 34

Completing the Git Hooks Task

This article details the steps taken to successfully complete the Git repository management task, which involved merging a feature branch, creating a post-update hook, and pushing the changes to a remote repository. We will also explore the broader context of why Git hooks are a powerful tool in a developer's workflow.

Task Summary

The objective was to:

  1. Navigate to a local Git repository cloned from /opt/official.git.
  2. Merge the feature branch into the master branch.
  3. Create a post-update hook in the remote repository so that a new tag with the name release-YYYY-MM-DD is created automatically whenever changes are pushed to the master branch.
  4. Push the merged changes and verify that the hook successfully created the tag.

Step 1: Merging the Branches

The initial step was to prepare the local repository for the push. This involved merging the feature branch, which contained the new code, into the master branch.

# Navigate to the local repository
cd /usr/src/kodekloudrepos/official

# Switch to the master branch
sudo git checkout master

# Merge the feature branch into master
sudo git merge feature
Enter fullscreen mode Exit fullscreen mode

This command integrates the commit history of the feature branch into the master branch, creating a new merge commit.

Step 2: Creating the post-update Hook

Git hooks are scripts that execute automatically at specific points in the Git workflow. A post-update hook is triggered on the remote repository after a push is completed. It is an ideal place to perform actions like creating release tags, deploying code, or sending notifications.

The key insight to completing this step was understanding that the hook must be placed and made executable in the bare repository (/opt/official.git), not the local cloned repository.

  1. Navigate to the hooks directory on the remote repository:

    cd /opt/official.git/hooks
    
  2. Create the post-update script:

    sudo vi post-update
    
  3. Add the script content:
    The following script checks if the push was to the master branch. If so, it gets the current date and uses it to create a new tag.

    #!/bin/sh
    
    # This hook will be executed after any push to the repository.
    # It checks if the push was to the 'master' branch and creates a tag.
    
    DATE=$(date +%Y-%m-%d)
    TAG_NAME="release-$DATE"
    
    # Check if the tag already exists to avoid errors
    if ! git tag -l | grep -q "$TAG_NAME"; then
      git tag "$TAG_NAME"
      echo "Created tag: $TAG_NAME"
    else
      echo "Tag $TAG_NAME already exists. Skipping..."
    fi
    
  4. Make the script executable and set ownership:
    These are crucial steps. The hook must be executable for the Git daemon to run it. The chown command sets the correct ownership to prevent permission errors. The chown command was adjusted to use the natasha:natasha user and group, as the git:git user did not exist on this specific server.

    sudo chmod +x post-update
    sudo chown natasha:natasha post-update
    

Step 3: Pushing Changes and Verifying the Hook

With the hook correctly set up, the final step was to push the merged master branch. This push would trigger the post-update hook on the remote repository.

  1. Navigate back to the local repository:

    cd /usr/src/kodekloudrepos/official
    

    or use:

    cd -
    
  2. Push the changes:
    The initial git push origin master failed due to ownership issues. The problem was resolved by using sudo git push origin master, which executed the command with administrative privileges and allowed the push to complete.

    sudo git push origin master
    
  3. Verify the hook execution:
    The terminal output from the push confirmed that the hook was successfully executed. The line remote: Created tag: release-2025-09-06 showed that the new tag was created on the remote repository.

  4. Confirm the tag's existence:
    The git ls-remote command was used to list all tags in the remote repository. The initial attempt failed due to a permissions issue, but using sudo resolved it, confirming the tag's presence.

    sudo git ls-remote --tags origin
    # Output: 8f75e59172a6ab5f4f5e29e0f5dab33959ec08ac refs/tags/release-2025-09-06
    

The final output clearly showed that the release-2025-09-06 tag was successfully created, indicating the task was completed.

The Importance of Git Hooks

Git hooks are a cornerstone of modern software development workflows, offering a flexible way to automate tasks and enforce policies. Here's why they are so valuable:

  • Automation and Consistency: Hooks automate repetitive tasks, such as creating release tags, running tests, or performing code analysis. This ensures these actions are performed consistently every time, reducing human error.

  • Continuous Integration/Continuous Deployment (CI/CD): Hooks are a fundamental component of CI/CD pipelines. A post-receive or post-update hook can trigger a build server to automatically deploy new changes to a staging or production environment, creating a seamless and efficient deployment process.

  • Policy Enforcement: Pre-commit and pre-receive hooks can be used to enforce code quality standards. For example, a pre-commit hook can prevent a commit if the code doesn't pass a linter check or if it contains sensitive information. A pre-receive hook can prevent developers from pushing directly to the master branch.

  • Communication: Hooks can be used to notify team members about significant events. A hook can send an email, a Slack message, or a webhook to a third-party service whenever a new release is tagged or a push is made to a specific branch.

By utilizing Git hooks, development teams can streamline their workflows, improve code quality, and automate critical processes, making the entire development lifecycle more robust and efficient.

Top comments (0)