Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge left HEAD marks in my files

I tried to merge a file in the command line using Git, when an error message appeared telling me the merge was aborted.

I thought that was the end of it, but then I realized there are gitmarks in my files. Like so:

start =     expression  validchar =      [0-9a-zA-Z_?!+\-=@#$%^&*/.]  integer =  <<<<<<< HEAD     digits:[0-9]+         { return digits.join(""); } =======     sign:"-"* digits:[0-9]+         { return sign + digits.join(""); } >>>>>>> gh-pages 

The files have been edited not by me and show lines inserted with:

  • HEAD after less than signs (<<<<<<< HEAD)
  • lines of changed code
  • a string of equals signs (=======)
  • the new version of the code
  • another line starting with greater than signs and the name of the branch (>>>>>>> gh-pages)

What's worse is that the file contents are no longer in order. Does anyone know how I get those files back to normal, and the changes I made in the gh-branch merged into the master branch?

like image 698
lowerkey Avatar asked May 18 '12 17:05

lowerkey


People also ask

What is head merge conflict?

A merge conflict usually occurs when your current branch and the branch you want to merge into the current branch have diverged. That is, you have commits in your current branch which are not in the other branch, and vice versa. Typically, there is one branch point, which is the latest common commit.

How do I get rid of unfinished merge?

Use git-reset or git merge --abort to cancel a merge that had conflicts. Please note that all the changes will be reset, and this operation cannot be reverted, so make sure to commit or git-stash all your changes before you start a merge.


2 Answers

Those are conflict markers. You're still in the process of merging, but there were some parts that Git couldn't merge automatically. You'll need to hand-edit those parts to what you want them to be and then commit the results.


For instance, in your particular case, you'd probably want to resolve it like this (note - the arrows/text on the right are just my notes, not something you'd type into the file):

integer =  <<<<<<< HEAD                                  <-+ remove the bits here     digits:[0-9]+                               |         { return digits.join(""); }             | =======                                       <-+     sign:"-"* digits:[0-9]+         { return sign + digits.join(""); } >>>>>>> gh-pages                              <-- and this 

and thus you'd save the file as...

integer =      sign:"-"* digits:[0-9]+         { return sign + digits.join(""); } 
like image 97
Amber Avatar answered Sep 20 '22 18:09

Amber


Absolutely start with 'git status' to see what you've got. If you aborted a merge (or had a merge aborted) and you've got conflicted files in the working directory then something went wrong. The Git status will tell you where you are. After that, you have a number of options. You should resolve the merge commit either by-hand, which can be challenging, or using a tool as:

git mergetool 

The merge tool will work if your files are listed as needing a merge.

You can also perform one of:

git checkout --ours -- /path/to/conflicted-file       # this is probably the one you want git checkout --theirs -- /path/to/conflicted-file 

You can see the different versions using the :1:filename syntax. See here for an explanation. But all of the above assumes that 'git status' shows the files as needing a merge.

Finally, you always have the option of:

git reset --hard   # sounds like --hard is what you need but check other options 
like image 31
GoZoner Avatar answered Sep 22 '22 18:09

GoZoner