Force merge file by file - git

喜你入骨 提交于 2020-01-23 18:54:05

问题


I have just merged two branches 'branch1' and 'branch2'. The problem is that it is now a mess with a lot of conflicts, with sometimes duplicate contents (is it possible ??).

What I want to do is to force for some files from 'branch1' to be merged against 'branch2' and conversely to force some files from 'branch2' to be merged against 'branch1', knowing I am now on 'branch1'. Is it possible ?

Update : there seems to be problem with git-merge ? Suppose I keep the branch1 version, then

echo $this->fetch('prehome.html');
        die; 

would appear two times, see :

protected function prehomepage()
    {
        $var = 'myvar';

        echo $this->fetch('prehome.html');
        die;
    }

<<<<<<< HEAD
    $this->mySmarty->assign('title', 'Primagora');

    echo $this->fetch('prehome.html');
    die;
  }

  /**
   * Generate the campaign block for the general sidebar, called through AJAX.
   */
  protected function getCampaignBlock()
  {
    $from = Utils::fromGet('from');
    echo $this->getCampaignSidebarBlock($from);
    die();
  }
=======
    /**
     * Generate the campaign block for the general sidebar, called through AJAX.
     */
    protected function getCampaignBlock()
    {
        $from = Utils::fromGet('from');
        echo $this->getCampaignSidebarBlock($from);
        die();
    }
>>>>>>> branch2

回答1:


On a merge with conflicts (if git config merge.conflictstyle diff3 is set) , you can:

  • ignore that merge and keep your original version (from branch1):
git show :2:full_path > file
# or 
git checkout-index --stage=2 -- file
  • ignore that merge and keep the other version (from branch2):
git show :3:full_path > file
# or 
git checkout-index --stage=3 -- file

From there, git add and git commit.




回答2:


You should always try to resolve conflicts. They tell you a lot about what is going on.

Netbeans can work with git which makes resolving conflicts a fairly simple task. You can resolve conflicts one by one ore choose the version you want to keep.

What do you mean by duplicate contents? It's not possible to have something marked as change if it's identical in both files.




回答3:


The file you are showing is the intermediate file in an ongoing git merge whit conflicts. The lines from

<<<<<<< HEAD

to

=======

is one version and from

========

to

>>>>>branch1

is the other. You can then either edit the file by hand and pick the version to use or run a tool to help you resolve the conflicts. The git command mergetool will guide you in the process of resolving all the conflicts https://www.kernel.org/pub/software/scm/git/docs/git-mergetool.html

git mergetool

git mergetool will most likely have to be configured to use your favorite conflict resolution program. I have found meld to be very helpful and intuitive.

Git mergetool with Meld on Windows or How to set Meld as git mergetool



来源:https://stackoverflow.com/questions/15245767/force-merge-file-by-file-git

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!