Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Comments on Get notified when there are unmerged changes from origin

Parent

Get notified when there are unmerged changes from origin

+4
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

git log --all --graph --oneline (1 comment)
Post
+3
−0

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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

No builtin option? (2 comments)
No builtin option?
matthewsnyder‭ wrote 4 months ago

Since you are suggesting a pre-commit hook, does that mean there is no built in way in git to do this? Just curious, I think the hook method is great - thanks for sharing it.

GeraldS‭ wrote 4 months ago

Not that I'm aware of.