Keatinge
Keatinge

Reputation: 4321

Git merge but abort all local changes

I have a situation where I have two branches. Branch good and branch bad. I'm looking to do something like a merge. But merge isn't quite right. Essentially I want to be on bad branch and I want the files on bad branch to become a copy or clone of the files on good branch. I want to run some kind of merge such that all the files in bad branch become identical to the files in good branch. But the history shouldn't be messed with. It should just be one new commit on top of the history from bad.

I tried

git checkout bad
git merge -X theirs good

But this isn't quite right, this keeps some changes from bad. I can fix this by doing

git diff bad good

And manually deleting every diff. Essentially I keep deleting things until the diff is empty. Then the operation is complete and branch bad is now identical to branch good. Surely there must be a better way? I want to do a merge where it just completely copies the other branches files instead of merging them.

Any help would be appreciated. I've tried searching for things like copy other branch git but nothing seems relevant.

Upvotes: 1

Views: 119

Answers (3)

jthill
jthill

Reputation: 60605

git reset --hard good       # set work tree, index and parent
git reset --soft bad        # undo the parent switch
git commit

or

git read-tree -um good      # set work tree and index
git commit

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 362147

git checkout bad
git reset --hard good

This makes bad point at the same commit as good, meaning they will be absolutely identical. bad will literally be a copy of good, both in contents and in history.

Upvotes: 3

eftshift0
eftshift0

Reputation: 30317

The problem (if you want to call it that way) with doing git reset --hard is that you are not just setting the files to be as in the other branch... you are setting everything to be like the other branch... and that also covers history of bad branch. If you want to keep bad story and have a new revision where everything will look like on good branch, then this will do the trick:

git checkout --detach good
git reset --soft bad # moving HEAD to bad, but all files will stay as they are in good, all differences will be on index, ready to be committed
git commit -m "New revision, everything is like it is on good branch"
git branch -f bad # move bad branch pointer to new revision
git checkout bad

But it all depends on if you want to keep bad branch history or not.

Upvotes: 2

Related Questions