Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to add -A/commit main/all submodules

By using git add -A and git commit -a, I can obviously add/commit all changes to the repo I'm currently situated in. However, is there a way to include all submodules in an add/commit and apply the same commit message to each?

like image 596
Qix - MONICA WAS MISTREATED Avatar asked Dec 07 '12 20:12

Qix - MONICA WAS MISTREATED


2 Answers

You can use an alias. Make script: e.g. ~/supercommit.sh

#!/bin/bash -e
if [ -z $1 ]; then
    echo "You need to provide a commit message"
    exit
fi

git submodule foreach git add -A .
git submodule foreach git commit -am "$1"

git add -A .
git commit -am "$1"

And mark it executable (chmod +x). Now, create an alias:

git config alias.supercommit '!~/supercommit.sh "$@"; #'

That should do (I'll test it in a bit)

like image 93
sehe Avatar answered Oct 21 '22 14:10

sehe


Well, this was asked and answered 9 years ago, and it helped me today when I was looking for the same thing. However, I faced some issues getting it working so I am posting this in order to help anyone else who may face the same issue.

issues :

  • fails with "line 2: [: too many arguments"
  • fails if any submodule has no changes

fixes :

  • add quotes (") around $1 on line 2, without which a comment with multiple words is treated as multiple arguments
  • check if submodule has staged changes before committing

updated code

#!/bin/bash -e
if [ -z "$1" ]; then
    echo "You need to provide a commit message"
    exit
fi

git submodule foreach "
    git add -A .
    git update-index --refresh
    commits=\$(git diff-index HEAD)
    if [ ! -z \"\$commits\" ]; then
        git commit -am \"$1\"
    fi"

git add -A .
git commit -am "$1"
like image 33
Kaleshwar Chand Avatar answered Oct 21 '22 13:10

Kaleshwar Chand