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...
44 Answers
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.
2The 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 otherbranchwill show no changes
- e.g.
- 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 If you would like simply to bring all files from another branch to your working directory, you can simple check them out by using the checkout subcommand of git.
git checkout target # checkout my local branch
git checkout otherbranch -- .The subcommand checkout allows you also to retrive files from another branch. For example git checkout otherbranch -- file.txt retrieves file.txt from branch otherbranch. Using . instructs Git to retrieve to full content from the given branch.
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.