[英]How to reset branch state to master while completely preserving branch history?
Essentially I want to reset my branch state to master without deleting its history and without appending the history of master.本质上,我想将我的分支状态重置为 master 而不删除其历史记录并且不附加 master 的历史记录。 I want to reset state of the branch in a single commit.我想在一次提交中重置分支的状态。
Let's say master
is in state A. And the branch
is in state B. master
has commits <X1>
, <X2>
, <X3>
, ... , <X(N-2)>
, <X(N-1)>
, <X(N)>
.假设master
处于状态 A. branch
处于状态 B. master
有提交<X1>
, <X2>
, <X3>
, ... , <X(N-2)>
, <X(N-1)>
, <X(N)>
。 branch
has commits <X(N-2+1)>
, <X(N-2+2)>
because it was started when master
was still at <X(N-2)>
. branch
有提交<X(N-2+1)>
, <X(N-2+2)>
因为它是在master
仍然在<X(N-2)>
。
(1) I want branch
to match state at <X(N)>
without having <X(N-2)>
, <X(N-1)>
, <X(N)>
appended onto my commit history. (1)我希望branch
匹配<X(N)>
处的状态,而无需将<X(N-2)>
、 <X(N-1)>
、 <X(N)>
附加到我的提交历史记录中。 (2) Additionally, I want my diff for branch
to show 0 files changed. (2)此外,我希望我的branch
差异显示 0 个文件已更改。 I just want a new commit <X(N-2+3)>
that completely replaces my branch state with <X(N)>
state.我只想要一个新的提交<X(N-2+3)>
用<X(N)>
状态完全替换我的分支状态。
In order to do that I want to replace all my local changes with the <X(N)>
state and push without --force.为了做到这一点,我想用<X(N)>
状态替换我所有的本地更改,并在没有 --force 的情况下推送。 I haven't been able to find a solution online for this kind of thing without the side effects of (1) and (2) as well as having to force push which to me seems to dangerous.我一直无法在没有(1)和(2)的副作用的情况下在线找到这种事情的解决方案,并且不得不强制推动这对我来说似乎很危险。
Well... I suppose you could嗯...我想你可以
git checkout branch
git rm -r :/:
git checkout master -- :/:
git add :/:
git commit -m "reset state to match master"
I'm not sure what you mean about the diff
, but given that you've already described the target state, the diff
is a given - it's going to be the difference between the state you described, and whatever you're comparing against with diff
.我不确定您对diff
意思,但是鉴于您已经描述了目标状态,因此diff
是给定的 - 这将是您描述的状态与您要比较的状态之间的差异diff
。
You can create a diff between branch
and master
, and apply that as one single commit onto branch
:您可以在branch
和master
之间创建一个差异,并将其作为单个提交应用到branch
:
git diff --binary HEAD..master | git apply --index
This updates the working directory and the index.这将更新工作目录和索引。 You can then git commit
the new state.然后你可以git commit
新状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.