Merging
Branches
Merging
Branching makes it super easy to work within self-
contained contexts, but often we want to incorporate
changes from one branch into another!
We can do this using the git merge command
Merging
The merge command can sometimes confuse students
early on. Remember these two merging concepts:
We merge branches, not specific commits
We always merge to the current HEAD branch
Merging
Made Easy To merge the bugfix branch into master...
To merge, follow these basic steps:
1. Switch to or checkout the branch you want to
❯git switch master
merge the changes into (the receiving branch)
2. Use the git merge command to merge changes ❯git merge bugfix
from a specific branch into the current branch.
To merge the bugfix branch
into the master branch...
Master
987fac... 236ff...
92faa... 2456...
Bugfix HEAD
❯ git switch master
Master HEAD Switch to the master branch
987fac... 236ff...
92faa... 2456...
Bugfix
❯ git merge bugfix
Merge bugfix into master
987fac... 236ff...
Master HEAD
92faa... 2456...
Bugfix
This is what it really looks like
Remember, branches are just defined by a branch pointer
Master HEAD
987fac... 236ff... 92faa... 2456...
Bugfix
This is what it really looks like
Master & Bugfix now point to the same commit, until they diverge again
Master HEAD
987fac... 236ff... 92faa... 2456...
Bugfix
❯ git merge bugfix
This Is Called A Fast-Forward
Master simply caught up on the commits from Bugfix
Master HEAD
987fac... 236ff... 92faa... 2456...
Bugfix
Not All Merges Are Fast Forwards!
We've branched off of master, just like before...
Master
987fac... 236ff...
92faa...
92faa... 2456...
2456...
Bugfix HEAD
What if we add a commit on master?
This happens all the time! Imagine one of your teammates merged in a new
feature or change to master while you were working on a branch
Master
987fac... 236ff... f4faa...
92faa...
92faa... 2456...
2456...
Bugfix HEAD
What happens when I try to merge?
Rather than performing a simple fast forward, git performs a "merge commit"
We end up with a new commit on the master branch.
Git will prompt you for a message.
Master HEAD
987fac... 236ff... f4faa...
92faa...
92faa... 2456...
2456...
Bugfix
What happens when I try to merge?
Rather than performing a simple fast forward, git performs a "merge commit"
We end up with a new commit on the master branch.
Git will prompt you for a message.
Master HEAD
NEW
987fac... 236ff... f4faa...
COMMIT
92faa...
92faa... 2456...
2456...
Bugfix
Heads Up!
Depending on the specific changes your are trying to
merge, Git may not be able to automatically merge.
This results in merge conflicts, which you need to
manually resolve.
❯ CONFLICT (content): Merge conflict in blah.txt
Automatic merge failed; fix conflicts and then commit the result.
<<<<<<< HEAD WHAT THE...
I have 2 cats
When you encounter a merge conflict, Git warns you
I also have chickens in the console that it could not automatically merge.
=======
It also changes the contents of your files to indicate
I used to have a dog :( the conflicts that it wants you to resolve.
>>>>>>> bug-fix
<<<<<<< HEAD
I have 2 cats
Conflict Markers
I also have chickens The content from your current HEAD (the branch you
======= are trying to merge content into) is displayed
between the <<<<<<< HEAD and =======
I used to have a dog :(
>>>>>>> bug-fix
<<<<<<< HEAD
I have 2 cats
Conflict Markers
I also have chickens The content from the branch you are trying to merge
======= from is displayed between the ======= and
>>>>>>> symbols.
I used to have a dog :(
>>>>>>> bug-fix
Resolving Conflicts
Whenever you encounter merge conflicts, follow these
steps to resolve them:
1. Open up the file(s) with merge conflicts
2. Edit the file(s) to remove the conflicts. Decide which
branch's content you want to keep in each conflict. Or
keep the content from both.
3. Remove the conflict "markers" in the document
4. Add your changes and then make a commit!
Deleting A Branch ❯ git branch -d bugfix
master branch
987fac... 236ff... 92faa... 2456... HEAD
12323 ffa1233
bugfix branch
Deleting A Branch
master branch
987fac... 236ff... 92faa... 2456... HEAD