There is a main stream line in software development.
And you sometimes want to develop being-developed-software in different direction.
Having different flow of the being-developed-software is called branching.
======================================================================
And you can add "differently edited code" into main stream line of project
which is called "merging"
======================================================================
The reason you do merging and branching is to do "non-linear programming"
======================================================================
Suppose you have copied project A from repository
And main stream line of this project A is called master
======================================================================
You edit A and get B
You edit B and get C
And master pointer points to latest code (C)
======================================================================
You create branch bug123
# -b: for creating branch
git checkout -b bug123
# To see your branches
git branch
Star means code stream in which you're working now
======================================================================
You edit C and get D
You edit D and get E
bug123 branch points to E
======================================================================
# When you merge code state of bug123 branch into master branch,
# first, you checkout to master branch
======================================================================
Then, you merge "code" of bug123 branchy into master branch
git merge bug123
And merged code from master branch and bug123 branch points E (latest code)
======================================================================
Now, bug123 branch is no useful anymore
so, you can remove it
git branch -d bug123
======================================================================
Another possible scenario
======================================================================
you edit code in master branch
======================================================================
To merge 2 branches,
first, you checkout to master branch
git checkout master
======================================================================
git merge bug456
Contents (F and G) of bug456 branch are merged into master branch
and H becomes latest code
======================================================================
There is case where merging 2 branches work smoothly
when the edit (D and E) in master branch has no common parts
with the edit (F and G) in bug456 branch
======================================================================
What if "D and E changes" edited the same parts which "F and G changes" edited?
In that case, merging can't happen automatically.
In that case, developers should manually resolve conflict
and then should try to merge 2 branches again
======================================================================
After merging, you can delete bug456 branch
======================================================================