Branches in git

diverging and converging

Andy Balaam
artificialworlds.net/blog

Contents

What is a branch?

it's kind of the point

What is a branch?

Two worldviews

what planet are you on?

Two worldviews

Creating a branch

diverge

Creating a branch

$ cd sayings
$ git branch release-0.1

That's it.

Creating a branch

Check where we are:

$ git branch
* master
  release-0.1

$ git status
# On branch master
nothing to commit (working directory clean)

Switching branches

change hats

Switching branches

$ git checkout release-0.1
Switched to branch 'release-0.1'

Switching branches

Check where we are:

$ git branch
  master
* release-0.1

$ git status
# On branch release-0.1
nothing to commit (working directory clean)

Switching branches

Bug fix in 0.1

$ vim northern.txt
northern.txt
Ay up
Ee bah gum
Were tha born in a barn?
$ git commit -a -m "Fix gum spelling"
[release-0.1 100c9bd] Fix gum spelling
1 file changed, 1 insertion(+), 1 deletion(-)

Switching branches

$ git checkout master 
Switched to branch 'master'

$ cat northern.txt 
Ay up
Eey by gum
Were tha born in a barn?

Switching branches

Development work in trunk

$ vim northern.txt
northern.txt
Ay up
Eey by gum
Were tha born in a barn?
Yer not as green as yer cabbage looking
$ git commit -a -m "Add cabbage saying"
[master aa3b4a5] Add cabbage saying
 1 file changed, 1 insertion(+), 1 deletion(-)

Merging

converge

Merging

Ensure we have no uncommitted changes:

$ git status
# On branch master
nothing to commit (working directory clean)

Merging

Then do it:

$ git merge release-0.1
.git/COMMIT_EDITMSG
Merge branch 'release-0.1'

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
# 
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

Merging

Then do it:

$ git merge release-0.1
Auto-merging northern.txt
Merge made by the 'recursive' strategy.
 northern.txt |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Merging

Result:

$ cat northern.txt 
Ay up
Ee bah gum
Were tha born in a barn?
Yer not as green as yer cabbage looking

Merging

More complex merges:

$ git merge release-0.1
Auto-merging pirate.txt
CONFLICT (add/add): Merge conflict in pirate.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git mergetool

Another time...

Visualising

understand

Visualising

$ gitk

More info

Videos youtube.com/user/ajbalaam
Twitter @andybalaam
Blog artificialworlds.net/blog
Projects artificialworlds.net