3

I just wanted to run a git hook(post-merge) to verify what are all the changes happened in the recent pull.

this is my post-merge script

#/usr/bin/env bash

echo "======> following are changes made to local repo <======"

git fetch && git log ..origin/master --pretty=format:"%s - %ar by %an (%h)"

echo "=======> ****************** <========"

I have given necessary file permission chmod +x post-merge

command git fetch && git log ..origin/master --pretty=format:"%s - %ar by %an (%h)" runs perfectly when i ran it manually.

But when i do a git pull origin master it is showing only

echo "======> following are changes made to local repo <======"
echo "=======> ****************** <========" 

Because git pull performs both git fetch and git merge i tried with

#/usr/bin/env bash

    echo "======> following are changes made to local repo <======"

    git log ..origin/master --pretty=format:"%s - %ar by %an (%h)"

    echo "=======> ****************** <========"

Where i am going wrong?

git version 1.9.1

Thanks

Aparichith
  • 1,452
  • 4
  • 20
  • 40

1 Answers1

5

that because post-merge is executed after git pull, that is when post-merge is executed, you HEAD is the same with origin/master, so you got empty output.

try this:

#/usr/bin/env bash

echo "======> following are changes made to local repo <======"

git fetch && git log ORIG_HEAD..origin/master --pretty=format:"%s - %ar by %an (%h)"

echo "=======> ****************** <========"

the key is ORIG_HEAD, which is the last value of HEAD before dangerous operation (includes merge)

for more infomation about ORIG_HEAD read HEAD and ORIG_HEAD in Git

Community
  • 1
  • 1
leo108
  • 817
  • 5
  • 12
  • i am new to this concept can you explain it little bit? – Aparichith Dec 11 '14 at 11:24
  • `that because post-merge is executed after git pull, that is when post-merge is executed` i wanted to check what are all changed after pull. this means after merge only i need to do this right? because git pull will do both git fetch and git merge right? – Aparichith Dec 11 '14 at 11:27
  • @h.APP.y `git pull will do both git fetch and git merge` --> yes. – leo108 Dec 11 '14 at 12:02
  • so how should i over come that? – Aparichith Dec 11 '14 at 12:19
  • @h.APP.y check my latest answer – leo108 Dec 11 '14 at 12:23
  • if i want to store the changed file names in a variable and want to send through mail server `changed_files = \`git fetch && git log ORIG_HEAD..origin/master --pretty=format:"%s - %ar by %an %h"\`` and sending through `mailx` `echo "demo code alert" | mailx -s "commits pulled to server" abcd@example.com` but it is returning me changed file not found error how to assign values to a shell variable? – Aparichith Dec 12 '14 at 05:42
  • 2
    @h.APP.y `changed_files=$(git fetch && git log ORIG_HEAD..origin/master --pretty=format:"%s - %ar by %an %h")` there is no space around `=` – leo108 Dec 12 '14 at 06:14