Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Sometimes the process of merging file contents doesn't go so smoothly. If you changed the same part of the file in both branches you're trying to merge together, Git won't be able to merge the changes automatically. This is called a __merge conflict__, and Git will need your help to resolve it.
- Suppose someone got impatient waiting for us to add letter conversions on the
add-letters
branch, and hastily added their own to themaster
branch.
(Notice they made a mistake; 2
should map to 'B'
, not 'A'
!)
KEY = {
1 => 'A',
2 => 'A',
3 => 'C',
4 => 'D',
5 => 'E',
6 => 'F',
7 => 'G',
8 => 'H',
9 => 'I',
}
- They stage their changes:
git add decoder.rb
- And commit them:
git commit -m 'Add A-I conversions, because I cannot wait'
- Now suppose I'm on the
add-letters
branch:git checkout add-letters
- I'm going to add some additional conversions to our
decoder.rb
file, for the letters F, G, H, I, and J. [Paste.]
(Again, notice the mistake on the mapping for the number 9
! But this time the mistake's on the add-letters
branch, not master
.)
6 => 'F',
7 => 'G',
8 => 'H',
9 => '*',
10 => 'J',
- Let me stage this:
git add decoder.rb
- And commit it:
git commit -m 'Add F-J conversions'
- Then I realize that my fix for missing conversions is only on the
master
branch. - So I merge the
master
branch into the currentadd-letters
branch:git merge master
- Because both the
master
andadd-letters
branches make changes to the same part of thedecoder.rb
file, there's a merge conflict. - Git stops the merge partway through, so you can resolve the conflict before it completes the merge.
$ git merge master
Auto-merging decoder.rb
CONFLICT (content): Merge conflict in decoder.rb
- As always, when you're not sure what's going on, you should run the
git status
command:
$ git status
On branch add-letters
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: decoder.rb
no changes added to commit (use "git add" and/or "git commit -a")
- Let's say we decided this is a total mess, and we have no idea how to fix it right now.
- As hinted by
git status
, to abort merge and restore all your files:git merge --abort
-
decode.rb
contents look like they did before you rangit merge
. -
git status
reports "working tree clean".
-
- But let's say we wanted to go ahead with the merge. Let's start over and try again.
git merge master
git status
- Git tries to let you use commands you're already familiar with to let you resolve the conflict:
-
git status
to show what's going on -
git stage
to indicate you've resolved the conflict -
git commit
to complete the merge commit
-
Conflict resolution markers
Git adds conflict resolution markers in the decoder.rb
file:
KEY = {
1 => 'A',
<<<<<<< HEAD
2 => 'B',
=======
2 => 'A',
>>>>>>> master
3 => 'C',
4 => 'D',
5 => 'E',
6 => 'F',
7 => 'G',
8 => 'H',
<<<<<<< HEAD
9 => '*',
10 => 'J',
=======
9 => 'I',
>>>>>>> master
}
- These have no special meaning to Git.
- We could stage and commit these right now, and Git would let us. But your code wouldn't work.
- So we need to edit our file to remove them, ideally in a way that combines all the changes from the two branches so everything works.
- Reading the markers:
- Remember, HEAD is a marker that points to your currently checked-out commit. So the HEAD section is the code from the branch you're merging into.
- The
=======
marker divides the changes from the two branches. - The ending conflict resolution marker will be labeled with the name of the branch you're merging in.
- Editing the markers:
- For each conflict marker, pick the code you want to keep, either from the checked out branch or the branch you're merging in.
- Sometimes, you might need to mix the two. You'll have to use common sense.
- Be sure to delete the starting marker, branch divider, and ending marker. Git doesn't care if you leave these in, but they're probably not valid code in your programming language, so you'll want to remove them.
- After you complete your changes, you should test your code to make sure everything's still working.
Completing the merge
- Let's run
git status
again to see what to do next.- It says
use "git add <file>..." to mark resolution
. - Let's do that:
git add decoder.rb
- Although Git uses this fancy "mark resolution" terminology, we're really just staging the file to become part of the merge commit.
- It says
- If we run
git status
again, it will say what to do next:
$ git status
On branch add-letters
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: decoder.rb
- It says
use "git commit" to conclude merge
.- Let's do that:
git commit
- Don't use the
-m
flag, because Git provides its own commit message. - You should probably just keep the default message.
- If the reasons for the changes you made when merging aren't obvious, you can add details to this commit message. But generally you should avoid big changes during a merge commit.
- Let's do that:
- If you run
git log
now, you'll see a merge commit at the top:
commit 370c5b832ca0860d35afc6f218c89b92fd1cc324 (HEAD -> add-letters)
Merge: d321035 98d7d33
Author: Jay McGavren <me@example.com>
Date: Sat Oct 6 00:40:14 2018 -0700
Merge branch 'master' into add-letters
commit d321035cd937c62b33828f701076eb239828b5ba
Author: Jay McGavren <me@example.com>
Date: Fri Oct 5 22:27:12 2018 -0700
Add F-J conversions
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up