1

I am in the situation of having a merge conflict in git, so far so good. After the merge I have a file foo.txt in my repository:

<<<<<<< HEAD
line 1 from-foo-branch
line 2 in bar-branch
||||||| empty tree
=======
line 1 from-foo-branch
line 2 in bar-branch
sdfgsdfgsdfg
>>>>>>> dev_futurama

If I open the conflicted file foo.txt with a non-git editor, thats what I have in it. I want to resolve the file with this external non-git editor. How must the solution look like and how can I proceed with the merge via terminal?

Daniel Stephens
  • 2,371
  • 8
  • 34
  • 86
  • Sure, but you'll have a clearer history if you do the conflict resolution in one commit and any other changes in another. Merges shouldn't really have new work in them. – isherwood Jan 03 '20 at 18:22
  • 1
    To put it simply, [a merge is just a commit like any other](https://stackoverflow.com/questions/47089913/merge-commit-vs-normal-commit). Git doesn't care what's in the file. – isherwood Jan 03 '20 at 18:24
  • 2
    Replace the part listed in the question with the lines you want to keep after the merge. They may be the lines from one branch or from the other branch or a combination of them or whatever you want to put there. After you decide how the conflicting lines should look like, save the file and add it to the index (`git add foo.txt`). Adding it to the index means you have solved the conflict. Fix all conflicts then run `git merge --continue` to resume the merge operation. – axiac Jan 03 '20 at 18:48
  • 2
    Read the Git documentation about [merge conflicts](https://git-scm.com/docs/git-merge#_how_conflicts_are_presented) – axiac Jan 03 '20 at 18:51
  • Side note: there are no "Git editors": Git has no built in editor, it uses whatever editor you tell it to use. So all editors are non-Git editors. Many *do* have special modes for editing Git commit messages (by recognizing the file name patterns Git uses when invoking them), but they're still just general-purpose editors. – torek Jan 03 '20 at 19:06
  • Indeed, incorrectly formulated.. – Daniel Stephens Jan 03 '20 at 19:07

2 Answers2

2

Sure you can overwrite the file with merge markers with content of new file. Just do git add <file> and git commit after that.

Shashank V
  • 10,007
  • 2
  • 25
  • 41
2

Edit the file in any editor you wish.

How to set the desired git editor?

 # Set the default git editor
 git config --global core.editor <your editor>

Once the conflict is resolved simply save the file then add it to the index and commit enter image description here


Tip:

If you wish to add some of the changes and not all of them at once use the git add -p

These are the options you can do within add -p:

y - stage this hunk
n - do not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

Once you use the s it will pick the chunk of code which can be considered as a standalone change. If you want to split it, even more, you will have to use the e to edit the hunk and then add it back to the stage area.

enter image description here

CodeWizard
  • 128,036
  • 21
  • 144
  • 167