Post History
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...
Answer
#2: Post edited
- 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
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