Git merge often you do and face conflict issue while doing git merge. Of course, it is possible to face such issue that is Git merge conflict, right? So how to resolve this in production or staging environment ?
Don’t worry, I will show you “How to resolve git merge conflict issue”. Even Git gives a hint to resolve conflicts issue in its error message. It shows Merge conflict in [filename], so you know there is a problem with that file. Then it refers to fix conflicts and then commit the result,
Am I Right? So if you follow instructions, do edit the file, then commit it, everything should work fine. But let’s see this in an example.
Git Merge Conflict Example-
Let’s create a new Git repo, add a file, do git merge, create new branch, create some conflicting edits to produce real scenario and see how it looks like and fix it.
You start with an empty directory and always run git init:
$ ls -l $ git init Initialized empty Git repository in home/demo/gitpractice/.git/ $
Then create a README file and commit the changes:
$ echo "This is a new README test file" > README.md $ cat README.md This is a new README test file $ git add README.md $ git commit -m "Merge Practice" 1 file changed, 1 insertion(+) create mode 100644 README.md $ git status On branch master nothing to commit, working tree clean $
Now create a new branch to produce real life hack:
$ git checkout -b "merge_conflict_branch" Switched to a new branch 'merge_conflict_branch'
Run git branch command to see the current branch:
$ git branch * merge_conflict_branch master
Open README.md file in editor and do an edit:
This is a new README test file ## Line1 This is a git merge conflict file ## Line2
Now commit the recent changes, use following steps to do this:
$ vi README.md ## Edit in README file $ git status $ git add README.md $ git commit -m "Edit README" [merge_conflict_branch 5583fb6] Edit README 1 file changed, 1 insertion(+), 0 deletion(-)
Great, you’re on half of your way to jump in real problem, Now return to the master branch, edit the README on line 2 with something different, and commit that.
Let’s go back to the master branch:
$ git checkout master
Switched to branch 'master'
Edit the README.md file but keep the new Test here in line2
This is a new README test file ## Line1 This is a git merge conflict demo file ## Line2
Commit the new changes that you edit:
$ git add README.md $ git commit -m "Edits README on maste branch" [master 7ea2985] Edits made to README on the master branch 1 file changed, 1 insertions(+)
Now merge the branch into master to see the error:
$ git branch * master merge_conflict_branch $ git merge merge_conflict_branch Auto-merging README.md CONFLICT (content): Merge conflict in README.md Automatic merge failed; fix conflicts and then commit the result.
Now, open the README file, as Git give hints to see what it looks like:
<<<<<<< HEAD "This is a new README test file" This is a git merge conflict demo file ======= "This is a new README test file" "This is a git merge conflict file" >>>>>>>merge_conflict_branch
Git Merge Real Issue
Wow! Now you can see, Git added some syntax including seven “less than” characters, <<<<<<< and seven “greater than” characters, >>>>>>>, separated by seven equal signs, =======. This gives easy to find quickly in editor where file edits need to be done.
Why there are two sections within this block?
- The ” (<) less than” characters stand for the current branch’s edits (in this case, “HEAD,” which also known as current branch), and the equal signs denote the end of the first section.
- The ” (>) greater than section is where the edits are from the attempted merge; that starts with the equal signs but ends with the “greater than” signs.
But as a Developer or DevOps, you know what should keep and what should go in production or stage branch. Now do your edits as necessary, then close the file:
For Example- I removed line 1 from concern branch:
"This is a new README test file" This is a git merge conflict demo file This is a git merge conflict file
You can see, this keeps the branch’s edits and you can run git status to see further instructions:
$ vi README.md $ git status On branch master You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add …" to mark resolution) both modified: README.md no changes added to commit (use "git add" and/or "git commit -a")
Even if you run into such issues, you can abort the merge by running git merge –abort to abort the merge.
But let’s follow the directions to add the file and then commit:
$ git add README.md $ git status On branch master All conflicts fixed but you are still merging. (use "git commit" to conclude merge) Changes to be committed: modified: README.md $ git commit -m "Edits Merge conflict in README on master branch" [master 108f3d5] Edits Merge conflict in README on maste branch
That’s all from this practice with git merge conflict and how to resolve it. You may refer advance merger conflict solution here and follow best practice to avoid merge conflict in production.