[git] git merge conflict resolution

Source: Internet
Author: User
Tags commit diff log log wrapper using git


Overview

When using Git for source code maintenance, it is often possible to view the differences before each code version, such as Git diff before committing the change to see the source file code relative to the corresponding version or remote repository changes. If there is a conflict, you need to merge the changed code into the specified version (for example, the remote branch).

start –beyond Compare from git difftool & Mergetool Tools

If there is a diff between the complex code version, if there is no difftool is unthinkable, if you have a good and familiar with the compare-comparison tool will often make you more effective, window of the diff tool has a lot, winmerge (free), Araxis Merge (charge), installed tortoisesvn words, also with a diff tool Tortioseidiff,sourcegear Mergediff, support windows,mac,linux, also very useful, Here I introduce the more commonly used beyond Compare. download install beyond Compare

Download and install beyond compare tools, here do not repeat, online search. Create a startup beyond Compare script

Create the following two scripts under the \cmd path of the git installation path, preceded by the actual pathname of the installed beyond Compare.exe, in order for Git to find the comparison tool, such as beyond Compare installation path "D:\Software_ Install\beyond\beyond Compare 3\bcompare.exe "" $ "" $ "| Cat

Create git-difftool-bcomp-wrapper.sh

# Place this file in the Windows Git installation Directory/cmd folder

# Be sure to add the. /cmd folder to the PATH environment variable

# diff is called by git with 7 parameters:

# path Old-file old-hex old-mode new-file new-hex New-mode

"D:\Software_install\Beyond\Beyond Compare 3\bcompare.exe" "$" "$" | Cat

Create git-mergetool-bcomp-wrapper.sh

# Place this file in the Windows Git installation Directory/cmd folder

# Be sure to add the. /cmd folder to the PATH environment variable

# passing the following parameters to Mergetool:

# Local Base Remote Merge_result

"D:\Software_install\Beyond\Beyond Compare 3\bcompare.exe" "$" "$" "$ $"--result= "$4"--title1= "Mine"

Setting Environment Variables

In order for Git to find the two SH previously defined, you need to set the cmd path for the git installation directory to an environment variable, such as my cmd path: C:\Program Files (x86) \git\cmd; The following figure:

change the git configuration file

The purpose of the configuration change is to specify Git's Difftool and Mergetool, first find the. gitconfig file, such as my installation path C:\Program files (x86) \git\etc, Open the editor to edit the. gitconfig file, the description of the last appended tool in the file, as follows:

[Merge]

Tool = Bcomp

[diff]

Tool = Bcomp

[Mergetool]

Keepbackup = False

[Mergetool "Bcomp"]

cmd = git-mergetool-bcomp-wrapper.sh "$LOCAL" "$BASE" "$REMOTE" "$MERGED"

[Difftool "Bcomp"]

cmd = git-difftool-bcomp-wrapper.sh "$LOCAL" "$REMOTE"

The above completes the Git Difftool and Mergetool configuration, the following to try the effect.

Difftool and Mergetool actual operation

The actual operation is described in the following sections.

Git Branch origin, Master introduction Introduction to the Git branch

In order to explain the next step, we must first have the concept of git branching, a few nonsense, the last real example.

By default you already have a version of the code from the remote server clone to the local, you can use Git branch–a to see all the branches as follows:

*master,* represents the branch of the current work, and the branch that is currently working is called Master, which is the local branch. Remotes/origin/master is a master branch on a remote library called origin and cannot change its contents locally. Origin is just an alias, and after clone is complete, Git automatically names this remote Repository as origin (Git remote–v or view. Git/config can see the meaning of origin). Remotes/origin/head-Origin/master,head is a pointer to the master branch on the remote Library named origin.

git diff & git difftool view Master branch and Origin/master branch

Knowing the difference between the master branch and the Origin/master branch in git, we can use the Git diff tool to see the difference between each version.

Before we introduce Git diff and git difftool operations, we have to make sure that your local branch master and Remote branches (Origin/master) are different-there are some differences that can be artificially made to achieve the demo effect.

Start with GIT fetch origin master crawl remote repository-about fetch and pull want to write the next section will be described in detail. As follows:

For example, the following diagram performs the git diff command in git bash:

This allows you to see the difference between Master and Origin/master.

But you'll find that the 1.1-point view at the command line is just too much, and this next configuration Difftool tool beyond compare can come in handy.

Run command: git difftool master Origin/master

The first hint is the difference between the first two branches of the file, type Y; you can go to beyond compare to see the difference between the two files:

Close this window and go back to bash, and if you have a different file, you will have the same hint until all the files have been traversed.

the difference between git fetch and git pull

Git fetches the latest version from the remote branch to the local two commands are used very frequently, namely git fetch and git pull, so in the merge conflict resolution, the GIT fetch and git pull command in-depth understanding is necessary.

git fetch

Git fetch, which is crawling version updates from a remote server, is the equivalent of getting the latest version from remote to local and not automatically merge.

git log//before crawling remote updates, log to view the current log

Git fetch origin master//update from remote crawl

git log//crawl to see log log not updated-no merge

So after the fetch you can do a diff operation, which is to see the diff between the local Master branch and the master branch of the remote Library called origin, to see the differences between the two versions.

This is the following two commands:

git diff master Origin/master

git difftool master Origin/master

This is already in the previous section: Git diff & git difftool see the Master Branch and the Origin/master branch for a detailed introduction.

git Pull

Git pull: Get the latest version from remote and merge to local

Git pull Origin master//crawl the branch from the master branch of the remote Repository named origin and merge

The above command is actually equivalent to git fetch and git merge

git pull and git fetch summary

Git fetch is a bit more secure in real-world use.
Because before the merge, we can see the update, use the git diff & git difftool command to see the difference between the local master and the remote Master Branch, and then decide whether to merge.

git merge and git rebase conflict resolution git merge resolves conflicts

In order to understand the entire process of conflict generation and conflict resolution, the following example is used to resolve the conflict with merge and submit the update to the server after the conflict is resolved. Conflict Scenario Settings

First set up a scenario: developer A and Developer B, clone a code from the remote origin to their own work computer, and then both develop locally on their own, then a snapshot is submitted first

SNAPSHOT_A1, this time B also to commit the code, but where the code changes and a changes have been committed to update the server conflict, this time need to be the latest code on the server fetch or

Pull to your own local, resolve the conflict and then commit your changes to the server.

To demonstrate this process. Update submission for branch a

End A: The following modifications have been made to the file readme.md:

Then a commits the changes to the server:

Git commit-a-M "Added Comment1 to Readme.md"

git push o r

Update submissions for B

On the other side B, the following modifications were made to README.MD, as follows:

and b also commits and push, doing the following:

Git commit-a-M "Added Comment1 to Readme.md"

Git push Origin Master

Push when prompted to conflict, the local version is older. b Resolve conflicts and submit

So b wants to crawl the remote server update to look at:

Git fetch Origin Master

Discovery server is updated, so B wants to see what updates the server has done

Execute command: Git difftool origin/master

Discover that README.MD has made updates, like your own updated files, type "Y",

Through the beyond can see the difference between two files, B viewed, changes need to be integrated, the next step is to resolve the conflict, and then submit.

B run the command on bash:

git merge Origin/master

There are conflicting hints, Git tries to integrate automatically, finds no integration, needs to resolve conflicts manually, and fortunately

Git has identified the conflicting places, as shown below:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.