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
@siimsoni

This comment has been minimized.

Copy link

siimsoni commented Apr 20, 2017

    If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i. Git will refuse to delete directories with .git sub directory or file unless a second -f is given.

Use force twice to clean directorise with .git subdirectories: git clean -xfdf. I had some tangling submodules that would not get deleted with just git clean -xfd.

@zippo227

This comment has been minimized.

Copy link

zippo227 commented Jul 14, 2017

this is nice. thanks!

@vSanjo

This comment has been minimized.

Copy link

vSanjo commented Dec 29, 2017

Now this is a handy, awesome set of commands!
Not especially destructive, just does a little house-keeping post-commit.

Excellent, thank you.

@larsbrinkhoff

This comment has been minimized.

Copy link

larsbrinkhoff commented Mar 8, 2018

Maybe git submodule sync?

@Davidius86

This comment has been minimized.

Copy link

Davidius86 commented May 21, 2019

If you do have a script that checks out a git repo or you have removed submodules this script fails. I also would reorder the cleanup to the end in case of submodule changes. That's what I have now:

#! /bin/sh

git reset --hard
git submodule foreach --recursive 'git checkout -- . || :'
git submodule update --init --recursive
git clean -d -f -f -x
git submodule foreach --recursive git clean -d -f -f -x

@Davidius86

This comment has been minimized.

Copy link

Davidius86 commented Nov 12, 2019

I today faced the issue that I had staged files in submodules. To get this solved the full script is:

#! /bin/sh

git reset --hard
git submodule foreach --recursive 'git reset HEAD . || :'
git submodule foreach --recursive 'git checkout -- . || :'
git submodule update --init --recursive
git clean -d -f -f -x
git submodule foreach --recursive git clean -d -f -f -x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.