311

I have a git submodule (RestKit) which I have added to my repo.

I accidentally changed some files in there and I'd like to go back to the source version. In order to do that, I tried to run

Mac:app-ios user$ git submodule update RestKit

But as you can see here, this did not work as it is still "modified content":

Mac:app-ios user$ git status
...
#   modified:   RestKit (modified content)

Even

Mac:app-ios user$ git submodule update -f RestKit 

doesn't revert locally modified files.
How do I reset the content of that submodule?

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
Eric
  • 4,607
  • 2
  • 22
  • 29
  • If `git reset --hard` doesn't work, first try specifying the remote branch with `git reset --hard origin/`. – Jerry K. Nov 01 '19 at 19:38

14 Answers14

310

If you want to do this for all submodules, without having to change directories, you can perform

git submodule foreach git reset --hard

You can also use the recursive flag to apply to all submodules:

git submodule foreach --recursive git reset --hard

theraven
  • 4,035
  • 3
  • 18
  • 19
  • 10
    this works so much better for automation than trying to cd into each submodule directory. – Travis Castillo Dec 19 '13 at 23:34
  • 6
    Note that you may also want to `git submodule foreach --recursive git clean -x -f -d` – yoyo Dec 08 '16 at 18:00
  • 1
    on my machine (Windows using Git 2.22.0) I need single quotes around the second git command when using the --recursive flag or it won't work: git submodule foreach --recursive 'git clean -x -f -d ' – aatwo Jan 17 '20 at 15:50
247

A more fail-safe method than all previous answers:

git submodule deinit -f .
git submodule update --init

The first command completely "unbinds" all submodules, the second then makes a fresh checkout of them.
It takes longer than the other methods, but will work whatever the state of your submodules.

rogerdpack
  • 50,731
  • 31
  • 212
  • 332
qwertzguy
  • 10,735
  • 5
  • 53
  • 59
  • 1
    Regrettably this did not work in my case (with modified local files in a git submodule), the "update --init" command spews `error: Your local changes to the following files would be overwritten by checkout` – rogerdpack Sep 21 '16 at 20:27
  • 2
    To update specific submodule do: $ git submodule deinit -f -- and then $ git submodule update --init -- – Priyank Mar 08 '18 at 08:47
  • 2
    I tried all methods above until I got to this one. For me, this is the only one that got my git looking "clean" (without the `*` in my `PS1` that `git status -uno` was unable to explain). – Guy Rapaport Mar 11 '20 at 23:47
240

Move into the submodule's directory, then do a git reset --hard to reset all modified files to their last committed state. Be aware that this will discard all non-committed changes.

Jamie Penney
  • 8,844
  • 3
  • 26
  • 37
  • 8
    git submodule update (even without the --init) worked for me to abandon the submodule "changes" when I hadn't actually changed anything. If you go to the submodule directory and git status comes up empty, try this instead of the reset. – Eclectic DNA Jan 26 '15 at 18:22
  • 34
    `git submodule update --init` worked for me; without `--init` it didn't work at all. – Per Lundberg Apr 28 '15 at 11:07
  • Superb!! I made changes to the submodule in a repo of mine where I imported it. Andthis reverted it back to what it was supposed to be. – Noitidart Jun 17 '16 at 06:28
  • 2
    reset --hard didn't work for me, my submodule still couldn't be deinit because of local changes. – malhal Sep 03 '16 at 13:41
  • 48
    in addition to @markshiz, `git submodule update -f --init` for my case. – otiai10 Sep 13 '17 at 03:51
  • 4
    For me only `git submodule update --init --recursive` worked. – user3667089 Oct 28 '19 at 23:35
70

Well for me, having

git reset --hard

just reset the submodule to the state where it checked out, not necessary to the main's repo referenced commit/state. I'll still have "modified contents" like OP said. So, in order to get the submodule back to the corrects commit, I run:

git submodule update --init

Then when I do git status, it's clean on the submodule.

checksum
  • 5,617
  • 4
  • 31
  • 31
51

do 4 steps sequential:

git submodule foreach git reset --hard HEAD
git submodule update
git submodule foreach "git checkout master; git pull"
git submodule foreach git clean -f
pevik
  • 3,746
  • 3
  • 27
  • 37
jiahut
  • 1,297
  • 12
  • 13
33

This worked for me, including recursively into submodules (perhaps that's why your -f didn't work, cause you changed a submodule inside the submodule):

git submodule update -f --recursive
Sergiu Todirascu
  • 1,235
  • 14
  • 18
17

First try this, as others have said:

git submodule update --init

If that doesn't work, change to the submodule directory and use the following command to see if there are any changes to the submodule:

git status

If there are changes to your submodule, get rid of them. Verify that you can don't see any changes when you run "git status".

Next, go back to the main repository and run "git submodule update --init" again.

Jean Libera
  • 431
  • 4
  • 8
9

Since Git 2.14 (Q3 2017), you don't have to go into each submodule to do a git reset (as in git submodule foreach git reset --hard)

That is because git reset itself knows now how to recursively go into submodules.

See commit 35b96d1 (21 Apr 2017), and commit f2d4899, commit 823bab0, commit cd279e2 (18 Apr 2017) by Stefan Beller (stefanbeller).
(Merged by Junio C Hamano -- gitster -- in commit 5f074ca, 29 May 2017)

builtin/reset: add --recurse-submodules switch

git-reset is yet another working tree manipulator, which should be taught about submodules.

When a user uses git-reset and requests to recurse into submodules, this will reset the submodules to the object name as recorded in the superproject, detaching the HEADs.

Warning: the difference between:

  • git reset --hard --recurse-submodule and
  • git submodule foreach git reset --hard

is that the former will also reset your main parent repo working tree, as the latter would only reset the submodules working tree.
So use with caution.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
7

For git <= 2.13 these two commands combined should reset your repos with recursive submodules:

git submodule foreach --recursive git reset --hard
git submodule update --recursive --init
cmcginty
  • 101,562
  • 37
  • 148
  • 155
3

This works with our libraries running GIT v1.7.1, where we have a DEV package repo and LIVE package repo. The repositories themselves are nothing but a shell to package the assets for a project. all submodules.

The LIVE is never updated intentionally, however cache files or accidents can occur, leaving the repo dirty. New submodules added to the DEV must be initialized within LIVE as well.

Package Repository in DEV

Here we want to pull all upstream changes that we are not yet aware of, then we will update our package repository.

# Recursively reset to the last HEAD
git submodule foreach --recursive git reset --hard

# Recursively cleanup all files and directories
git submodule foreach --recursive git clean -fd

# Recursively pull the upstream master
git submodule foreach --recursive git pull origin master

# Add / Commit / Push all updates to the package repo
git add .
git commit -m "Updates submodules"
git push   

Package Repository in LIVE

Here we want to pull the changes that are committed to the DEV repository, but not unknown upstream changes.

# Pull changes
git pull

# Pull status (this is required for the submodule update to work)
git status

# Initialize / Update 
git submodule update --init --recursive
David H.
  • 329
  • 1
  • 8
3

If you want to discard all changes in the entire repository along with sub modules, you can use

git restore . --recurse-submodules

This will undo all changes made in repository and in sub modules.

Nitin Garg
  • 308
  • 1
  • 4
3

If there's changes in your submodules, then use

git submodule foreach --recursive git reset --hard

If your changes are remote submodule changes, then use

git submodule update --init

If these changes have been committed, then use

git reset --hard
git submodule update --init
Feiyu Zhou
  • 3,311
  • 22
  • 33
2

my way to reset all submodules (WITHOUT detaching & keeping their "master" branch):

git submodule foreach 'git checkout master && git reset --hard $sha1'

alex_1948511
  • 2,529
  • 2
  • 17
  • 19
0

It is simple:

cd /path/to/submodule/root
git submodule update -f --init  .

Most answers are suggesting resetting all the submodule, which I think is not the best approach as there might be legit changes in them.

A. K.
  • 26,873
  • 15
  • 44
  • 74