Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn on git auto-fetch?

I have several git projects which I want to fetch everyday (in the morning for example) and checkout to the last commit (if there are no local changes of course) to the branch "origin/dev" (e.g. it may not be a master branch). So how to do this for all projects in the directory?

like image 565
Cherry Avatar asked Sep 07 '14 03:09

Cherry


3 Answers

If you are using a *nix/mac you could use the following bash script and create a cron job/launchdaemon task:

#!/usr/bin/env bash
ls -d */ | while read folder; do
    if [ -d "$folder/.git" ]; then
        cd "$folder"
        git pull # CHANGE THIS TO YOUR NEEDS
        cd ..
    fi
done
like image 104
Motine Avatar answered Oct 21 '22 11:10

Motine


If you're using zsh, you could use https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/git-auto-fetch:

Automatically fetches all changes from all remotes while you are working in git-initialized directory.

Just add to plugins:

plugins=(... git-auto-fetch)
like image 38
serv-inc Avatar answered Oct 21 '22 10:10

serv-inc


How to do this for all projects in the directory?

One way would be to (experiment in a separate local directory):

  • create a local repo in that directory
  • add all those Git repos projects as submodules (git add submodule -b dev url/git/repo/for/a/project): they will be set to track the dev branch
  • every day (with a cron job for instance), do a git submodule update --recursive --remote: that will fetch and checkout the latest from origin/dev for each submodules.

Note that the local repo in the directory act as a "parent repo" for those submodules, and is purely local: no need to push that repo. It is just there to benefit from the submodule tracking branch feature introduced in git 1.8.2+ (March 2013).

Your git project repos can ignore completely the fact they are submodules for the parent directory repo.

In one command, you trigger a fetch + checkout of the latest commits on origin/dev for all your git project repos.

like image 41
VonC Avatar answered Oct 21 '22 11:10

VonC