in Coding, linux

Resolve Git Merge Conflicts Multiple Files in Single command

Use Case : Merge conflict when we did git pull.

Conflict Example:

(Code not in Conflict)
>>>>>>>>>>>
(first alternative for conflict starts here/local or our version of code)
Multiple code lines here
===========
(second alternative for conflict starts here/their or remote branch version of code)
Multiple code lines here too    
<<<<<<<<<<<
(Code not in conflict here)

Resolving merge conflicts while Git Pull/Push

Find files with merge conflict

Change working directory to project folder.

cd project-folder

Search for all conflicting files.

grep -lr '<<<<<<<' .

Above will list all files which has marker special marker <<<<<<< in them.

Resolve easy/obvious conflicts

At this point you may review each files. If solution is to accept local/our version, run:

git checkout --ours PATH/FILE

If solution is to accept remote/other-branch version, run:

git checkout --theirs PATH/FILE

 

If you have multiple files and you want to accept local/our version, run:

grep -lr '<<<<<<<' . | xargs git checkout --ours

 

If you have multiple files and you want to accept remote/other-branch version, run:

grep -lr '<<<<<<<' . | xargs git checkout --theirs

 

After above review your git branch using git status . And then commit and add to git folder.