简体   繁体   中英

Force merging in a branch in git

Currently I am on branch foo , and I want to merge changes from branch bar , which git apparently thinks is part of foo 's history, due to reasons that are complicated to explain.

Unfortunately, the consequence is that if I run

git checkout foo
git merge bar

I will get the messages Already up-to-date although there are changes in branch bar that I want to get into branch foo .

I have considered two possible solutions.

One possibility is to simply checkout bar , copy it somewhere else, checkout foo , and then manually perform the merge. This will work but I would prefer a less manual solution if such exists.

Another possibility is to do the reverse merge, that is, merge foo into bar instead. Unfortunately, when I tried this, git's automerge erases exactly those changes in bar that I want to preserve and replace them with changes from foo , which is undesirable.

How can I force git to pretend bar is in the future and foo is in the past, to allow a merge of bar into foo ?

Not a clever resolution but should work:

First, run

git diff -p foo bar <files_you_want_to_merge> > patch

to generate a path file and edit it if necessary (like delete those newer in foo than bar ).

Then run

git checkout foo
git apply patch

will 'pseudo-merge' bar to foo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM