4

I've come here even after looking at this link - How to resolve merge conflicts in Git? and many other links. Everywhere, it is mentioned that you need to do a 'git add' after resolving the conflicts in a text editor.

I've been using git for a few years now, and have managed to not use merge too often. So, every once in a while, I have to google the method to resolve after merge.

Hence, I think I haven't understood some part of git (or 'git add') that makes me forget that I need to do a 'git add'. Can anyone explain/point out to me what I am missing so that the next time I merge, doing a 'git add' is the logical thing to do after resolving conflicts?

Community
  • 1
  • 1
Devesh
  • 859
  • 8
  • 17

2 Answers2

4

The git index contains three copies of conflicted file. These are;

  1. Merge base
  2. "our" version
  3. "their" version

you can see them with git ls-files -s

The version with merge markers is not stored in the index, it is only in the working directory. After merging and resolving the conflicted file, git add reduces the index to one copy.

Alper
  • 12,860
  • 2
  • 31
  • 41
  • 2
    @Devesh, this one is a good answer. But to get better understanding of the reason you have to `git add` a file after resolving conflicts in it is that *each commit in Git is always created from the state of the staging area ("the index").* In other words, when you run `git commit` Git takes the state of the staging area and creates the commit. What's not `git add`'ed won't be a part of the commit. So you have to `git add` the new (fixed) state of a file with conflicts to make conflicts in it solved. – kostix Apr 23 '14 at 12:19
1

When you have merge conflicts, Git asks you to resolve the conflicts yourself, so you have to edit the files. When you git add the edited files, that's the sign that you've resolved the conflicts, just like in the ordinary Git workflow: you have to signal what you want to commit. It might be good to think of merge as a variant of commit that makes merge commits. The only thing really special about it is that it commits changes without git add when there are no conflicts.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836