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.
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.
No comments:
Post a Comment