DEV Community

Cover image for A Better Way to GitHub Commit: Simplify with a Custom .zshrc Function
Jahidul Islam (Saeid)
Jahidul Islam (Saeid)

Posted on

A Better Way to GitHub Commit: Simplify with a Custom .zshrc Function

Version control systems like Git are essential tools for developers to track changes in their projects. Committing code changes with meaningful messages is crucial for maintaining a well-organized and easily navigable project history. While git commit -m works perfectly fine, there's a more engaging and efficient way to create commit messages using a custom script. In this blog, we'll explore how to create a custom GitHub commit script to enhance your version control workflow.

The Problem with Generic Commit Messages

When you use git commit -m, you often end up with generic commit messages like "Fixed a bug" or "Updated code." These messages lack context and don't provide meaningful insights into the changes made, which can lead to confusion down the line. Additionally, it can be challenging to maintain a consistent commit message format across a development team, making collaboration more difficult.

Simplifying GitHub Commits with a Custom .zshrc Function

Firstly, you need to understand what the .zshrc file is. It's a configuration file for the Zsh shell, a powerful, highly customizable shell. You can use it to define custom functions and aliases, which can significantly improve your command-line experience.

Here's the custom gitcommit function that you can add to your .zshrc file:

gitcommit() {
    local commit_type
    PS3="Select a commit type: "

    local -A options=(
        [initial]="๐ŸŽ‰ Initial commit"
        [feat]="โœจ New Feature"
        [fix]="๐Ÿ› Fix Bug"
        [docs]="๐Ÿ“š Update Documentation"
        [refactor]="๐Ÿ”จ Refactor code"
        [perf]="๐Ÿš€ Improves performance"
        [style]="๐Ÿ’… Style changes"
        # Add more if you want
    )

    select commit_type in "${(k)options[@]}"; do
        if [[ -n $commit_type ]]; then
            break
        else
            echo "Invalid option. Please select a valid commit type."
        fi
    done

    commit_hint=$options[$commit_type]

    print -n "Enter your commit message ($commit_hint): "
    read commit_message
    git add .
    git commit -m "${options[$commit_type]} - $commit_message"
}

Enter fullscreen mode Exit fullscreen mode

This custom gitcommit function makes it easy and fun to commit your changes. Here's how it works:

  1. You run the gitcommit command in your terminal.

  2. It presents you with a menu of commit types such as "Initial commit," "New Feature," "Fix Bug," and more. You can easily customize this list to match your project's needs.

  3. You select a commit type by entering the corresponding number.

  4. You're prompted to enter your commit message, with a default hint based on the type you selected.

  5. The function stages all changes (with git add .) and commits them with a message in the format "Type - Your Message."

By using this custom function, you not only streamline the commit process but also maintain a consistent and informative commit history. It ensures that you and your team can quickly understand the purpose of each commit without delving into the details of the changes.

Customizing the Commit Types

In the custom gitcommit function, you can easily customize the list of commit types to match your project's conventions. Simply edit the options associative array with the types and emojis you prefer. You can also add or remove types based on your needs.

Conclusion

Improving your GitHub commit workflow doesn't have to be a complicated process. By creating a custom function in your .zshrc file, you can simplify the commit process and ensure that your commit history is more informative and consistent. This is just one example of how you can enhance your development workflow with custom scripts and functions in your shell environment. Give it a try, and see how it makes your work on GitHub repositories more efficient and enjoyable. Happy coding! ๐Ÿš€

Top comments (0)