简体   繁体   English

如何在完全保留分支历史记录的同时将分支状态重置为 master?

[英]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 :您可以在branchmaster之间创建一个差异,并将其作为单个提交应用到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.

相关问题 GIT:如何用Master覆盖质量检查分支,同时保留质量检查分支的历史记录? - GIT: How to overwrite QA branch with Master, preserving QA branch history? 如何丢弃分支中的目录并将其重置为master分支的状态? - How to discard a directory in my branch and reset it to the master branch's state? git如何通过完全覆盖主分支将分支合并到主分支 - git How to merge a branch into the master branch by completely overwriting master branch 如何用主分支历史覆盖开发分支 - How to overwrite the development branch with the master branch history 如何将develop分支重置为master - how to reset develop branch to master 如何在保留原始提交历史记录的同时将分支重新建立为master? - How to rebase a branch to master while keeping the original commit history? 如何将现有GIT项目导入SVN,在主分支上保留GIT历史记录? - How do I import an existing GIT project into SVN, preserving GIT history on master branch? 如何将master分支历史记录重写为其他Repository master分支 - How to rewrite the master branch history to other Repository master branch 将主分支重置为功能分支 - Reset master branch to feature branch 如何使用GIT将&#39;master&#39;分支完全重置为&#39;dev&#39;的内容? - How do I use GIT to completely reset the 'master' branch to the contents of 'dev'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM