Search code examples
gitatlassian-sourcetree

Why does a conflicted file appear both staged and unstaged in Git?


enter image description here

I've been experimenting with Git for the purposes of writing a Git tutorial. I created a branch, modified the file on both branches, then merged the branch back to master to generate a conflict. What I'm curious about is why the conflicted file appears to be both "staged" and "unstaged". If I click the file in either place the diff window shows the exact same information.


Solution

  • A file being both staged and unstaged is common in Git. Git recognizes different parts of a changed file as 'hunks', hence the buttons 'Stage hunk' and 'Discard hunk' in the screenshot. This UI situation means that some of the file is staged, and some if it is not. You could commit here and only the changes in the top menu would be committed.

    I'm not sure why each version of the file shows the same information; that's surprising. Sourcetree is probably having trouble showing the conflict in a way that makes sense.

    To proceed, you'll want to unstage everything, resolve your merge conflict, and then commit the corrected file. This means removing these lines:

    <<<<<<< HEAD
    =======
    >>>>>>> new-feature
    

    And keeping the code you want from either above or below the centerline (or both).