Tutorial: GIT and GitHub 8. Merge conflicts - 2020
We'll start from where we left off (7. Branching & Merging). We'll see how we can resolve conflicts when we try to merge the changes made by different branches.
Let's create a new branch:
admin@KHONG /c/MyGit/GitProject (master) $ git checkout -b fix2 Switched to a new branch 'fix2'
Then, modify the README:
README first README fix2
Commit the change:
admin@KHONG /c/MyGit/GitProject (fix2) $ git commit -a -m "Changed via fix2" [fix2 f77a7a0] Changed via fix2 1 file changed, 1 insertion(+), 1 deletion(-)
admin@KHONG /c/MyGit/GitProject (fix2) $ git checkout master Switched to branch 'master' admin@KHONG /c/MyGit/GitProject (master)
We need to check if the README has not been changed since we did not do merge with the fix1 branch yet. Note that we did not merge because we want to create a conflict:
README first README fix1
As we can see, it stays untouched. The 'fix1' is there instead of 'fix2'.
We usually name a branch as "hotfix" if we're planning to merge immediately:
admin@KHONG /c/MyGit/GitProject (master) $ git checkout -b "hotfix" Switched to a new branch 'hotfix' admin@KHONG /c/MyGit/GitProject (hotfix)
Let's modify our README:
README first README hotfix
admin@KHONG /c/MyGit/GitProject (hotfix) $ git commit -a -m "Adding hotfix" [hotfix 2d540f8] Adding hotfix 1 file changed, 1 insertion(+), 1 deletion(-) admin@KHONG /c/MyGit/GitProject (hotfix)
Since we have two different branches, there will be an issue when we do merge.
Let's switch back to master and merge:
admin@KHONG /c/MyGit/GitProject (hotfix) $ git checkout master Switched to branch 'master' admin@KHONG /c/MyGit/GitProject (master) $ git merge hotfix Updating 77bb1bc..2d540f8 Fast-forward README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) admin@KHONG /c/MyGit/GitProject (master)
So far, no problem. We should see 'hotfix' in our README:
README first README hotfix
Yes, the hotfix branch has been merged successfully.
We now safely delete the 'hotfix' branch:
admin@KHONG /c/MyGit/GitProject (master) $ git branch -d 'hotfix' Deleted branch hotfix (was 2d540f8). admin@KHONG /c/MyGit/GitProject (master)
Note that we checked out fix2 branch before the hotfix branch which has already been merged. This will create a conflict when we try to merge fix2 branch:
admin@KHONG /c/MyGit/GitProject (master) $ git branch -v fix2 f77a7a0 Changed via fix2 * master 2d540f8 Adding hotfix merged 4aece6d initial commit admin@KHONG /c/MyGit/GitProject (master) $ git merge fix2 Auto-merging README CONFLICT (content): Merge conflict in README Automatic merge failed; fix conflicts and then commit the result. admin@KHONG /c/MyGit/GitProject (master|MERGING)
Since we want to use KDiff3 as our mergetool, we need to let git know about that:
admin@KHONG /c/MyGit/GitProject (master|MERGING) $ git config --global merge.tool kdiff3 $ git config --global mergetool.kdiff3.cmd '"C:\\Program Files\\KDiff3\\kdiff3" $BASE $LOCAL $REMOTE -o $MERGED'
We'll use mergetool:
$ git mergetool Merging: README Normal merge conflict for 'README': {local}: created file {remote}: created file Hit return to start merge resolution tool (kdiff3):
At Return key, it will popup the following KDiff3 window:
Select "Choose B for All Unresolved Conflicts" under "Merge" top-menu.
Save the file as README. When we look at our directory, we can see another file called "README.orig". We may want to delete it:
admin@KHONG /c/MyGit/GitProject (master|MERGING) $ ls README README.orig admin@KHONG /c/MyGit/GitProject (master|MERGING) $ rm README.orig
admin@KHONG /c/MyGit/GitProject (master|MERGING) $ git branch fix2 * master admin@KHONG /c/MyGit/GitProject (master|MERGING) $ git branch --no-merged fix2
We'll delete the unmerged branch, "fix2":
admin@KHONG /c/MyGit/GitProject (master|MERGING) $ git branch -D fix2 Deleted branch fix2 (was 2a2cda3).
Now we have only one branch which is 'master' branch:
admin@KHONG /c/MyGit/GitProject (master|MERGING) $ git branch * master
Git/GitHub Tutorial
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization