简体   繁体   English

Git 如何恢复到某个提交,但保留我所做的所有提交

[英]Git how to revert to a certain commit, but keep all the commits I made

Suppose I have commits ABCDEF.假设我有提交 ABCDEF。 Note that all the commits have been pushed upstream.请注意,所有提交都已推送到上游。

I want to revert to commit D, but also want to keep commits E and F in the branch.我想恢复提交 D,但也想将提交 E 和 F 保留在分支中。

So what I want to do is to create a commit G, which will have the same contents as D. In the end my branch will look like ABCDEFG, with D=G.所以我想做的是创建一个提交G,它的内容与D相同。最后我的分支看起来像ABCDEFG,D=G。

How do I do this?我该怎么做呢?

You just have to take the exact content of D , and create a commit with that content:您只需获取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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM