command git merge is usually used in conjunction with git checkout and git branch -d(for deleting obsolete branches).
credits to Atlassian Bitbucket Tutorials
prepare to merge Link to heading
confirm the receiving branch Link to heading
use git checkout <receiving_branch> to switch to receiving branch. In some case, I want to merge to master so that I execute git checkout master
fetch latest remote commits Link to heading
this is very important when you work on a feature, for example, and someone else works on master at the mean time. execute git fetch to pull the latest remote commits. Once the fetch is completed ensure the master branch has the latest updates by executing git pull.
This is not so often when doing a personal project, at least for me.
merging Link to heading
git merge <branch_name> where <branch_name> is the name of branch that you want to merge.
fast forward merge Link to heading
the following graph demonstrates what is a fastforward merge.
some features
|
v
N --------- N
master /
| /
v /
N --- N --- N ------
before merging
some features
|
v
N --------- N
/ ^
/ |
/ master
N --- N --- N ------
after merging
a fastforward merging is not possible if the branches have diverged.
the following is how it is executed.
# Start a new feature
git checkout -b new-feature master
# Edit some files
git add <file>
git commit -m "Start a feature"
# Edit some files
git add <file>
git commit -m "Finish a feature"
# Merge in the new-feature branch
git checkout master
git merge new-feature
git branch -d new-feature
Not that master has no progress.
3-way merge Link to heading
the following graph demonstrates what is a 3-way merge.
some features
|
v
N --------- N
/ master
/ |
/ v
N --- N --- N ------ N --- N --- N
before merging
some features
|
v
N --------- N
/ \ master
/ \ |
/ \ v
N --- N --- N ------ N --- N --- N --------N
after merging
the following is how the command is executed.
Start a new feature
git checkout -b new-feature master
# Edit some files
git add <file>
git commit -m "Start a feature"
# Edit some files
git add <file>
git commit -m "Finish a feature"
# Develop the master branch
git checkout master
# Edit some files
git add <file>
git commit -m "Make some super-stable changes to master"
# Merge in the new-feature branch
git merge new-feature
git branch -d new-feature
conflicts Link to heading
when both branches, in this case, master and new feature edited the same file, say, hello.py, you could see:
On branch master
Unmerged paths:
(use "git add/rm ..." as appropriate to mark resolution)
both modified: hello.py
here is some content not affected by the conflict
<<<<<<< master
this is conflicted text from master
=======
this is conflicted text from feature branch
Once you’ve identified conflicting sections, you can go in and fix up the merge to your liking. When you’re ready to finish the merge, all you have to do is run git add on the conflicted file(s) to tell Git they’re resolved. Then, you run a normal git commit to generate the merge commit. It’s the exact same process as committing an ordinary snapshot, which means it’s easy for normal developers to manage their own merges.