Git Hooks, Aliases, and Scripts

Follow Us

Our Communities

Module 7 – Git Hooks, Aliases, and Scripts

The beauty of Git lies in its flexibility and adaptability. It provides powerful tools to customize and automate your workflow, enabling you to spend less time on manual tasks and more on actual development. In this module, we will delve into these tools—Git hooks, aliases, and scripts—which not only streamline your processes but also enable enforcement of code quality and consistency.

We will cover:

  • 7.1 Git Hooks
  • 7.2 Git Aliases
  • 7.3 Git Scripts

Through exploring Git hooks, you’ll learn to automate and enforce rules at different stages of the Git workflow. Git aliases will offer you shortcuts to your frequently used Git commands, saving you precious time and effort. Lastly, with Git scripts, we will uncover the potential to chain multiple commands together and even create new ones.

By the end of this module, you’ll be fully equipped to customize your Git environment to meet your specific needs and workflow, making your interactions with Git more efficient and enjoyable. Let’s jump right into the fascinating world of Git customization!

Git Hooks

Git hooks are scripts that can automatically run when specific events occur in the Git lifecycle, such as before/after committing, pushing, and receiving updates. These scripts can automate various tasks, from style checking to triggering builds and other automation jobs.

  • Understanding Git Hooks: Git hooks reside in the .git/hooks directory of every Git repository. They are named after the event they’re tied to, with a .sample suffix to indicate that they won’t run until renamed (e.g., pre-commit.sample).

  • Implementing Git Hooks: Let’s create a simple pre-commit hook. You will need to create a new file in .git/hooks called pre-commit (no extension) with the following content:

files=$(git diff --cached --name-only --diff-filter=ACMR | grep ' \.js$')
[ -z "$files" ] && exit 0

# check for console.log in javascript files
for file in $files; do
  grep -H -n console.log "$file"
  if [ $? -eq 0 ]; then
    echo "COMMIT REJECTED Found 'console.log' references. Please remove them before committing."
    exit 1

This script prevents commits containing console.log in JavaScript files.

  • Sharing Git Hooks: Git hooks are not included when you clone a repository. This is because they can execute any code, which poses a security risk. To share hooks, you can create a hooks directory in your repo, store your hooks there, and instruct your team to link or copy them into their .git/hooks directory.

Git Aliases

Git aliases let you create your own custom commands by mapping new command names to existing Git commands. This can save typing and make complex commands easier to remember.

  • Creating Git Aliases: To create a Git alias, use the git config command. For example, to create an alias st for status, you would run: git config --global status.

  • Advanced Git Aliases: Aliases can also map to complex command sequences. For instance, to create an alias that shows the detailed log as a graph, you could run: git config --global alias.graph "log --all --decorate --oneline --graph".

Git Scripts

Git scripts are custom scripts that automate sequences of Git commands. They can simplify complex workflows and ensure consistency in recurring tasks.

  • Writing Git Scripts: A Git script is just a shell script with Git commands. To create one, write your commands in a file, make the file executable (chmod +x [file-name]), and put it in your PATH.

  • Implementing Git Scripts: For example, to automate the process of creating a new branch and switching to it, you might write a script like this:

git checkout -b $branch

To run the script, you would use: ./ new-feature.

These tools – hooks, aliases, and scripts – empower you to customize Git to your liking and automate routine tasks, improving productivity and consistency.

Exercises and Practice

Exercise 1: Implement a Git Hook: Set up a pre-commit hook in your repository that checks for any TODO comments in your code. If any are found, the commit should be aborted with an appropriate message.

Exercise 2: Create a Git Alias: Create an alias for the following Git command: git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short. This command shows the commit history in a certain format. The alias could be hist.

Exercise 3: Create a Complex Git Alias: Create an alias unstage that undoes git add. Hint: The command is git reset HEAD <file>.

Exercise 4: Implement a Git Script: Write a script named that removes all branches that have been merged into the current branch. Be careful not to delete the main or master branches.

Exercise 5: Share Git Hooks: Create a pre-push hook that prevents any push where the latest commit message contains the word WIP. Place this hook in a new hooks directory in your repository and write instructions in your README on how to install it.


Remember, these exercises are intended to get you familiar with using hooks, aliases, and scripts in Git. Feel free to experiment and create your own based on your needs and workflow.

Extracting Data from the Repository


Repository Maintenance