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?
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
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)
How to do this for all projects in the directory?
One way would be to (experiment in a separate local directory):
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With