I\'d like to use git to record all the changes to a file.
Is there a way I can turn git \'commit\' on to automatically happen every time a file is updated - so ther
According to this article about autocommit, you shou create .bat
file with content:
git add -u
git commit -m "your commit message"
git push origin master
and execute with Task Scheduler
. If you don't know how to do step-by-step, refer that article.
The earlier inotifywait answer is great, but it isn't quite a complete solution. As written, it is a one shot commit for a one time change in a file. It does not work for the common case where editing a file creates a new inode with the original name. inotifywait -m apparently follows files by inode, not by name. Also, after the file has changed, it is not staged for git commit without git add or git commit -a. Making some adjustments, here is what I am using on Debian to track all changes to my calendar file:
/etc/rc.local:
su -c /home/<username>/bin/gitwait -l <username>
/home/<username>/bin/gitwait:
#!/bin/bash
#
# gitwait - watch file and git commit all changes as they happen
#
while true; do
inotifywait -qq -e CLOSE_WRITE ~/.calendar/calendar
cd ~/.calendar; git commit -a -m 'autocommit on change'
done
This could be generalized to wait on a list of files and/or directories, and the corresponding inotifywait processes, and restart each inotifywait as a file is changed.
#!/bin/bash
git commit -a -m "autoupdate `date +%F-%T`"
git push
autopushing with current date and time.
In case anyone tries to do this from Powershell I had success using the following:
& "C:\Program Files (x86)\Git\bin\sh.exe" -c "cd D:\PATH\TO\REPO && git add --all && git commit -m 'Automatic Commit Message Goes Here' && git push origin master"
i had the same problem and on mac launchd provides you with a great solution. it will watch a file or a directory and if there are changes you can run an app or anything else...
I wanted to do this in windows, and found the best way was to use Directory Monitor to check for changes then when it detected a change have it run:
Program: cmd.exe
Params: /C C:\pathToBatchFile.bat
That batch file contained:
c:
cd c:\gitRepoDirectory\
(if exist "%PROGRAMFILES(X86)%" (
"%PROGRAMFILES(X86)%\git\bin\sh.exe" --login -i -c "git commit -am AutoCommitMessage"
) else (
"%PROGRAMFILES%\git\bin\sh.exe" --login -i -c "git commit -am AutoCommitMessage"
))
I also tried having another command in there to add files ("%PROGRAMFILES(X86)%\git\bin\sh.exe" --login -i -c "git add *.*"
), but I don't think I got that working properly.
I also made a post-commit hook containing:
#!/bin/sh
git.exe pull -v --progress "origin"
git.exe push --progress "origin" master:master
curl.exe -s https://webserverdomain.com/updateFromGitHook.x?r=repoName
(If there were any conflicts then it would abort the pull and abort the push, but there wasn't any clear way to tell that had happened - in the end we abandoned the whole idea because of this one flaw.)
That curl command told my server that it needed to do a pull on the code. All that was needed to handle it in php was:
<?
$r = $_GET['r'];
if (!empty($c)) {
//use system instead of exec if you want the output to go back to the git client
exec("cd /path/to/repo/parent/$r; sudo git reset --hard HEAD; sudo git pull;");
echo "\n\nServer: Updated\n\n";
} else {
echo "\n\nServer: UPDATE FAILED\n\n";
}
?>
The only problem with that was it needed to be run by the root user instead of the apache user, so I also had to make a file in /etc/sudoers.d/
containing:
www-data ALL = NOPASSWD: /usr/bin/git
For me, I think that worked pretty solidly. Directory Monitor can be configured to run on startup and start minimized, and it can watch several different folders