GIT Command Line Cheat Sheet
git branch -a #List branches git branch --delete BRANCH_NAME #Delete a branch git checkout mybranch #switch to mybranch git merge FromBranch #merge FromBranch to the current working branch
Introduction
This post is an introduction on how to resolve merge conflict. The following procedure will create a merge conflict
As shown in the following picture, the merge conflict is caused by that the same file was change at two different branches at different time. Typically, one develop check out the code from master branch, made some change and merged to the master branch. Another developer checkout the code after the previous developer and committed the code later.
Create Merge Conflicts
$ mkdir groovy $ cd groovy $ vi basic_one.groovy
Copy and paste the following code to the file of basic_one.grooy, and safe it (:wq)
println('Hello World!!!') def myList = [1, 3, 4, 5, [10, 13], 1, 2, 3, [15, 16], 22, 33] println newList = myList.flatten() newList.each { item -> print item + " " } println()
Now execute the following commands
git init git add . git commit -am "first commit on master"
So far we have commit a file to our local repository. Next, we will create a new branch, namely, develop. We will make changes to the same file
Now execute the following commands
git branch develop # create develop branch git branch -a # list branches, you should see the * master and develop. # The start * indicates we are working on master git checkout develop # switch to develop branch
git branch -a
Now, I see the following:
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ git branch -a * develop master Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$
You can see that the current active branch is develop. We will change the content of the file to the following:
println('Hello, Gary Liu!') def myList = [10, 13, 15, 16, 22, 33, 44, 55] println newList = myList.flatten(); newList.each { item -> print item + " " }; println();
Now execute the following commands to commit the develop branch:
git commit -am "First commit to develop branch"
In order to create conflicts, we will need to checkout the master branch and do some modification on the file
git checkout master.
If you execute: cat basic_one.groovy, you should see the following:
println('Hello World!!!') def myList = [1, 3, 4, 5, [10, 13], 1, 2, 3, [15, 16], 22, 33] println newList = myList.flatten() newList.each { item -> print item + " " } println()
This is where we left off. In the meantime, develop branch has changed. Let's make some change to the file and it will be looks like the following:
println('Hello, World Again!'); def myList = [-1, -2, 1, 3, 4, 5, [10, 13], 1, 2, 3, [15, 16], 22, 33] println newList = myList.flatten() newList.each { item -> print item + " " } println()
Remember that we are at master branch now. We will merge from develop branch to the master
git add . git commit -am "Second commit on master" git merge develop
Now we see the following output:
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ git merge develop Auto-merging basic_one.groovy CONFLICT (content): Merge conflict in basic_one.groovy Automatic merge failed; fix conflicts and then commit the result. Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$
We have successfully created merge conflict. If you can the file of basic_one.groovy, it looks like the following:
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ cat basic_one.groovy <<<<<<< HEAD println('Hello, World Again!'); def myList = [-1, -2, 1, 3, 4, 5, [10, 13], 1, 2, 3, [15, 16], 22, 33] println newList = myList.flatten() newList.each { item -> print item + " " } println() ======= println('Hello, Gary Liu!') def myList = [10, 13, 15, 16, 22, 33, 44, 55] println newList = myList.flatten(); newList.each { item -> print item + " " }; println(); >>>>>>> develop Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$
Resolve Merge Conflicts
The above case is a bit extreme, as the whole file is almost changed. The HEAD section is from master branch and the lower part is from develop. To resolve the merge conflict, we need to edit file. For this case I will just take the master case, and the resultant file looks like the following:
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ vi basic_one.groovy Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ git commit -am "Resolve merge conflict" [master d2d5ecf] Resolve merge conflict Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ git merge develop Already up-to-date. Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ cat basic_one.groovy println('Hello, World Again!'); def myList = [-1, -2, 1, 3, 4, 5, [10, 13], 1, 2, 3, [15, 16], 22, 33] println newList = myList.flatten() newList.each { item -> print item + " " } println() Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$
To resolve merge conflict, we have to manually modify the file. We can use IDE like eclipse, sourceTree, etc, but we have to make decisions on which one is the best. After that we need to commit the changes and do a merge again to make sure that all the conflicts are addressed correctly.