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?
-
7There is no such thing as autofetch. You might consider writing a cron job though – George Hilliard Sep 07 '14 at 03:39
4 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

- 1,638
- 18
- 18
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.
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)

- 35,772
- 9
- 166
- 188
-
This works but seems like creating lags typing in the shell or using emacs once in a while – alper Jul 30 '20 at 12:52
Might be late for the party but I wrote a tool for Windows and macOS to auto-fetch multiple repositories (and other features):

- 5,361
- 3
- 34
- 51