0

If I have a src folder with multiple git repos inside of it, maybe something like,

/src
  /repo1
  /repo2
  ...

Maybe some of them have some uncommited/unstaged edits. I was looking for a command that would run git pull on all of them, regardless of their changes.

I know a command like find . -mindepth 1 -maxdepth 1 -type d -print -exec git -C {} pull \; will just do a git pull, but how would I throw a git checkout . in there? Maybe something with a git reset --hard origin/master?

granduser
  • 67
  • 7
  • 2
    Wrap all the commands in a script. – Guildenstern May 23 '23 at 19:11
  • `git reset --hard @` or `git reset --hard "@{u}"`. `git pull --autostash origin @` – phd May 23 '23 at 19:12
  • Guildenstern gives good advice, and you should certainly wrap the commands in a script. But it is easy to run multiple commands in an exec: `find ... -execdir sh -c 'git reset --hard @{u}; git pull' \;` (But it makes more sense to do `git fetch; git reset --hard @{u}`) WARNING: `git reset --hard` is destructive and may discard data. – William Pursell May 23 '23 at 19:33

1 Answers1

2

Tasks like that can be easily automated with a shell loops:

cd src
for dir in */; do                # */ makes sure we are only looking at directories
  cd "$dir"                      # go into working directory
  git fetch                      # sychronize local history
  git reset --hard origin/master # !!! WILL WIPE ALL LOCAL CHANGES !!!
  cd -                           # go back to previous directory
done
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Add a `[[ test -d objects -a -d refs -a -f HEAD ]] || continue` in there and recurse with `find` to find submodules and such, see [here](https://stackoverflow.com/a/46758507/1290731) for a full starter kit, you could maybe add `-depth` to get the submodules fetched first. Like you I'm not sure op really knows that pull isn't fetch. – jthill May 23 '23 at 20:32
  • @jthill sure, the script can be as simple or as complex as required. The OP didn't say anything about submodules, so this (simple) answer assumes plain, non-bare Git repos without submodules. – knittl May 24 '23 at 05:44
  • worth mentioning for the OP: perhaps `git fetch` is enough -- you can then check on a per repo basis if you want to `git merge @{u}`, `git rebase @{u}` or `git reset @{u}` ... – LeGEC May 24 '23 at 06:28