Tuesday, June 27, 2023

git tag

The concept is similar between tags and branches, but branches are mutated, which always points to the latest commit. Unlike git branches, git tags are static.


Currently, I am curious to find out what Git will do for us behind the scene if we run the 'git tag' commands. Below are the logs coming from my experiment. 


Reference: Git Book


Preparation


Create a brand new folder and run the 'git init' command.

Then run the following commands.


    $ echo 'test' > test.txt
$ git add .
$ git commit -m 'first commit'
[master (root-commit) f2cce29] first commit 1 file changed, 1 insertion(+) create mode 100644 test.txt

    $ echo 'test2' > test2.txt
$ git add .
$ git commit -m 'second commit'
[master 060872d] second commit 1 file changed, 1 insertion(+) create mode 100644 test2.txt

    $ git checkout -b add-billing-feature
Switched to a new branch 'add-billing-feature'

$ echo 'add billing feature' > billing.py
$ git add .
$ git commit -m 'add billing feature'
[add-billing-feature 534f26a] add billing feature 1 file changed, 1 insertion(+) create mode 100644 billing.py


Step 1:


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
│   ├── add-billing-feature │   └── master ├── objects │   ├── 06 │   │   ├── 03a9a9c4879280e7e99294fb3b40a2697a822a │   │   └── 0872df77629e6b5a69c75013d9970f16f4135f │   ├── 18 │   │   └── 0cf8328022becee9aaa2577a8f84ea2b9f3827 │   ├── 2b │   │   └── 297e643c551e76cfa1f93810c50811382f9117 │   ├── 53 │   │   └── 4f26aaf0fbdbc18a435597eceb32938ab0bcb8 │   ├── 59 │   │   └── 50f5c8ec9efcb96612f89264fd72994badb828 │   ├── 9d │   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4 │   ├── c6 │   │   └── e79a24bc5663ca0d968459e154a6206168b8be │   ├── f2 │   │   └── cce29b0158c070ef5cdd2c90e6ed2307e7dd7d │   ├── info │   └── pack └── refs ├── heads
│   ├── add-billing-feature │   └── master └── tags 20 directories, 33 files


Check the HEAD pointer and branch pointers info.


    $ cat .git/HEAD
ref: refs/heads/add-billing-feature

$ cat .git/refs/heads/master
060872df77629e6b5a69c75013d9970f16f4135f

$ cat .git/refs/heads/add-billing-feature
534f26aaf0fbdbc18a435597eceb32938ab0bcb8

$ git log --oneline --decorate --graph --all
* 534f26a (HEAD -> add-billing-feature) add billing feature * 060872d (master) second commit * f2cce29 first commit



Step 2:


Run the 'git checkout' command to checkout the first commit 'f2cce29'.

Then create a lightweight tag with the following comands.


    $ git checkout f2cce29
Note: switching to 'f2cce29'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name> Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at f2cce29 first commit

$ git tag v1.0.0


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
│   ├── add-billing-feature │   └── master ├── objects │   ├── 06 │   │   ├── 03a9a9c4879280e7e99294fb3b40a2697a822a │   │   └── 0872df77629e6b5a69c75013d9970f16f4135f │   ├── 18 │   │   └── 0cf8328022becee9aaa2577a8f84ea2b9f3827 │   ├── 2b │   │   └── 297e643c551e76cfa1f93810c50811382f9117 │   ├── 53 │   │   └── 4f26aaf0fbdbc18a435597eceb32938ab0bcb8 │   ├── 59 │   │   └── 50f5c8ec9efcb96612f89264fd72994badb828 │   ├── 9d │   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4 │   ├── c6 │   │   └── e79a24bc5663ca0d968459e154a6206168b8be │   ├── f2 │   │   └── cce29b0158c070ef5cdd2c90e6ed2307e7dd7d │   ├── info │   └── pack └── refs ├── heads
│   ├── add-billing-feature │   └── master └── tags
└── v1.0.0 (a new file) 20 directories, 34 files

From the logs, lightweight tags will not create any type of git objects.


Check the HEAD pointer, branch pointers and tags pointers.


    $ cat .git/HEAD
f2cce29b0158c070ef5cdd2c90e6ed2307e7dd7d

$ cat .git/refs/heads/master
060872df77629e6b5a69c75013d9970f16f4135f

$ cat .git/refs/heads/add-billing-feature
534f26aaf0fbdbc18a435597eceb32938ab0bcb8

    $ cat .git/refs/tags/v1.0.0
f2cce29b0158c070ef5cdd2c90e6ed2307e7dd7d

$ git log --oneline --decorate --graph --all
* 534f26a (add-billing-feature) add billing feature * 060872d (master) second commit * f2cce29 (HEAD, tag: v1.0.0) first commit



Step 3:


Follow the commands below to add a new commit.


    $ echo 'first patch' > first_patch.py
$ git add .
$ git commit -m 'first patch'
[detached HEAD b09a2bb] first patch 1 file changed, 1 insertion(+) create mode 100644 first_patch.py


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
│   ├── add-billing-feature │   └── master ├── objects │   ├── 06 │   │   ├── 03a9a9c4879280e7e99294fb3b40a2697a822a │   │   └── 0872df77629e6b5a69c75013d9970f16f4135f │   ├── 18 │   │   └── 0cf8328022becee9aaa2577a8f84ea2b9f3827 │   ├── 2b │   │   └── 297e643c551e76cfa1f93810c50811382f9117 │   ├── 53 │   │   └── 4f26aaf0fbdbc18a435597eceb32938ab0bcb8 │   ├── 59 │   │   └── 50f5c8ec9efcb96612f89264fd72994badb828 
│   ├── 6e (a new directory) │   │   └── 77be4c789d55cbe3df13541a2aa7d4e748c583 (a new file for git tree object)
│   ├── 9d │   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4 
│   ├── ae (a new directory) │   │   └── 2b2106e3c354a9736f912d17c7960de73786d6 (a new file for git blob object) │   ├── b0 (a new directory) │   │   └── 9a2bb68d204f6948330aff2fa6d836bb2574b1 (a new file for git commit object)
│   ├── c6 │   │   └── e79a24bc5663ca0d968459e154a6206168b8be │   ├── f2 │   │   └── cce29b0158c070ef5cdd2c90e6ed2307e7dd7d │   ├── info │   └── pack └── refs ├── heads
│   ├── add-billing-feature │   └── master └── tags
└── v1.0.0 23 directories, 37 files


Check the HEAD pointer, branch pointers, and tags pointers.


    $ cat .git/HEAD
b09a2bb68d204f6948330aff2fa6d836bb2574b1

$ cat .git/refs/heads/master
060872df77629e6b5a69c75013d9970f16f4135f

$ cat .git/refs/heads/add-billing-feature
534f26aaf0fbdbc18a435597eceb32938ab0bcb8

    $ cat .git/refs/tags/v1.0.0
f2cce29b0158c070ef5cdd2c90e6ed2307e7dd7d

$ git log --oneline --decorate --graph --all
* b09a2bb (HEAD) first patch | * 534f26a (add-billing-feature) add billing feature | * 060872d (master) second commit |/ * f2cce29 (tag: v1.0.0) first commit


Unlike branches, tags is static. (If it was a branch, then that branch will point to the latest commit.)



Step 4:


Follow the commands below to create an 'annotated tag'


    $ git tag -a v1.0.1 -m 'v1.0.1'


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
│   ├── add-billing-feature │   └── master ├── objects │   ├── 06 │   │   ├── 03a9a9c4879280e7e99294fb3b40a2697a822a │   │   └── 0872df77629e6b5a69c75013d9970f16f4135f │   ├── 18 │   │   └── 0cf8328022becee9aaa2577a8f84ea2b9f3827 │   ├── 2b │   │   └── 297e643c551e76cfa1f93810c50811382f9117 │   ├── 53 │   │   └── 4f26aaf0fbdbc18a435597eceb32938ab0bcb8 │   ├── 59 │   │   └── 50f5c8ec9efcb96612f89264fd72994badb828 
│   ├── 6e │   │   └── 77be4c789d55cbe3df13541a2aa7d4e748c583
│   ├── 74 (a new directory) │   │   └── bdf6070ad5009685cbce64b92123e767a41824 (a new file for git tree object)
│   ├── 9d │   │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4 
│   ├── ae │   │   └── 2b2106e3c354a9736f912d17c7960de73786d6 │   ├── b0 │   │   └── 9a2bb68d204f6948330aff2fa6d836bb2574b1
│   ├── c6 │   │   └── e79a24bc5663ca0d968459e154a6206168b8be │   ├── f2 │   │   └── cce29b0158c070ef5cdd2c90e6ed2307e7dd7d │   ├── info │   └── pack └── refs ├── heads
│   ├── add-billing-feature │   └── master └── tags
└── v1.0.0
└── v1.0.1 (a new file)

24 directories, 39 files


Check the type of the git object '74bdf6'.


    $ git cat-file -t 74bdf6
tag


Check the content of the git object '74bdf6'.


    $ git cat-file -p 74bdf6
object b09a2bb68d204f6948330aff2fa6d836bb2574b1 type commit tag v1.0.1 tagger Frank <frank@demo.com> datetime v1.0.1

Annotated tags will create git tag object which will record more information such as tagging message and the 


Check the HEAD pointer and branch pointers info.


    $ cat .git/HEAD
b09a2bb68d204f6948330aff2fa6d836bb2574b1

$ cat .git/refs/heads/master
060872df77629e6b5a69c75013d9970f16f4135f

$ cat .git/refs/heads/add-billing-feature
534f26aaf0fbdbc18a435597eceb32938ab0bcb8

    $ cat .git/refs/tags/v1.0.0
f2cce29b0158c070ef5cdd2c90e6ed2307e7dd7d

    $ cat .git/refs/tags/v1.0.1
74bdf6070ad5009685cbce64b92123e767a41824

$ git log --oneline --decorate --graph --all
* b09a2bb (HEAD, tag: v1.0.1) first patch
| * 534f26a (add-billing-feature) add billing feature | * 060872d (master) second commit |/ * f2cce29 (tag: v1.0.0) first commit



No comments:

Post a Comment