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

Post History

71%
+3 −0
Q&A Get notified when there are unmerged changes from origin

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 beh...

posted 4mo ago by GeraldS‭  ·  edited 4mo ago by GeraldS‭

Answer
#2: Post edited by user avatar GeraldS‭ · 2024-07-29T09:23:28Z (4 months ago)
  • You can use the [pre-commit hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) for this.
  • Example `.git/hooks/pre-commit`:
  • ```bash
  • #!/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:
  • ```console
  • gerald@localmachine:~/test2$ git commit -m "fourth commit"
  • ERROR: local repo is behind remote!
  • ```
  • You can also configure [global hooks](https://git-scm.com/docs/git-config#Documentation/git-config.txt-corehooksPath) that affect all repositories (with the drawback that it disables the per repo hooks):
  • git config --global core.hooksPath /path/to/my/centralized/hooks
  • You can use the [pre-commit hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) for this.
  • Example `.git/hooks/pre-commit`:
  • ```bash
  • #!/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:
  • ```console
  • 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](https://git-scm.com/docs/git-config#Documentation/git-config.txt-corehooksPath) 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: Initial revision by user avatar GeraldS‭ · 2024-07-29T09:00:57Z (4 months ago)
You can use the [pre-commit hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) for this.

Example `.git/hooks/pre-commit`:

```bash
#!/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:

```console
gerald@localmachine:~/test2$ git commit -m "fourth commit"
ERROR: local repo is behind remote!
```

You can also configure [global hooks](https://git-scm.com/docs/git-config#Documentation/git-config.txt-corehooksPath) that affect all repositories (with the drawback that it disables the per repo hooks):

    git config --global core.hooksPath /path/to/my/centralized/hooks