我有一个git子模块(RestKit),我已经添加到我的repo。

我不小心更改了里面的一些文件,我想回到原始版本。为了做到这一点,我试着逃跑

Mac:app-ios user$ git submodule update RestKit

但正如你在这里看到的,这并没有起作用,因为它仍然是“修改的内容”:

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

甚至

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

不恢复本地修改的文件。 如何重置该子模块的内容?


当前回答

如果子模块中有更改,则使用

git submodule foreach --recursive git reset --hard

如果您的更改是远程子模块更改,则使用

git submodule update --init

如果已提交这些更改,则使用

git reset --hard
git submodule update --init

其他回答

这对我来说是有效的,包括递归到子模块中(也许这就是为什么你的-f不起作用,因为你在子模块中改变了一个子模块):

git submodule update -f --recursive

一个比之前所有答案都更安全的方法:

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

第一个命令完全“解除”所有子模块的绑定,第二个命令则对它们进行新的签出。 它比其他方法花费的时间长,但无论子模块的状态如何,它都可以工作。

按顺序执行4个步骤:

git submodule foreach git reset --hard HEAD
git submodule update
git submodule foreach "git checkout master; git pull"
git submodule foreach git clean -f

对于git <= 2.13,这两个命令结合起来应该用递归子模块重置你的repo:

git submodule foreach --recursive git reset --hard
git submodule update --recursive --init

很简单:

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

大多数答案都建议重置所有的子模块,我认为这不是最好的方法,因为它们可能会有合法的变化。