Use Git hooks to automatically push after every commit
A Git hook is a shell script that is invoked at specific points during some Git operations. They are documented in the Git book.
The hook we are interested in is post-commit
(documented
here). It gets run after a new commit is made. This is exactly when
we want to try and update our remote.
Our hook will:
- Execute each time you commit something into your repository.
- Avoid doing anything if the commit is made during a rebase or merge.
- Push to a remote named
origin
(or whichever one you change theUPSTREAM
shell variable to point). - Create or update a remote reference that matches the branch name that’s currently checked out.
Copy and paste the following script into a file named
.git/hooks/post-commit
in your repository directory.
IMPORTANT: Afterwards make the script runnable via
chmod +x .git/hooks/post-commit
.
Last modified: July 5, 2022