1

I don't know how to word the question, but basically whenever I make changes to a project, such as a class file for example, git will throw in erroneous text. Here is a snippet of code from a project I am working on in Java.

public class PlayerList {
   ...
   <<<<<<< HEAD
    public static String getNamesByIndex(int index) {
        return playerNameList.get(index);
    }

=======
>>>>>>> 0bfheisi8d88wdjksiijfils8879s
   ...
}

How do I prevent git from adding the erroneous text? Obviously from methods I didn't change, no erroneous text were added.

Travis
  • 1,674
  • 1
  • 9
  • 14
  • This text marks changes to be merged due to different versions of the (class) file! Merge the files to get rid of it. – deHaar May 29 '18 at 08:30
  • Yes, but it leaves that markup in my class files. I have to go in and manually delete the erroneous text afterwards. – Travis May 29 '18 at 08:32
  • You NEED to do that because Git can't know, at this stage, which version is the good one. Everything between `<<<<<` and `====` comes from the current version of the file and everything between `=====` and `>>>>>` comes from the one you're merging into your current branch. So you have to make a choice, adapt your code, then remove those special separators. – Obsidian May 29 '18 at 08:38
  • Do you always see these where the line containing the equals signs and the line containing the greater than signs are next to each other, with no code in between? Do you check in from different computers? Is one of them Windows? – Edward Thomson May 29 '18 at 08:40
  • They are generated by Git and specified as this in the `git merge` documentation. Fomat is : <<<<< Your content… ===== Their content… >>>>> … provided that every content can be potentially empty depending on its side. – Obsidian May 29 '18 at 08:47
  • Possible duplicate of [Git merge left HEAD marks in my files](https://stackoverflow.com/questions/10657315/git-merge-left-head-marks-in-my-files) – phd May 29 '18 at 13:35

2 Answers2

2

Like our commentator said, this is due to a git conflict, lets look closer:

<<<<<<< HEAD
    public static String getNamesByIndex(int index) {
        return playerNameList.get(index);
    }

=======
>>>>>>> 0bfheisi8d88wdjksiijfils8879s

the ======= is the separator. Everything above that is what is in your current directory, and everything below it is in the remote, commit 0bfheisi.

This might be an automatically resolvable conflict. If you're the only one working on the project, and only are using it on a single computer, then this shouldn't be showing up if you're using git correctly. If you're working on the project with other colleagues then if you both make a change to the same area (and you didn't pull first) then this will happen because git needs you to choose which version is the correct version.

However, you shouldn't ever have to

go in and manually delete the erroneous text afterwards.

What you should be doing is the following:

  • Stage, and commit local changes.
  • Pull remote branch (can cause an automatic merge)
  • If there is a conflict, git will tell you at this point. If so, use git mergetool to open your specified conflict resolution tool. I use Beyond Compare 3 as it is an excellent gui tool for windows, but many options exist.
  • Now, once conflicts are resolved (not manually, but with your mergetool) push your changes to your remote branch.

I mentioned this above but will stress it again here, conflicts don't need to happen often, even if you're working with multiple people and are changing the same areas of code - as long as you frequently pull changes before starting a new change yourself, and committing and pushing often.

Happy gitting.

caesay
  • 16,932
  • 15
  • 95
  • 160
1

That text that you see is from a git merge conflict. When you attempt to merge a branch into your current one, if both branches have changed the same line then git does not know what line to pick as the new change. This is resolved by the user performing a manual merge that consists of the user picking the correct change. When git adds this merge conflict, your branch enters a merging state thus you shouldn't be able to push that text out to your remote branch until youve resolved the underlaying conflict or if you commit it and push it.

Kieran Devlin
  • 1,373
  • 1
  • 12
  • 28