How to Automatically Resolve Git Merge Conflicts in Favor of Any Side

  1. Resolving merge conflicts automatically
  2. The git pull and git merge Commands
  3. Merge Strategies

If you want to resolve all the conflicting changes from a working tree without going through all of the merge conflicts, also keep all the changes that are not conflicted, then you are in the right place to learn how to do that.

Resolving merge conflicts automatically

In cases when you prefer the work of other developers rather than yours, you can mention the appropriate strategy to resolve the conflicts by giving the preference to other developers' work.

git pull -s recursive -X theirs <remoterepo or other repo>

Or, simply, for the default repository:

git pull -X theirs

If you have already pulled without using the theirs strategy and want to give the preference to the work of other developers in one of the conflicting files, then run the command below:

git checkout --theirs path/to/file

If you are already in a conflicted state and want to give the preference to the work of other developers in all the conflicting files, then run the git checkout and git add commands:

git checkout --theirs .
git add .

If you give the preference to the code, written by yourself, then you should use --ours , instead of --theirs as follows:

git checkout --ours .
git add .

However, this is drastic, so make sure before running it.

You may not use the "." and type the file name in place of the dot to checkout.

You can also use the recursive --theirs strategy option with git merge:

git merge --strategy-option theirs

The git pull and git merge Commands

The git pull command is executed to fetch and download content from the remote repository and integrates changes into the local repository; thus, it is called the mixture of git fetch and git merge. The basic use of git merge is to combine two branches and multiple commits into one history. Merge commits are thought as unique because they have two parent commits. Git automatically combines separate histories when a new merge commit is created. The changed data in both histories won’t be combined. This is called “version control conflict”.

Merge Strategies

While combining the work into the main line of development, you should choose a one of merge strategies. If you do not choose the merge strategy is not specified, Git will automatically select a merge strategy based on the provided branches. The -s option can be attached with the name of the specified strategy. When you pull or merge branches, Git will select the recursive strategy as default. The recursive strategy only can detect and do merges which involve renames, but cannot use detected copies. The ours option forces conflicted parts to be automatically resolved by favoring 'our' version. The changes from the other tree that are not conflicting with our side are reflected in the merge output.

The theirs option favors the other merging tree in conflict resolution.

Do you find this helpful?

Related articles