Showing posts with label GIT Merge Conflicts. Show all posts
Showing posts with label GIT Merge Conflicts. Show all posts

Friday, 28 December 2018

GIT: Git Merge Therapy

Git merge conflicts are normal and okay.
They are supposed to happen and, most likely, will happen regularly. They don’t happen because you did something wrong. They happen because Git is trying to protect you from losing your hard work.
If you’re new to Git or haven’t done extensive work with it in a team environment then you probably haven’t had the experience of a lot of merge conflicts.
In a future article I will share how you can resolve conflicts but right now, let’s talk about prevention.
We can do some things to prevent conflicts; here’s a list of a few:
  • Ignore generated files
  • Ignore cache or other runtime directories and files
  • Have a good branching strategy
  • Avoid whitespace errors

GIT: How can I fix & solve merge conflicts?

A merge conflict is not the end of the world. Actually, if you keep a couple of things in mind, solving conflicts is easy as pie:
  1. Keep Calm
    Above all, you need to realize that you cannot break anything: Git always allows you to go back to the state before the conflict occurred. With a simple "git merge --abort", you can always undo the merge and start over again. This makes it almost impossible to severely screw things up.
  2. How do I Know I Have a Conflict?
    When calling "git status", you'll see a special Unmerged paths category. All of the items in this category are in a conflict state and need to be dealt with:
    $ git status
    # On branch contact-form
    # You have unmerged paths.
    #   (fix conflicts and run "git commit")
    #
    # Unmerged paths:
    #   (use "git add <file>..." to mark resolution)
    #
    #       both modified:   contact.html
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    
  3. Understand When & Why a Conflict Happens
    Conflicts occur when the same file was changed in contradictory ways. Most modifications don't fall into this category: if two people just work on the same file, Git can most likely figure things out on its own.
    The most common situation when it cannot do this is when the exact same lines were edited in that file. In that case, Git has no way of knowing what's correct - you'll have to look at the changes and decide how you want the file to finally look.
  4. A Conflict is Just an Annotation
    It helps to realize that a conflict is nothing magical. In the concerned file, Git simply marks the areas that were edited in contradictory ways:
    This helps you understand which edits were made - and even on which branches.
  5. Solving Means Choosing & Editing
    Your job now is to condition the file to its desired state. There are a couple of ways to do this:
    (a) You can simply open the file in an editor, search for the conflict markers (see above image) and make any necessary modifications. When you're done, the file needs to look exactly as you want it to look.
    (b) Alternatively, you can tell Git that you'll simply go with one of the edited versions, called "ours" or "theirs".
    git checkout --ours path/to/conflict-file.css
    
    Note that there are lots of dedicated "Merge Tool" applications that help you with this process. Especially in complex situations with multiple conflicts in the same file, a good tool can be of tremendous value. We've compiled a list of merge tools.
  6. Wrap Up
    When you've successfully solved all conflicts, you need to do two more things:
    (1) Mark each conflicted file as solved. A simple "git add <filepath>" does this for you.
    (2) Commit the resolution just as you would commit any other change with the "git commit" command.

Thursday, 27 December 2018

GIT: Solving merge conflicts

39.1. What is a conflict during a merge operation?

Merge conflict A conflict during a merge operation occurs if two commits from different branches have modified the same content and Git cannot automatically determine how both changes should be combined when merging these branches.
This happens for example if the same line in a file has been replaced by two different commits.
If a conflict occurs, Git marks the conflict in the file and the programmer has to resolve the conflict manually.
After resolving it, he adds the file to the staging area and commits the change. These steps are required to finish the merge operation.

GIT: Keep a version of a file during a merge conflict

Merge conflict LARSSECONDARY"theirs parameter"LARSSECONDARY Merge conflict
LARSSECONDARY"ours parameter"LARSSECONDARY Sometimes if a conflict occurs the developer does not want to solve the conflict. He decides that he wants to keep the original version or the new version of the file.
For this, there is the --theirs and the --ours options on the git checkout command. The first option keeps the version of the file that you merged in, and the second option keeps the version before the merge operation was started.
git checkout --ours foo/bar.java
git add foo/bar.java
git checkout --theirs foo/bar.java
git add foo/bar.java