How to Automatically Resolve Git Merge Conflicts in Favor of Any Side
Read this tutorial and solve the problem of resolving git merge conflicts in favor of pulled changes. Get overall understanding of Git 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 specify the appropriate strategy to resolve the conflicts by giving the preference to other developers' work.
recursive strategy option git
git pull -s recursive -X theirs <remote> <branch>Or, simply, for the default repository:
recursive strategy option git
git pull -X theirsIf you have already pulled without using the <kbd class="highlighted">theirs</kbd> 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
git checkout --theirs path/to/fileIf 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, git add
git checkout --theirs .
git add .
git commitIf you give the preference to the code, written by yourself, then you should use <kbd class="highlighted">--ours</kbd>, instead of <kbd class="highlighted">--theirs</kbd> as follows:
git checkout, git add
git checkout --ours .
git add .
git commitHowever, this is drastic, so verify your changes before running it.
You can also specify a file name instead of the dot to checkout a specific file.
You can also use the recursive <kbd class="highlighted">--theirs</kbd> strategy option with git merge:
git merge, recursive strategy option git
git merge --strategy-option theirsThe 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 <kbd class="highlighted">git merge</kbd> is to combine two branches and multiple commits into one history. Merge commits are considered 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 one of merge strategies. If a merge strategy is not specified, Git will automatically select a merge strategy based on the provided branches. The <kbd class="highlighted">-s</kbd> 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 can detect and handle merges involving both renames and 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.