Comments on Get notified when there are unmerged changes from origin
Parent
Get notified when there are unmerged changes from origin
Sometimes I work with Git repos that have some changes fetched but not yet merged to the local branch. Occasionally I make commits without noticing this, which necessitates rebase/conflict resolution.
I would like to be notified when I'm working with a Git repository such as this. Is there a way to configure either Git or my shell (fish) to automatically print a message like below?
Warning: Local branch is not even with upstream, merge changes to rectify.
It doesn't have to be a message, just any reasonable way of notifying me will do.
Post
You can use the pre-commit hook for this.
Example .git/hooks/pre-commit
:
#!/usr/bin/bash
git fetch
if [ $(git status -sb |grep -c behind) -gt 0 ]; then
echo "ERROR: local repo is behind remote!"
exit 1
fi
Result:
gerald@localmachine:~/test2$ git commit -m "fourth commit"
ERROR: local repo is behind remote!
The hooks can be bypassed with the --no-verify
argument if needed.
You can also configure global hooks that affect all repositories (with the drawback that it disables the per repo hooks):
git config --global core.hooksPath /path/to/my/centralized/hooks
1 comment thread