Bird
Raised Fist0
Gitdevops~5 mins

post-merge hook in Git - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the post-merge hook in Git?
easy
A. To create a new branch after merging
B. To prevent merges from happening
C. To delete merged branches automatically
D. To run commands automatically after a merge completes

Solution

  1. Step 1: Understand the role of Git hooks

    Git hooks are scripts that run at specific points in Git workflows to automate tasks.
  2. Step 2: Identify the specific timing of the post-merge hook

    The post-merge hook runs right after a merge finishes, allowing automation of follow-up tasks.
  3. Final Answer:

    To run commands automatically after a merge completes -> Option D
  4. Quick Check:

    post-merge hook = runs after merge [OK]
Hint: Post-merge hook runs after merge finishes [OK]
Common Mistakes:
  • Confusing post-merge with pre-merge hooks
  • Thinking it prevents merges
  • Assuming it creates or deletes branches
2. Which of the following is the correct way to make a post-merge hook script executable on a Unix-like system?
easy
A. chmod 777 .git/hooks/post-merge
B. chmod +x .git/hooks/post-merge
C. git hook enable post-merge
D. chmod 644 .git/hooks/post-merge

Solution

  1. Step 1: Understand file permissions for executable scripts

    To run a script as a hook, it must have executable permission, usually set with chmod +x.
  2. Step 2: Identify the correct command

    chmod +x .git/hooks/post-merge sets the executable bit without giving excessive permissions.
  3. Final Answer:

    chmod +x .git/hooks/post-merge -> Option B
  4. Quick Check:

    Make hook executable = chmod +x [OK]
Hint: Use chmod +x to make hook executable [OK]
Common Mistakes:
  • Using chmod 777 which is insecure
  • Using chmod 644 which is not executable
  • Trying git commands to enable hooks
3. Given this post-merge hook script saved in .git/hooks/post-merge and made executable:
#!/bin/sh
npm install
npm run build

What will happen after you run git merge feature-branch?
medium
A. The dependencies will install and the project will build automatically
B. The merge will fail because of the script
C. Nothing happens because post-merge hooks do not run automatically
D. Only npm install will run, not npm run build

Solution

  1. Step 1: Understand what the post-merge hook script does

    The script runs npm install to install dependencies, then npm run build to build the project.
  2. Step 2: Know when the post-merge hook runs

    It runs automatically after a successful merge, so both commands will execute in order.
  3. Final Answer:

    The dependencies will install and the project will build automatically -> Option A
  4. Quick Check:

    post-merge runs script after merge [OK]
Hint: Post-merge runs script commands after merge [OK]
Common Mistakes:
  • Thinking hooks don't run automatically
  • Assuming only first command runs
  • Believing merge fails due to hook
4. You wrote a post-merge hook script but it does not run after merging. Which of these is the most likely cause?
medium
A. The script file is not executable
B. You forgot to commit the hook script
C. The merge was done with git rebase
D. The script is saved outside the .git/hooks directory

Solution

  1. Step 1: Check script execution permissions

    If the script is not executable, Git will not run it after merge.
  2. Step 2: Understand that hooks are local and not committed

    Hooks are not shared via commits, so forgetting to commit is irrelevant.
  3. Step 3: Consider merge method and script location

    Rebase does not trigger post-merge, but the question states a merge was done. Also, hooks must be in .git/hooks.
  4. Final Answer:

    The script file is not executable -> Option A
  5. Quick Check:

    Non-executable script = hook won't run [OK]
Hint: Check if hook script has executable permission [OK]
Common Mistakes:
  • Thinking hooks are shared via commits
  • Confusing merge with rebase triggers
  • Saving script outside .git/hooks folder
5. You want to ensure that after every merge, your Python project automatically installs new dependencies listed in requirements.txt and runs tests. Which post-merge hook script is the best choice?
hard
A. #!/bin/sh git pull pytest
B. #!/bin/sh npm install npm test
C. #!/bin/sh pip install -r requirements.txt pytest
D. #!/bin/sh pip uninstall -r requirements.txt pytest

Solution

  1. Step 1: Identify the correct commands for Python dependency installation and testing

    pip install -r requirements.txt installs dependencies; pytest runs tests.
  2. Step 2: Evaluate other options for relevance

    #!/bin/sh npm install npm test uses npm commands for JavaScript, not Python. #!/bin/sh pip uninstall -r requirements.txt pytest uninstalls dependencies which is wrong. #!/bin/sh git pull pytest pulls changes again, unnecessary after merge.
  3. Final Answer:

    #!/bin/sh pip install -r requirements.txt pytest -> Option C
  4. Quick Check:

    Python post-merge = pip install + pytest [OK]
Hint: Use pip install and pytest for Python post-merge tasks [OK]
Common Mistakes:
  • Using npm commands for Python projects
  • Uninstalling dependencies instead of installing
  • Running git pull again after merge
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard