假设我有提交A-B-C-D-E-F。请注意,所有提交都已推送到上游。
我想要还原到提交D,但也想在分支中保留提交E和F。
所以我想做的是创建一个提交G,它将与D具有相同的内容。最终我的分支将看起来像A-B-C-D-E-F-G,其中D=G。
我该如何做到这一点?
我想要还原到提交D,但也想在分支中保留提交E和F。
所以我想做的是创建一个提交G,它将与D具有相同的内容。最终我的分支将看起来像A-B-C-D-E-F-G,其中D=G。
我该如何做到这一点?
您只需获取D
的确切内容,并使用该内容创建一个提交:
# choose a. or b. :
# a. 'git restore' was added for this purpose in version 2.25,
# and has options which are more explanatory (readable at least) :
git restore --source D --staged --worktree -- .
# b. for older gits, or for people enjoying more obscure commands, there is 'git read-tree' :
git read-tree D
# after that :
# confirm that the content you expect is staged, and commit :
git status
git commit
一个小心的提醒:使用带有 --worktree
选项的 git restore
命令可能会删除文件并放弃尚未存储在 git
中的文件修改,就像 git reset --hard
命令一样。
建议您在清洁的工作目录上使用此命令,或者至少检查您是否可以扔掉当前的修改。
--source
比-s
更易读。 - chorobagit checkout D; git reset --soft F; git commit -m "Reverting E and F"
。随意设置一个分支,以便您在该点上停留。 - eftshift0git restore
加入我的工具箱。 - eftshift0git reset --soft
只会将分支指针移动到 F
,但工作树和索引仍然保留着 D
中的内容。如果在此之后立即提交,你最终得到的是跟随 F
的 G
,其内容与 D
相同。 - eftshift0
git revert E F
,但它会创建两个提交来撤销 E 和 F。 - choroba