3

My current git status is:

modified:   Gemfile
modified:   Gemfile.lock
modified:   ... many more files
modified:   submodule_1 (new commits)
modified:   submodule_2 (new commits)
modified:   ... many more updated submodules ...

How do I git add -u except new commits in submodules?

What I want to achieve is an equivalent of git add -u, then git reset HEAD submodule_1 submodule_2 ... submodule_n, or an equivalent of git add -u Gemfile Gemfile.lock ...all other files that are not submodules...

Nowaker
  • 12,154
  • 4
  • 56
  • 62

1 Answers1

1

Once git add -u is done, you would need to reset all submodule paths.
As documented in "List submodules in a git repository"

git config --file .gitmodules --get-regexp path | awk '{ print $2 }' | xargs git reset -- 

This would list all submodule paths and do a git reset on each one.

A similar idea, from this gist:

for i in `git config -f .gitmodules --get-regexp path | cut -d" " -f2` ; do git reset -- $i ; done

Final solution, a bashrc/zshrc funnction that does git add -u and resets submodules:

gitaddus() {
  git add -u
  git config -f .gitmodules --get-regexp path | awk '{ print $2 }' | xargs git reset -- >/dev/null
  git status
}

(There's still a room for improvements, e.g. check which submodules are already added to the index so we don't git reset these.)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @Nowaker OK, I got mixed up. I have edited my answer. – VonC Jan 13 '17 at 21:10
  • Thanks @VonC. I've made it a function and added `git add -u`. – Nowaker Jan 14 '17 at 22:30
  • @Nowaker Great! you can add it as a function, or you can make a bash script (even on Windows) called `git-addus` (no file extension), which you can then call as `git addus`. If `git-addus` is in your `$PATH` or `%PATH%`, it will be executed as a bash by Git, when called as `git addus`. This works for any script called `git-xxx`. – VonC Jan 15 '17 at 04:59