1

Suppose I have a master branch and a feature branch. feature is severely outdated and I now want to make it exactly like master, but I want all changes made to feature that were required in order to complete the "merge" to appear in a single commit.

First I tried this:

git checkout feature
git merge --squash master

But this gives me a squillion conflicts. I don't want to manually resolve conflicts, I want to simply take whatever is in master and be done with it. So I do this:

git checkout feature
git merge --squash -X theirs master

With this I still get merge conflicts (where files were deleted in master. What do I do now? I could do git reset --hard master but then I don't get a commit message that shows the changes.

Any ideas? I'm using git 1.8.1.2.

love
  • 1,000
  • 2
  • 16
  • 35
quant
  • 21,507
  • 32
  • 115
  • 211
  • Could you clarify what you mean by _"but I want all changes made to feature to appear in a single commit"_? Do you want to keep or discard the work currently in `feature`? – 200_success Aug 22 '14 at 05:05
  • @200_success I mean that I would like all the changes that had to be made in order to make `feature` like `master` to appear in a single commit, like with a `merge --squash`. – quant Aug 22 '14 at 05:09
  • The important question is *why* do you want it? – Jan Hudec Aug 22 '14 at 05:20
  • possible duplicate of [git command for making one branch like another](http://stackoverflow.com/questions/4911794/git-command-for-making-one-branch-like-another) – Jan Hudec Aug 22 '14 at 05:25
  • The thing is, you are dropping the changes you made on feature so far and starting again with content of master. So just drop the commits too (`git reset --hard master`); they are not contributing to the project anyway. – Jan Hudec Aug 22 '14 at 05:27
  • @JanHudec say you've got a branch *formatting changes* where you want to do only changes related to formatting. – quant Aug 22 '14 at 06:02
  • @Arman: That does not explain why you want to keep the history that didn't contribute to the project in the end. – Jan Hudec Aug 22 '14 at 07:15

1 Answers1

1

See the answers to git command for making one branch like another. Note that there's rarely any good reason to do this in the first place (as already noted as the first comment on that question).

There's another method for doing this, not listed there. This assumes you're in the top level directory of your repository:

git checkout feature
git merge --no-commit -s ours master # -s ours just to make things quiet
git rm -q -rf .           # remove all resulting files
git checkout master -- .  # replace with master branch version
git commit

In fact, you don't need the git merge command at all; here's a variant without it:

git checkout feature
git rm -q -rf .; git checkout master -- .
git rev-parse master > .git/MERGE_HEAD
git commit

(the difference is that the last git commit will give you a different starting message—using git merge --no-commit leaves a MERGE_MSG file in the git directory as well, to provide the commit message).

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775