Handling merge conflicts

One of the best features of git is its ability to easily merge multiple changes by different people.

Say you and a friend have both made changes to the same file at the same time. When you pull your friend’s changes, git will often be able to combine them without any problem.

Sometimes, though, after you do

$ git pull myfriend master

You’ll get a message like

Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

If you open the offending file in a text editor, you’ll find an indication of the bits that are different, something like this:

<<<<<<< HEAD
A line in my file.
A line in my friend's file
>>>>>>> 031389f2cd2acde08e32f0beb084b2f7c3257fff

Edit the bits from <<<<<<< to >>>>>>>, to make the file just as you want it.

Then do git add, git commit, and git push.

Amend the last commit message

This usually happens to me when I intended to use just git commit but typed git commit -a and committed a whole bunch more stuff that I hadn’t mentioned in the commit message.

It’s easy to fix just the message for the last commit:

$ git commit --amend -m "New commit message"

Or leave off the -m "New commit message" and type the message in the text editor that opens.

Licenses and Attributions

Speak Your Mind