如何将更改恢复到 git 子模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10906554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I revert my changes to a git submodule?
提问by Eric
I have a git submodule (RestKit) which I have added to my repo.
我有一个 git 子模块(RestKit),我已将它添加到我的仓库中。
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?
不恢复本地修改的文件。
如何重置该子模块的内容?
采纳答案by Jamie Penney
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.
移动到子模块的目录,然后执行 agit reset --hard
将所有修改过的文件重置为其最后提交的状态。请注意,这将丢弃所有未提交的更改。
回答by theraven
If you want to do this for all submodules, without having to change directories, you can perform
如果要对所有子模块都这样做,而不必更改目录,则可以执行
git submodule foreach git reset --hard
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
git submodule foreach --recursive git reset --hard
回答by qwertzguy
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.
第一个命令完全“解除绑定”所有子模块,然后第二个命令重新检出它们。
它比其他方法需要更长的时间,但无论您的子模块处于何种状态都可以工作。
回答by checksum
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:
只需将子模块重置为它检出的状态,对于主存储库引用的提交/状态来说不是必需的。我仍然会像OP所说的那样“修改内容”。因此,为了让子模块恢复正确的提交,我运行:
git submodule update --init
Then when I do git status
, it's clean on the submodule.
然后当我这样做时git status
,它在子模块上是干净的。
回答by jiahut
do 4 steps sequential:
按顺序执行 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
回答by Sergiu Todirascu
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):
这对我有用,包括递归进入子模块(也许这就是你的 -f 不起作用的原因,因为你改变了子模块内的子模块):
git submodule update -f --recursive
回答by Jean Libera
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".
如果您的子模块有更改,请删除它们。验证您在运行“git status”时看不到任何更改。
Next, go back to the main repository and run "git submodule update --init" again.
接下来,返回主存储库并再次运行“git submodule update --init”。
回答by VonC
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
)
从 Git 2.14(2017 年第 3 季度)开始,您不必进入每个子模块来执行git reset
(如git submodule foreach git reset --hard
)
That is because git reset itself knows now how to recursively go into submodules.
那是因为 git reset 本身现在知道如何递归地进入子模块。
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)
见提交35b96d1(2017年4月21日),以及提交f2d4899,提交823bab0,提交cd279e2由(2017年4月18日)的Stefan贝勒(stefanbeller
)。
(由Junio C gitster
Hamano合并-- --在提交 5f074ca 中,2017 年 5 月 29 日)
builtin/reset: add --recurse-submodules switch
内置/重置:添加 --recurse-submodules 开关
git-reset
is yet another working tree manipulator, which should be taught about submodules.
git-reset
是另一个工作树操纵器,应该教授子模块。
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.
当用户使用 git-reset 并请求递归到子模块时,这会将子模块重置为超级项目中记录的对象名称,从而分离 HEAD。
Warning: the difference between:
警告:之间的区别:
git reset --hard --recurse-submodule
andgit submodule foreach git reset --hard
git reset --hard --recurse-submodule
和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.
是前者还将重置您的主要父存储库工作树,因为后者只会重置子模块工作树。
所以请谨慎使用。
回答by cmcginty
For git <= 2.13 these two commands combined should reset your repos with recursive submodules:
对于 git <= 2.13,这两个命令组合起来应该使用递归子模块重置您的存储库:
git submodule foreach --recursive git reset --hard
git submodule update --recursive --init
回答by David H.
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.
这适用于我们运行 GIT v1.7.1 的库,其中我们有一个 DEV 包 repo 和 LIVE 包 repo。存储库本身只不过是为项目打包资产的外壳。所有子模块。
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.
LIVE 永远不会被有意更新,但是可能会发生缓存文件或事故,使 repo 变脏。添加到 DEV 的新子模块也必须在 LIVE 中初始化。
Package Repository in DEV
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
LIVE 中的软件包存储库
Here we want to pull the changes that are committed to the DEV repository, but not unknown upstream changes.
在这里,我们要拉取提交到 DEV 存储库的更改,而不是未知的上游更改。
# Pull changes
git pull
# Pull status (this is required for the submodule update to work)
git status
# Initialize / Update
git submodule update --init --recursive