1

I have a number of branches in my repo. I want to loop thought the each branch, checkout the branch and run a command that update all packages and commit the change. In the end I want to push all.

So for I figure out how to loop thought the branch

for BRANCH in `git branch -a | grep remotes/origin/*` ;

do
    A="$(cut -d'/' -f3 <<<"$BRANCH")"
    echo $A

done 

Not sure if it is the best way of doing this. However I am still struggling how to git checkout the branch and then proceed with automatic commit.

Any ideas?

UPDATE

Based on @bk2204 answer I've done two more additional things: Figure out how to run the script withing a context of another folder https://stackoverflow.com/a/10566581/2926340 and then how to clone all remote branches automatically https://stackoverflow.com/a/40318872/2926340

That is what I came up with:

#!/bin/sh
cd my-folder #<-- tight to a specific folder
:

set -e

[ -z "$(git status --porcelain)" ] || { echo "working tree not clean" >&2; false; }

#for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
   #git branch --track ${branch#remotes/origin/} $branch #<-- throws an error if was already cloned
#done

for branch in $(git for-each-ref refs/heads/* | cut -d"$(printf '\t')" -f2 | cut -b12-)
do
    echo $branch
    # This will overwrite any changes.
    git checkout -f $branch
    # Run command here.

    rm -rf node_modules
    npm i
    # run my update thing
    git add .
    git commit -m "Update packages"
done

It is works but now it has two issues:

1) the script only runs in specific folder, so I always have to alter the path to the folder if I want to run it in a different folder
2) if I already pull all branches #git branch --track ${branch#remotes/origin/} $branch throws an error.

How would you address these issues to be able to run script on any folder and to be able to handle the case if particular remote repo was being already cloned?

Sergino
  • 10,128
  • 30
  • 98
  • 159
  • Aside: `-f3` is definitely wrong: what happens when the remote-tracking names include `remotes/origin/group/b1` *and* `remotes/origin/group/b2`? You can use `-f3-`, but it's probably better to use `git for-each-ref --format= refs/remotes/origin` here. – torek Apr 22 '20 at 05:00
  • If you don't want it to always run in a specific folder, don't put the `cd` in your script. And if you want to run `git branch --track` multiple times and overwrite the old branch, use `-f`. – bk2204 Apr 22 '20 at 17:48
  • @bk2204 yeah I was trying to avoid `cd` but this solution didn't help https://superuser.com/a/510180/534199 when running `(cd /wherever/ ; sh script.sh)` getting an error `sh: script.sh: No such file or directory` – Sergino Apr 22 '20 at 19:42
  • @bk2204 when using `-f` on existing branches got an error `fatal: Cannot force update the current branch.` – Sergino Apr 22 '20 at 19:47
  • 1
    You need to skip the current branch, change to a different branch, or use `git reset --hard` to modify the current branch. I think by this point, your original question has been answered, and if you have further questions, a new question is appropriate, since we can't really continue to respond in comments indefinitely. – bk2204 Apr 22 '20 at 19:53

1 Answers1

1

git branch isn't designed to be scripted, so you're probably going to want to do something a little different. Maybe something more like this will be helpful:

#!/bin/sh

set -e

[ -z "$(git status --porcelain)" ] || { echo "working tree not clean" >&2; false; }

for branch in $(git for-each-ref refs/heads/* | cut -d"$(printf '\t')" -f2 | cut -b12-)
do
    # This will overwrite any changes.
    git checkout -f $branch
    # Run command here.
    git add .
    git commit -m "Update packages by running command"
done

Note that you can place a literal tab in the cut invocation, but I thought I'd make things a little more resistant to cut-and-paste errors.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • I did try your suggestion and it works but I've done some basic modifications that I am not really happy about (plz see update), any ideas how to fix these? – Sergino Apr 22 '20 at 09:54