19

When a project contains submodules you need to stash/unstash all of them separately. Is there a way to do it using less actions?

This link maybe helpful:

Easy way pull latest of all submodules

It is about "pull" command but there are some ways how to iterate between all the submodules.

Community
  • 1
  • 1
Vyachaslav Gerchicov
  • 2,126
  • 3
  • 21
  • 46

2 Answers2

33

You can use foreach to run a specific git command on each sub-module. For example to apply 'git stash' to every modules use this:

git submodule foreach 'git stash'

Similarly, the following command will checkout master branch and then pull any updates from the remote source for each submodule.

git submodule foreach 'git checkout master; git pull'

Read more at: http://git-scm.com/book/en/v2/Git-Tools-Submodules

computingfreak
  • 4,604
  • 1
  • 34
  • 47
Shoaib Shakeel
  • 1,361
  • 17
  • 24
  • Could you additionally explain: `git submodule foreach "(git checkout master; git pull)&"`? This command is used for pull for each submodule. Why do they add ampersand at the end and you don't do the same thing? – Vyachaslav Gerchicov Apr 10 '15 at 09:59
  • 1
    Also `&` at the end of a shell command will force shell to execute command in background. In this case you won't see output text from `git checkout master; git pull` command – Shoaib Shakeel Apr 10 '15 at 10:16
3

Warning: before Git .24 (Q4 2019), using "git submodule foreach git stash" could lost local changes to submodules.

See commit 556895d (12 Oct 2019) by Jakob Jarmar (jarmar).
(Merged by Junio C Hamano -- gitster -- in commit bb52def, 18 Oct 2019)

stash: avoid recursive hard reset on submodules

Signed-off-by: Jakob Jarmar

git stash push does not recursively stash submodules, but if submodule.recurse is set, it may recursively reset --hard them.

Having only the destructive action recurse is likely to be surprising behaviour, and unlikely to be desirable, so the easiest fix should be to ensure that the call to git reset --hard never recurses into submodules.

This matches the behavior of check_changes_tracked_files, which ignores submodules.

Community
  • 1
  • 1
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755