git branch

CommandNew filesChanged filesDeleted files
Alice/> git init
Initialized empty Git repository in /home/rene/github/github/git-internals/repos/branch/Alice/.git/
snap no: 1

Alice creates the (empty) file numbers.txt

Alice/> touch numbers.txt
snap no: 2

She adds the file to the index …

Alice/> git add . 
snap no: 3

… and commits the addition of the file.

Alice/> git commit -m "Adding numbers.txt"
[master (root-commit) b9a67b1] Adding numbers.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 numbers.txt
snap no: 4
.git/COMMIT_EDITMSG
.git/logs/HEAD
.git/logs/refs/heads/master
.git/objects/20/bc1e2d5da43d25aff12bb23d3b5e8e64a6367f [tree]
.git/objects/b9/a67b17e19af09a9736ebb2227aa8a598f27404 [commit]
.git/refs/heads/master

Adding some numbers to the file. Note the typo (tow instead of two)

Alice/> printf "one\ntow\nthree\nfour\n" > numbers.txt
snap no: 5
Alice/> git commit numbers.txt -m "Numbers 1 through 4"
[master 42c3a08] Numbers 1 through 4
 1 file changed, 4 insertions(+)
snap no: 6
.git/COMMIT_EDITMSG
.git/index
.git/logs/HEAD
.git/logs/refs/heads/master
.git/refs/heads/master

Create a branch to fix the typo:

Alice/> git branch fixTypoBranch

It creates the two files .git/logs/refs/heads/fixTypoBranch and .git/refs/heads/fixTypoBranch. These files will later be deleted again when the branch will be deleted.

snap no: 7
.git/logs/refs/heads/fixTypoBranch
.git/refs/heads/fixTypoBranch

Show "available" branches

Alice/> git branch
  fixTypoBranch
* master

The star indicates the "current" branch

snap no: 8

Change to the newly created branch

Alice/> git checkout fixTypoBranch
Switched to branch 'fixTypoBranch'

The content of .git/HEAD changes from refs/heads/master to refs/heads/fixTypoBranch

snap no: 9
.git/HEAD
.git/logs/HEAD

Fix the typo

Alice/> sed -i s/tow/two/ numbers.txt
snap no: 10

Make sure that the sed command did its job

Alice/> cat numbers.txt
one
two
three
four
snap no: 11

Commit the fix

Alice/> git commit -a -m "Fix typo"
[fixTypoBranch 1dde689] Fix typo
 1 file changed, 1 insertion(+), 1 deletion(-)
snap no: 12
.git/COMMIT_EDITMSG
.git/index
.git/logs/HEAD
.git/logs/refs/heads/fixTypoBranch
.git/refs/heads/fixTypoBranch

Go back to the original master branch. Note, the fix is not yet commited.

Alice/> git checkout master
Switched to branch 'master'

The content of .git/HEAD changes back to refs/heads/master.

snap no: 13
.git/HEAD
.git/index
.git/logs/HEAD
numbers.txt

The typo is fixed in the fixTypoBranch. Because it had not been merged into the master branch, the typo is still there:

Alice/> cat numbers.txt
one
tow
three
four
snap no: 14

Also, the "Fix typo" commit is not present in the master branch

Alice/> git log --pretty=format:"%h %ar%x09%s"
42c3a08 9 minutes ago	Numbers 1 through 4
b9a67b1 11 minutes ago	Adding numbers.txt
snap no: 15

Add a few numbers:

Alice/> printf "five\nsix\n" >> numbers.txt
snap no: 16

Commit the addition of 5 and 6:

Alice/> git commit numbers.txt -m "Added 5 and 6"
[master 17468ca] Added 5 and 6
 1 file changed, 2 insertions(+)
snap no: 17
.git/COMMIT_EDITMSG
.git/index
.git/logs/HEAD
.git/logs/refs/heads/master
.git/refs/heads/master

Showing the commit tree:

Alice/> git log --branches --graph --oneline --decorate
* 17468ca (HEAD -> master) Added 5 and 6
| * 1dde689 (fixTypoBranch) Fix typo
|/  
* 42c3a08 Numbers 1 through 4
* b9a67b1 Adding numbers.txt

Read the log from bottom upwards. Asterisks signify commits.

snap no: 18

By switching to the fixTypoBranch-branch, it can be shown that the branches are truly independent from one another.

Alice/> git checkout fixTypoBranch
Switched to branch 'fixTypoBranch'
snap no: 19
.git/HEAD
.git/index
.git/logs/HEAD
numbers.txt

In fixTypoBranch, the numbers 5 and 6 don't exist, but the typo is fixed.

Alice/> cat numbers.txt
one
two
three
four
snap no: 20

Going back to master.

Alice/> git checkout master
Switched to branch 'master'
snap no: 21
.git/HEAD
.git/index
.git/logs/HEAD
numbers.txt

Int the master branch, the numbers 5 and 6 exist, but also does the typo:

Alice/> cat numbers.txt
one
tow
three
four
five
six
snap no: 22

Merge the typo fix from fixTypoBranch

Alice/> git merge fixTypoBranch
Auto-merging numbers.txt
Merge made by the 'recursive' strategy.
 numbers.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
snap no: 23
.git/index
.git/logs/HEAD
.git/logs/refs/heads/master
.git/refs/heads/master
numbers.txt

Showing the effects of the merge: numbers 5 and 6 exist and the typo has gone.

Alice/> cat numbers.txt
one
two
three
four
five
six
snap no: 24

A merge creates a seperate commit object (In this case: fcc8c66…). So (unlike Subversion(?), it needs not be commited).

Alice/> git log --branches --graph --oneline --decorate
*   fcc8c66 (HEAD -> master) Merge branch 'fixTypoBranch'
|\  
| * 1dde689 (fixTypoBranch) Fix typo
* | 17468ca Added 5 and 6
|/  
* 42c3a08 Numbers 1 through 4
* b9a67b1 Adding numbers.txt
snap no: 25

Note: the commit object of a merge has two parents

Alice/> git cat-file -p fcc8c66
tree ddaedce41157e122ba35e9b085b0bd97a3ab586e
parent 17468ca3631f1e67e7a751830273b5665575bd73
parent 1dde6893e90b0e9d53b9fd344e4eb2d3a0480c71
author René Nyffenegger <rene.nyffenegger@adp-gmbh.ch> 1451604120 +0100
committer René Nyffenegger <rene.nyffenegger@adp-gmbh.ch> 1451604120 +0100

Merge branch 'fixTypoBranch'
snap no: 26

The fixTypoBranch is not needed anymore, we can delete it.

Alice/> git branch -d fixTypoBranch
Deleted branch fixTypoBranch (was 1dde689).

This deletes the two files ./git/logs/refs/heads/fixTypoBranch and ./git/refs/heads/fixTypoBranch that were previously created with the git branch command. Note: the objects of the branch are not deleted.

snap no: 27
.git/logs/refs/heads/fixTypoBranch
.git/refs/heads/fixTypoBranch

The branch is gone.

Alice/> git branch
* master
snap no: 28