Execute a Script After Every Git Push

How to run bash script after git push

This is fairly easy. There is a script ready to run. You will have to modify .git/hooks/post-commit to find the script you need to run.

mv .git/hooks/post-commit.sample .git/hooks/post-commit
vim .git/hooks/post-commit

I found this at: git-scm.com: Git Hooks

If there is no .git/hooks/post-commit.sample file, not a problem, just create .git/hooks/post-commit from scratch (Remember to make the script executable with chmod +x), for example:

#!/bin/sh
echo look at me I am the post-commit script
pwd
# call the script you want

Do a test commit and you should see the output of the script, right before the regular output of the commit.

Run a script on git push

Take a look at this answer, create a local wrapper for git pushand you should be good to go.

Local executing hook after a git push?

From Git 1.8.2 there is a new hook invoked before the push operation: pre-push If the script returns other than 0 the push operation will be cancelled.

Mention in the release notes: https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt#L167

Sample: https://github.com/git/git/blob/87c86dd14abe8db7d00b0df5661ef8cf147a72a3/templates/hooks--pre-push.sample

Execute a command after `git push` finalize, using git hook

post-receive/update are server-side hooks, so if you have defined them locally (on where you are doing the push), they would not run anyway.

The pre-push hook should run, provided it has been defined in your own repo, and made executable, in myrepo/.git/hooks/pre-push

Automating a script to run after every git commit on team repo?

I am now running a script to check for changes everyday using launchd which does the job, although I would have preferred some kind of hook.

The launchd script in turn runs a bash script:

date >> output.txt
cd /Path/to/Git/repo

git pull
python AnalysisToRun.py
git add .
git commit -m "updated after changes"
git push

If anyone comes up with a hookier, Gitier answer I am happy to change

How to execute a command right after a fetch or pull command in git?

If you need to execute a script after a git pull on the client side (your machine), then a there is no reliable solution.

The post-merge client-side hook (mentioned here) won't be triggered by every pull (for instance, not in a pull rebase, not in case of fast-forward merge)

So an alias wrapping both commands remains a possible workaround.


Original answer (2011)

If you need that on the server side, where you would checkout the bare repo and pull from a working tree (on the server) after each push to a bare repo (on the server):

A post-receive or post-update hook is generally used for this kind of task, executed after each push.

The difference between the post-receive and post-update hooks is in the arguments: a post-receive will get both the old and the new values of all the refs in addition to their names.

The missing piece is: where that hook executes itself?

The blog post "Missing git hooks documentation" from (a great) SO contributor Mark Longair sheds some light on this:

the current working directory will be the git directory.

  • So, if this is a bare repository called “/src/git/test.git/”, that will be the current working directory
    – if this is a non-bare repository and the top level of the working tree is “/home/mark/test/” then the current working directory will be “/home/mark/test/.git/

Chris Johnsen details in his SO answer: "If –-git-dir or GIT_DIR are specified but none of -–work-tree, GIT_WORK_TREE and core.worktree is specified, the current working directory is regarded as the top directory of your working tree."

In other words, your working tree will also be the current directory (the .git directory), which almost certainly isn’t what you want.

Make sure your hook/post-receive script, once created and made executable, sets the GIT_WORK_TREE at the right path, in order for your ./sync-all pull to be executed in the right directory (i.e. not "xxx.git" one).



Related Topics



Leave a reply



Submit