Currently, I am curious to find out what Git will do for us behind the scene if we run the 'git branch' command. Below are the logs coming from my experiment. 
Reference: Git Book
Preparation
Create a brand new folder and run the 'git init' command.
Step 1: 
Check the .git tree structure.
    $ tree .git/
    .git/
    ├── HEAD
    ├── branches
    ├── config
    ├── description
    ├── hooks
    │   ├── applypatch-msg.sample
    │   ├── commit-msg.sample
    │   ├── fsmonitor-watchman.sample
    │   ├── post-update.sample
    │   ├── pre-applypatch.sample
    │   ├── pre-commit.sample
    │   ├── pre-merge-commit.sample
    │   ├── pre-push.sample
    │   ├── pre-rebase.sample
    │   ├── pre-receive.sample
    │   ├── prepare-commit-msg.sample
    │   ├── push-to-checkout.sample
    │   └── update.sample
    ├── info
    │   └── exclude
    ├── objects
    │   ├── info
    │   └── pack
    └── refs
        ├── heads
        └── tags
    9 directories, 17 files
 
HEAD is the special pointer which can point to a branch pointer or a commit (detach HEAD)
  
    $ cat .git/HEAD
    ref: refs/heads/master
    $ cat .git/refs/heads/master
    cat: .git/refs/heads/master: No such file or directory
Create a new commit in the current branch 'master'.
  
    $ echo 'test' > test.txt
    $ git add .
    $ git commit -m 'first commit'
    [master (root-commit) 7353d8d] first commit
     1 file changed, 1 insertion(+)
     create mode 100644 test.txt
Check the .git tree structure.
    $ tree .git/
    .git/
    ├── COMMIT_EDITMSG (a new file)    
    ├── HEAD
    ├── branches
    ├── config
    ├── description
    ├── hooks
    │   ├── applypatch-msg.sample
    │   ├── commit-msg.sample
    │   ├── fsmonitor-watchman.sample
    │   ├── post-update.sample
    │   ├── pre-applypatch.sample
    │   ├── pre-commit.sample
    │   ├── pre-merge-commit.sample
    │   ├── pre-push.sample
    │   ├── pre-rebase.sample
    │   ├── pre-receive.sample
    │   ├── prepare-commit-msg.sample
    │   ├── push-to-checkout.sample
    │   └── update.sample
    ├── index (a new file)    
    ├── info
    │   └── exclude
    ├── logs (a new directory)  
    │   ├── HEAD (a new file)    
    │   └── refs (a new directory)  
    │       └── heads (a new directory)  
    │           └── master (a new file)    
    ├── objects    │   ├── 2b (a new directory)  
    │   │   └── 297e643c551e76cfa1f93810c50811382f9117 (a new file for git tree object)    
    │   ├── 73 (a new directory)  
    │   │   └── 53d8d4755041cbd9600ce8a1c2f5e7e35646ea (a new file for git commit object)    
    │   ├── 9d (a new directory)  
    │   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4 (a new file for git blob object)    
    │   ├── info
    │   └── pack
    └── refs
        ├── heads
        │   └── master (a new file)    
        └── tags
    15 directories, 25 files
 
Check the HEAD pointer and branch pointers info.
  
    $ cat .git/HEAD
    ref: refs/heads/master
    $ cat .git/refs/heads/master
    7353d8d4755041cbd9600ce8a1c2f5e7e35646ea
    $ git log --oneline --decorate  --graph --all
    * 7353d8d (HEAD -> master) first commit
Step 2: 
Create a hotfix branch.
(The following command will just create a branch, but the HEAD pointer will still point to the current branch 'master')
Check the .git tree structure.
    $ tree .git/
    .git/
    ├── COMMIT_EDITMSG     
    ├── HEAD
    ├── branches
    ├── config
    ├── description
    ├── hooks
    │   ├── applypatch-msg.sample
    │   ├── commit-msg.sample
    │   ├── fsmonitor-watchman.sample
    │   ├── post-update.sample
    │   ├── pre-applypatch.sample
    │   ├── pre-commit.sample
    │   ├── pre-merge-commit.sample
    │   ├── pre-push.sample
    │   ├── pre-rebase.sample
    │   ├── pre-receive.sample
    │   ├── prepare-commit-msg.sample
    │   ├── push-to-checkout.sample
    │   └── update.sample
    ├── index   
    ├── info
    │   └── exclude
    ├── logs  
    │   ├── HEAD    
    │   └── refs  
    │       └── heads  
    │           ├── hotfix (a new file)    
    │           └── master  
    ├── objects    │   ├── 2b   
    │   │   └── 297e643c551e76cfa1f93810c50811382f9117    
    │   ├── 73 
    │   │   └── 53d8d4755041cbd9600ce8a1c2f5e7e35646ea     
    │   ├── 9d  
    │   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4 
    │   ├── info
    │   └── pack
    └── refs
        ├── heads
        │   ├── hotfix  (a new file)    
        │   └── master
        └── tags
    15 directories, 27 files
 
Check the HEAD pointer and branch pointers info.
  
    $ cat .git/HEAD
    ref: refs/heads/master
    $ cat .git/refs/heads/master
    7353d8d4755041cbd9600ce8a1c2f5e7e35646ea
    $ cat .git/refs/heads/hotfix 
    7353d8d4755041cbd9600ce8a1c2f5e7e35646ea
    $ git log --oneline --decorate  --graph --all
    * 7353d8d (HEAD -> master, hotfix) first commit
Add a new commit in the current branch 'master'
  
    $ echo 'test2' > test2.txt
    $ git add .
    $ git commit -m 'second commit from master branch'
    [master eef89eb] second commit from master branch
     1 file changed, 1 insertion(+)
     create mode 100644 test2.txt
Check the .git tree structure.
    $ tree .git/
    .git/
    ├── COMMIT_EDITMSG     
    ├── HEAD
    ├── branches
    ├── config
    ├── description
    ├── hooks
    │   ├── applypatch-msg.sample
    │   ├── commit-msg.sample
    │   ├── fsmonitor-watchman.sample
    │   ├── post-update.sample
    │   ├── pre-applypatch.sample
    │   ├── pre-commit.sample
    │   ├── pre-merge-commit.sample
    │   ├── pre-push.sample
    │   ├── pre-rebase.sample
    │   ├── pre-receive.sample
    │   ├── prepare-commit-msg.sample
    │   ├── push-to-checkout.sample
    │   └── update.sample
    ├── index   
    ├── info
    │   └── exclude
    ├── logs  
    │   ├── HEAD    
    │   └── refs  
    │       └── heads  
    │           ├── hotfix 
    │           └── master  
    ├── objects    │   ├── 18 (a new directory)    
    │   │   └── 0cf8328022becee9aaa2577a8f84ea2b9f3827 (a new file for git blob object)    
    │   ├── 2b   
    │   │   └── 297e643c551e76cfa1f93810c50811382f9117    
    │   ├── 59 (a new directory)
    │   │   └── 50f5c8ec9efcb96612f89264fd72994badb828 (a new file for git tree object)    
    │   ├── 73 
    │   │   └── 53d8d4755041cbd9600ce8a1c2f5e7e35646ea     
    │   ├── 9d  
    │   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4 
    │   ├── ee (a new directory)
    │   │   └── f89ebbefa9af58ca3f5c7a86a12416bf9c74aa (a new file for git commit object)    
    │   ├── info
    │   └── pack
    └── refs
        ├── heads
        │   ├── hotfix  
        │   └── master
        └── tags
    18 directories, 30 files
 
Check the HEAD pointer and branch pointers info.
  
    $ cat .git/HEAD
    ref: refs/heads/master
    $ cat .git/refs/heads/master
    eef89ebbefa9af58ca3f5c7a86a12416bf9c74aa
    $ cat .git/refs/heads/hotfix 
    7353d8d4755041cbd9600ce8a1c2f5e7e35646ea
    $ git log --oneline --decorate  --graph --all
    * eef89eb (HEAD -> master) second commit from master branch
    * 7353d8d (hotfix) first commit
From the result, it is a linear graph.
The current branch will keep moving to point to the latest commit.
Step 3: 
Run the 'git checkout' command to switch branch.
    Switched to branch 'hotfix'
Check the HEAD info.
  
    $ cat .git/HEAD
    ref: refs/heads/hotfix
Create a new commit.
  
    $ echo 'test3' > test3.txt
    $ git add .
    $ git commit -m 'fix bug in hotfix branch'
    [hotfix b135500] fix bug in hotfix branch
     1 file changed, 1 insertion(+)
     create mode 100644 test3.txt
Check the .git tree structure.
    $ tree .git/
    .git/
    ├── COMMIT_EDITMSG     
    ├── HEAD
    ├── branches
    ├── config
    ├── description
    ├── hooks
    │   ├── applypatch-msg.sample
    │   ├── commit-msg.sample
    │   ├── fsmonitor-watchman.sample
    │   ├── post-update.sample
    │   ├── pre-applypatch.sample
    │   ├── pre-commit.sample
    │   ├── pre-merge-commit.sample
    │   ├── pre-push.sample
    │   ├── pre-rebase.sample
    │   ├── pre-receive.sample
    │   ├── prepare-commit-msg.sample
    │   ├── push-to-checkout.sample
    │   └── update.sample
    ├── index   
    ├── info
    │   └── exclude
    ├── logs  
    │   ├── HEAD    
    │   └── refs  
    │       └── heads  
    │           ├── hotfix 
    │           └── master  
    ├── objects    │   ├── 18 
    │   │   └── 0cf8328022becee9aaa2577a8f84ea2b9f3827     
    │   ├── 2b   
    │   │   └── 297e643c551e76cfa1f93810c50811382f9117    
    │   ├── 59 
    │   │   └── 50f5c8ec9efcb96612f89264fd72994badb828    
    │   ├── 73 
    │   │   └── 53d8d4755041cbd9600ce8a1c2f5e7e35646ea     
    │   ├── 8c (a new directory)
    │   │   └── 281897622f3e3750998f1509b7c441cbb1f3e2 (a new file for git tree object)
    │   ├── 9d  
    │   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4 
    │   ├── b1 (a new directory)
    │   │   └── 35500a8511bfa6f8a75d219a0d7e3951bedf78 (a new file for git commit object)
    │   ├── df (a new directory)
    │   │   └── 6b0d2bcc76e6ec0fca20c227104a4f28bac41b (a new file for git blob object)
    │   ├── ee 
    │   │   └── f89ebbefa9af58ca3f5c7a86a12416bf9c74aa    
    │   ├── info
    │   └── pack
    └── refs
        ├── heads
        │   ├── hotfix  
        │   └── master
        └── tags
    21 directories, 33 files
 
Check the HEAD pointer and branch pointers info.
  
    $ cat .git/HEAD
    ref: refs/heads/hotfix
    $ cat .git/refs/heads/master
    eef89ebbefa9af58ca3f5c7a86a12416bf9c74aa
    $ cat .git/refs/heads/hotfix 
    b135500a8511bfa6f8a75d219a0d7e3951bedf78
    $ git log --oneline --decorate --graph
    * b135500 (HEAD -> hotfix) fig bug in hotfix branch
    | * eef89eb (master) second commit from master branch
    |/
    * 7353d8d first commit
It is a diverge graph, not a linear graph.
NOTE: We can use 'git checkout -b xxx' as a shortcut to create the xxx branch and switch to it directly.
 
No comments:
Post a Comment