1

My usual git workflow is

git checkout -b branch_name
.... <do my thing>
git commit -am 'some comment'
git fetch
git merge origin/develop
.... <do some more things>
git commit -am 'more things'
git push

Unfortunately the repo I'm working on has submodules (which I never modify, but others do). If I do the above and if the merge updated submodules, the second commit pulls in the old version of the submodules and I end up with unintended submodule changes in my pull request.

I know I can just do git submodule update --init --recursive after the merge. But it's fairly easy to forget, and I don't always want to do this (e.g. when just jumping to another branch to change something and jumping back)

My Question

Is there some alternative to git commit -am 'blahblah' which commits everything OUTSIDE of submodules?

I realize I'll have to update them for my code to work properly. But I just don't want to have the problem of accidentally committing them.

I'd also like to avoid automatically update them when merging/switching branches - as this makes it slower and more cumbersome to jump around.

2 Answers 2

1

So, you want to carry outdated submodule checkouts but add everything else? Git doesn't have a one-letter commit option to just do that, but it's easy enough to script,

git ls-files -s $(git ls-files -m) | grep -v ^16 | cut -f2-

will list all modified-non-submodule paths, you can add or explicitly commit those.

So

git commit -m blahblah $(
        git ls-files -s $(git ls-files -m) | grep -v ^16 | cut -f2-
)

wrapped up however you like should do it.

1

I know I can just do git submodule update --init --recursive. But it's fairly easy to forget, and I don't always want to do this (e.g. when just jumping to another branch to change something and jumping back)

You can do that automatically by creating a pre-commit hook (a script that runs before committing).

As this runs only before committing, you don't need to think about this and it does not happen if you just switch branches etc. However, committing with this script will be slower.

In order to do that, navigate to the hooks folder in your .git directory.

There, you create a file named pre-commit with the following content:

#!/bin/bash
git submodule update --init --recursive

This automatically updates all submodules in that repository before committing.

This script should work on linux and windows as git for windows comes with bash. If it does not work, you can try to point use the full path to cmd/git bash instead of /bin/bash. I think it might also be faster to use cmd on windows.

If you want to commit without updating the submodules, you can add --no-verify to the git commit command.

See that chapter in Git Pro for details.

Not the answer you're looking for? Browse other questions tagged or ask your own question.