Resolving a merge conflict using the command line

本文介绍如何使用命令行解决Git中因竞争性行更改导致的合并冲突。通过具体步骤指导如何定位冲突文件、查看冲突标记、选择保留的更改,并最终提交解决后的合并。

Competing line change merge conflicts

To resolve a merge conflict caused by competing line changes, you must choose which changes to incorporate from the different branches in a new commit.

For example, if you and another person both edited the file styleguide.md on the same lines in different branches of the same Git repository, you'll get a merge conflict error when you try to merge these branches. You must resolve this merge conflict with a new commit before you can merge these branches.

  1. Open Terminal.

  2. Navigate into the local Git repository that has the merge conflict.

    cd REPOSITORY-NAME
  3. Generate a list of the files affected by the merge conflict. In this example, the file styleguide.md has a merge conflict.

    $ git status
    > # On branch branch-b
    > # You have unmerged paths.
    > #   (fix conflicts and run "git commit")
    > #
    > # Unmerged paths:
    > #   (use "git add ..." to mark resolution)
    > #
    > # both modified:      styleguide.md
    > #
    > no changes added to commit (use "git add" and/or "git commit -a")
  4. Open your favorite text editor, such as Atom, and navigate to the file that has merge conflicts. Atom is easy to use when merge confilit. It has "use me" prompt.

  5. To see the beginning of the merge conflict in your file, search the file for the conflict marker <<<<<<<. When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <<<<<<< HEAD. Next, you'll see =======, which divides your changes from the changes in the other branch, followed by >>>>>>> BRANCH-NAME. In this example, one person wrote "open an issue" in the base or HEAD branch and another person wrote "ask your question in IRC" in the compare branch or branch-a.

    If you have questions, please
    <<<<<<< HEAD
    open an issue
    =======
    ask your question in IRC.
    >>>>>>> branch-a
    
  6. Decide if you want to keep only your branch's changes, keep only the other branch's changes, or make a brand new change, which may incorporate changes from both branches. Delete the conflict markers <<<<<<<=======>>>>>>> and make the changes you want in the final merge. In this example, both changes are incorporated into the final merge:

    If you have questions, please open an issue or ask in our IRC channel if it's more urgent.
    
  7. Add or stage your changes.

    $ git add .
  8. Commit your changes with a comment.

    $ git commit -m "Resolved merge conflict by incorporating both suggestions."

use git diff one file, use

git diff <revision> <path>

example: 

$ git diff 105eda3039f9d928cacf119f0faf59a1f136f12b  Part.1.E.1.entrance.ipynb

git diff <branch>:<dir>/<filename> <branch>:<div>/<filename> 

revision is diff current commit vision.

example:

$ git diff master:Part.1.E.1.entrance.ipynb study:Part.1.E.1.entrance.ipynb

More info please see below references.

references:

1. https://help.github.com/en/articles/resolving-a-merge-conflict-using-the-command-line

2. https://stackoverflow.com/questions/5586383/how-to-diff-one-file-to-an-arbitrary-version-in-git

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值