foerever
foerever

Reputation: 335

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. 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)>. branch has commits <X(N-2+1)>, <X(N-2+2)> because it was started when master was still at <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. (2) Additionally, I want my diff for branch to show 0 files changed. I just want a new commit <X(N-2+3)> that completely replaces my branch state with <X(N)> state.

In order to do that I want to replace all my local changes with the <X(N)> state and push without --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.

Upvotes: 1

Views: 120

Answers (2)

G. Sliepen
G. Sliepen

Reputation: 7973

You can create a diff between branch and master, and apply that as one single commit onto 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.

Upvotes: 1

Mark Adelsberger
Mark Adelsberger

Reputation: 45649

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.

Upvotes: 2

Related Questions