Saturday, June 24, 2023

git branch

  

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')

  
$ git branch hotfix


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.

  
$ git checkout hotfix
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.


Friday, June 23, 2023

git commit amend

 

Currently, I am curious to find out what Git will do for us behind the scene if we run the 'git commit --amend' 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: 


Create two files, but use the 'git add' command to add 'test.txt' to the staging area only.

We simulated this situation when we forgot to include test2.txt in our commit later. And we will use the 'git commit --amend' command to adjust the last commit.

  
    $ echo 'test' > test.txt
    $ echo 'test2' > test2.txt
$ git add test.txt


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
├── index (a new file)
├── info │   └── exclude ├── objects
│   ├── 9d (a new directory)
│   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4 (a new file for git blob object)
│   ├── info │   └── pack └── refs ├── heads └── tags 10 directories, 19 files

 

Run the 'git commit' command.

  
    $ git commit -m 'first commit'

[master (root-commit) 8e8f211] 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 
├── 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)
│   ├── 8e  (a new directory) │   │   └── 8f2115bdaf7c00b68a61ceaf5f924846a333ac  (a new file for git commit object)
│   ├── 9d
│   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4
│   ├── info │   └── pack └── refs ├── heads
    │   └── master (a new file)
└── tags 15 directories, 25 files


Check the content of the git commit object '8e8f211'


    $ git cat-file -p 8e8f211
    tree 2b297e643c551e76cfa1f93810c50811382f9117
    author Frank <frank@demo.com>
    committer Frank <frank@demo.com>

    first commit        


Check the content of the git tree object '2b297e'. 


    $ git cat-file -p 2b297e
    100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test.txt


Check the git log history.


    $ git log
commit 8e8f2115bdaf7c00b68a61ceaf5f924846a333ac (HEAD -> master)
Author Frank <frank@demo.com>
Date:   Date...

    first commit


Step 2:


Use the 'git add' command to add the missing file 'test2.txt' to the staging area.

  
$ git add 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 
    │           └── master 
├── objects
    │   ├── 18 (a new directory)
    │   │   └── 0cf8328022becee9aaa2577a8f84ea2b9f3827 (a new file for git commit object)
    │   ├── 2b 
    │   │   └── 297e643c551e76cfa1f93810c50811382f9117 
│   ├── 8e  │   │   └── 8f2115bdaf7c00b68a61ceaf5f924846a333ac
│   ├── 9d
│   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4
│   ├── info │   └── pack └── refs ├── heads
    │   └── master (a new file)
└── tags 16 directories, 26 files


Run the 'git commit --amend' command to commit this missing file.

(It will show the prompt to ask about your actions.)

  
    $ git commit --amend

[master 737ee84] first commit Date: Fri Jun 23 00:03:02 2023 -0700 2 files changed, 2 insertions(+) create mode 100644 test.txt 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 
    │           └── master 
├── objects
    │   ├── 18 
    │   │   └── 0cf8328022becee9aaa2577a8f84ea2b9f3827 
    │   ├── 2b 
    │   │   └── 297e643c551e76cfa1f93810c50811382f9117 
│   ├── 59 (a new directory) │   │   └── 50f5c8ec9efcb96612f89264fd72994badb828 (a new file for git tree object) │   ├── 73 (a new directory) │   │   └── 7ee8418424688cd92a9287fe0866b6174456ee (a new file for git commit object)
│   ├── 8e  │   │   └── 8f2115bdaf7c00b68a61ceaf5f924846a333ac
│   ├── 9d
│   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4
│   ├── info │   └── pack └── refs ├── heads
    │   └── master (a new file)
└── tags 18 directories, 28 files


Check the content of the git commit object '737ee84'


    $ git cat-file -p 737ee84
    tree 5950f5c8ec9efcb96612f89264fd72994badb828
    author Frank <frank@demo.com>
    committer Frank <frank@demo.com>

    first commit        


Check the content of the git tree object '5950f5'. 


    $ git cat-file -p 5950f5
100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test.txt 100644 blob 180cf8328022becee9aaa2577a8f84ea2b9f3827 test2.txt


Check the git log history.


    $ git log
commit 737ee8418424688cd92a9287fe0866b6174456ee (HEAD -> master)
Author Frank <frank@demo.com>
Date:   Date...

    first commit


Using the 'git commit --amend' command, we replaced the last commit successfully.

And the old git commit or tree object would not be removed.