Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cleans and resets a git repo and its submodules
git clean -xfd
git submodule foreach --recursive git clean -xfd
git reset --hard
git submodule foreach --recursive git reset --hard
git submodule update --init --recursive
@nicktoumpelis
Copy link
Author

Thank you everybody for your comments!

@NaClYen
Copy link

NaClYen commented May 3, 2021

Git 2.14+ supports reset of the repo and submodules in one line: git reset --hard --recurse-submodule

Awesome information!! Quite simple solution.

@yuzhichang
Copy link

I revised the script:

#!/bin/bash
#Cleans and resets a git repo and its submodules
#https://gist.github.com/nicktoumpelis/11214362
git reset --hard
git submodule sync --recursive
git submodule update --init --force --recursive
git clean -ffdx
git submodule foreach --recursive git clean -ffdx

@sambacha
Copy link

sambacha commented Jul 6, 2022

I revised the script:

#!/bin/bash
#Cleans and resets a git repo and its submodules
#https://gist.github.com/nicktoumpelis/11214362
git reset --hard
git submodule sync --recursive
git submodule update --init --force --recursive
git clean -ffdx
git submodule foreach --recursive git clean -ffdx
git config --global alias.rinse '!git submodule foreach --recursive git clean -xfd && git submodule sync --recursive && git restore . --recurse-submodules'

@lkwg82
Copy link

lkwg82 commented Sep 12, 2022

git reset --hard --recurse-submodule

use this (see above)

@yuzhichang
Copy link

git config --global alias.rinse '!git reset --hard --recurse-submodule && git submodule sync --recursive && git submodule update --init --force --recursive && git clean -ffdx && git submodule foreach --recursive git clean -ffdx'

@lucasrangit
Copy link

git restore

@sambacha can you elaborate on why you added git restore? What's the use-case?

@sambacha
Copy link

```shell
git restore

@sambacha can you elaborate on why you added git restore? What's the use-case?

It resets the submodules back

@lucasrangit
Copy link

lucasrangit commented Sep 19, 2022

git restore . --recurse-submodules

It resets the submodules back

Does it also take care of the git reset --hard --recurse-submodules to discard any changes in the working trees? If so, why do the git restore last and not first?

@hhe
Copy link

hhe commented Dec 2, 2022

The key for me was git submodule sync. Thank you @larsbrinkhoff @yuzhichang!

It's a sneaky issue whenever the URLs specified in .gitmodules get updated, but the old URLs continue to silently be used (as shown by git submodule foreach --recursive git remote get-url origin). Git should honestly warn us whenever the two are different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment