Making git auto-commit

前端 未结 18 1592
面向向阳花
面向向阳花 2020-11-27 08:41

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

相关标签:
18条回答
  • 2020-11-27 09:20

    For Windows

    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.

    0 讨论(0)
  • 2020-11-27 09:22

    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.

    0 讨论(0)
  • 2020-11-27 09:22
    #!/bin/bash
    git commit -a -m "autoupdate `date +%F-%T`"
    git push
    

    autopushing with current date and time.

    0 讨论(0)
  • 2020-11-27 09:23

    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"
    
    0 讨论(0)
  • 2020-11-27 09:26

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

    0 讨论(0)
  • 2020-11-27 09:27

    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

    0 讨论(0)
提交回复
热议问题