Git Commands
Merge
Commit
Edit commit before push
1 | git commit --amend |
Hooks
Like a real hook, Git Hook will run a specific script before or after important git actions, such as committing, rebasing, and pushing.
Where is it
All the hook files are stored in the .git/hooks
:
1 | applypatch-msg.sample post-update.sample pre-merge-commit.sample pre-receive.sample update.sample |
How to write?
Open post-update.sample
; here is an example:
1 |
|
Actually, not only bash script works, Python
, Lua
, or other script language work. Remember to add the path of exec file at the first line.
1 |
|
Exit Value
In some pre
check, the exit value could control whether the next step will run. If exit(0)
is used in the pre-commit hook, the commit will achieve success. However, if the hook exit with -1
(exit(-1)
), the commit will be prevented until the requirements are met. Here is an example:
1 | #!/usr/bin/env python3 |
In this example, the script uses black
to check whether the code is formatted or not. If the code is unformatted, it will prevent the commit.
Usage
Remove the .sample
in the file name; the hook will automatically run at a certain time.