0
0
Gitdevops~5 mins

post-merge hook in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes after combining code changes from others, you want your project to update automatically. The post-merge hook helps by running commands right after a merge finishes, so you don't have to do extra steps manually.
When you want to automatically install new dependencies after merging code that added them
When you need to rebuild your project files after merging changes
When you want to clear caches or restart services after a merge
When you want to notify your team or log merge events automatically
When you want to run tests or checks immediately after merging
Config File - post-merge
post-merge
#!/bin/sh
# This script runs after a successful git merge

echo "Merge completed. Running post-merge tasks..."

# Example: install dependencies if package.json changed
if git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD | grep -q '^package.json$'; then
  echo "package.json changed, installing dependencies..."
  npm install
fi

# Add other commands below

This is a shell script placed in the .git/hooks/ directory named post-merge. It runs automatically after a successful merge.

The script checks if package.json changed in the merge. If yes, it runs npm install to update dependencies.

You can add any commands you want to run after merges here.

Commands
Make the post-merge hook script executable so Git can run it automatically after merges.
Terminal
chmod +x .git/hooks/post-merge
Expected OutputExpected
No output (command runs silently)
+x - Adds execute permission to the file
Merge the 'feature-branch' into the current branch. After the merge, the post-merge hook runs automatically.
Terminal
git merge feature-branch
Expected OutputExpected
Updating abc1234..def5678 Fast-forward package.json | 2 ++ 1 file changed, 2 insertions(+) Merge completed. Running post-merge tasks... package.json changed, installing dependencies... added 50 packages, and audited 50 packages in 2s found 0 vulnerabilities
Check the latest commit to confirm the merge was successful.
Terminal
git log -1 --oneline
Expected OutputExpected
def5678 Merge branch 'feature-branch'
Key Concept

If you remember nothing else, remember: the post-merge hook runs automatically after a merge to help you automate follow-up tasks.

Common Mistakes
Not making the post-merge hook script executable
Git will not run the script if it lacks execute permission, so your tasks won't run.
Run 'chmod +x .git/hooks/post-merge' to make the script executable.
Placing the post-merge script outside the .git/hooks directory
Git only runs hooks located in the .git/hooks folder with the exact hook name.
Put the script in .git/hooks/post-merge exactly.
Writing commands that assume a certain shell without specifying it
The script may fail if run in a different shell environment.
Start the script with a shebang line like '#!/bin/sh' to specify the shell.
Summary
Create a post-merge script in .git/hooks/post-merge to automate tasks after merges.
Make the script executable with chmod +x so Git can run it.
Run a merge command; the post-merge hook runs automatically after the merge.
Use the hook to run commands like installing dependencies or rebuilding files.