2

I don't want to commit anything. I just want to take all the files from a different branch and replace all the current files I'm working on with those, while it doesn't change my current branch at all, essentially like a merge, but without creating that merge state.

The end result would be the same as checking out a different branch, copying the files, then going back to original branch, and just pasting on top of them.

EDIT: I tried merging the branch into the working tree so it was in the merge state, then doing a git reset originalbranch, and that nearly worked, but it left all the conflicted files with the diff comments...

EDIT2: Just realized I was on SU and not SO...

  • Why? What is your goal? – Raystafarian Dec 24 '13 at 23:58
  • I have a base branch. It's forked with every project. Throughout the project I've updated some of the base files. I don't know/care which commits (they're bundled with many other irrelevant changes), but I want to look through the final project files diffed with base and choose what I want to commit to the base individually, file per file. – phazei Dec 25 '13 at 0:21
  • Would a code audit or file comparison between base and project identify the changes? Then you could commit those files back to base, or am I misunderstanding? – Raystafarian Dec 25 '13 at 0:26
  • That's basically what I'm going for trying to get the project files into the base branch. There are hundreds of changes in many different files. I want to hand pick them, they aren't only from individual commits, and some are just pieces of commits. I use smartGit and it will give me the files modded, I just need to get them into the working tree. – phazei Dec 25 '13 at 0:28
4

This seems to do the trick:

git merge otherbranch --no-commit --no-ff -X theirs
git reset currentbranch

"-X theirs" is needed since "--strategy=theirs" isn't a valid strategy.

"--no-commit --no-ff" prevents the merge from going through just leaving the files in the working tree

And the reset cancels the merge state, but leaves the files.

  • -X theirs is not the opposite of --strategy=ours. The two options do very different things. The question is asking for the functionality of --strategy=theirs, but your answer does not provide that. What your answer does it tell git to try and automatically merge all files from otherbranch into currentbranch. If and only if there are conflicts will the files from "theirs" be peferred. But after doing this type of merge git diff otherbranch currentbranch will still show lots of changes. See my answer below for an alternative. – BrianHVB May 15 at 15:14
0

It may be possible to git rebase your branch. This merges in changes without creating a new merge commit. It only works if there are no conflicting changes between the two branches.

0

The end result would be the same as checking out a different branch, copying the files, then going back to original branch, and just pasting on top of them.

Option 1

This is exactly the case for checkout. We tend to think that checkout is only used to get a branch to work on, but it can also be used to replace files in a current branch. Assuming that you want to replace the contents of currentbranch with the contents of otherbranch (e.g. copying all files of otherbranch and pasting them to currentbrach), then

git checkout currentbranch
git checkout otherbranch .

The command says checkout all files (.) from otherbranch and put them on top of currentbranch. Note, that unlike the copy/paste analogy, this will also remove any files in currentbranch that are not present in otherbranch. The downside is that you will lose all of the commit histories from otherbranch.

Option 2

The below sequence will:

  • List item
  • make the contents of currentbranch identical to otherbranch
    • e.g. git diff currentbranch otherbranch will show no changes
  • preserve the commit history of (otherbranch commits will be added to currentbranch)
  • show the change as a merge (so that it can be reverted)
  • leave otherbranch unchanged (skip making a temporary branch if this isn't needed)
   git checkout otherbranch
   git checkout -b temp-merge-other
   git merge currentbranch -s ours
   git checkout currentbranch
   git merge temp-merge-other --no-ff
   git diff otherbranch      # no difference
   git log           # commit history from dev is included
   git branch -D temp-merge-other

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.