Git: How to do pre-commit

Git: How to do pre-commit

STEP1: Find .git/hooks

Find the hooks folder in the (hidden) .git configuration folder

1
cd .git/hooks

STEP2: Create pre-commit file

1
touch pre-commit

STEP3: edit pre-commit file

1
vi pre-commit

and add something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash

# store the top level git repository path
root_directory=$(git rev-parse --show-toplevel)

# list all subdirectories
subdirectories=("HW1", "HW2", "HW3")

# iterate over each subdirectory
for subdir in "$(subdirectories[@])";
do
(cd "$root_directory/$subdir" && make clean)
done

# Continue with the commit
exit 0

Now whenever you commit, it will automatically clean the subdirectory and will not commit any executable files. Note, if the operating system changes, the script will also need to change.

Author

Tragic Master

Posted on

2024-02-15

Updated on

2024-02-15

Licensed under