How to turn on git auto-fetch?

2019-05-01 06:12发布

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?

3条回答
一夜七次
2楼-- · 2019-05-01 06:48

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
查看更多
Animai°情兽
3楼-- · 2019-05-01 06:51

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.

查看更多
迷人小祖宗
4楼-- · 2019-05-01 06:57

Might be late for the party but I wrote a tool for Windows and macOS to auto-fetch multiple repositories (and other features):

RepoZ

查看更多
登录 后发表回答