Stash everything in Git including all submodules?
Asked Answered
E

3

25

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.

Esma answered 10/4, 2015 at 9:15 Comment(0)
S
43

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

Sachikosachs answered 10/4, 2015 at 9:50 Comment(2)
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?Esma
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 commandSachikosachs
W
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.

Welcome answered 21/10, 2019 at 17:5 Comment(0)
A
2

I used to do what the answer above (https://mcmap.net/q/527911/-stash-everything-in-git-including-all-submodules) said, but it is not ideal really.

The reason it is not ideal is that if you don't have local changes in one of the submodules then git stash does nothing on that submodule, and when you do git stash pop you may have different stash queues that don't line up on multiple submodules.

There are workarounds, but I take a different approach:

What I have started doing, which is also not ideal (but might be more ideal) is:

git diff --submodule=diff > temp.patch
git apply -R temp.patch

And then to get back to my old state, I just go:

git apply temp.patch

And I also have *.patch in my list of files for git to ignore.

Afrikaner answered 23/12, 2022 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.