Making git auto-commit

前端 未结 18 1594
面向向阳花
面向向阳花 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:33

    It sounds like you're looking for something similar to etckeeper, which is designed to automatically check all your changest to /etc/* into git (or whatever VCS you want), but I see no reason it couldn't be used with files other than those in /etc.

    If you only want to deal with one single file, maybe that wouldn't be perfect, but if you want to keep track of everything in a given directory, I think it's worth checking into.

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

    git-wip is a great solution that works well for me. "WIP" stands for "work in progress". Every time you run 'git wip', the changes are commited to a separate branch. It can be run on the command line, but there are extensions for vim and emacs to automatically run git-wip each time a file is written.

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

    On Linux you could use inotifywait to automatically execute a command every time a file's content is changed.

    Edit: the following command commits file.txt as soon as it is saved:

    inotifywait -q -m -e CLOSE_WRITE --format="git commit -m 'autocommit on change' %w" file.txt | sh
    
    0 讨论(0)
  • 2020-11-27 09:43

    This does not satisfy the "Ideally" part of the question, but this was the closest question I saw to the answer I wanted, so I figured it could go here. My first stackoverflow post though, so apologies if I'm mistaken.

    The following script ensures automatic commits on saved changes, but does prompt the user for commit input. (I realise my scenario is a little different to git-noob's).

    
    # While running, monitors the specified directory, and when a file therein 
    # is created or edited and saved, this prompts the user for a commit message.
    # The --exclude is to avoid extra prompts for the changes made to 
    # version control directories.
    
    # requires inotify-tools
    
    inotifywait --exclude '/\..+' -m  path/to/directory -e modify -e create |
            while read path action file; do
                    gnome-terminal -e 'bash -c "cd path/to/directory; 
                                                git add *; 
                                                echo What did you just do??; 
                                                read varname; 
                                                git commit -m \"$varname\""'
            done
    
    0 讨论(0)
  • 2020-11-27 09:44

    Inotify really sounds like the right tool for the job.

    There is a tool called incron which could be exactly what you are looking for. You can specify files or folders (and event types, like "change", "create", "unlink") in something like a crontab, and a command to run when such an event occurs.

    In contrast to inotifywait (which would be the analog of the poor man's cron sleep 10;do stuff), this will catch every event, not just the first.

    I haven't used it myself, but from the documentation it doesn't look too complex to setup.

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

    This script doesn't run when a user changes the file, but it could be run as a cron job (by dropping it into an /etc/cron.* directory), which is also a reasonable solution.

    This script will iterate through your /srv/www directory (change it to wherever all of your sites are stored), add, commit, and push all files, and log everything to /var/log/gitlog.txt

    now=$(date +"%m.%d.%Y_%T")
    logfile='/var/log/gitlog.txt'
    
    for dir in /srv/www/*/
    do
    echo -e '\n' >> $logfile
    echo "autocommit $dir $now" >> $logfile
    echo "--------------------------" >> $logfile
    
    cd $dir
    git add . >> $logfile
    git commit -am "autocommit $now" >> $logfile
    git push origin master >> $logfile
    done
    
    0 讨论(0)
提交回复
热议问题