webworm
webworm

Reputation: 11019

Bring remote branch into Git and then add back my changes without creating new branch

I need to merge code from a remote branch into my working branch. The merge is so complicated I feel more comfortable bringing in the remote branch and then adding back my code. Is there a way to "stash" my changes so that I can add them back without having to create another new branch just to merge the changes?

Upvotes: 0

Views: 31

Answers (2)

alextercete
alextercete

Reputation: 5171

I think what you're after is:

$ git fetch
$ git merge --strategy recursive -Xours origin/<branch-name>

This will merge the changes from the remote branch origin/<branch-name> into the current branch and, if there are any conflicts, your changes will "win".

Another thing you can do is perform a regular Merge and then resolve the conflicts by picking your version for each conflicting file:

$ git checkout --ours -- path/to/file

AFAIK, there isn't a straightforward way to "stash" changes that have been already committed. Even if there was, you would still have to somehow fix the conflicts when applying the stash back to the working directory.

Upvotes: 2

Dan O&#39;Leary
Dan O&#39;Leary

Reputation: 2832


You can use the

 git stash

command to do exactly that. The documentation is here: https://git-scm.com/docs/git-stash

Upvotes: 0

Related Questions