Commands discussed in this section:
- git commit
- git log
- git show
Commits and referring to Commits
Our example repository
When you use the git commit command to commit/check-in/submit to the git repository:
- A new git tree object is created in the git object store that reflects the contents of all files and directories in the index at that point in time.
- A new git commit object is created in the git object store that refers to the new tree created.
Using the example from the index page, let’s create a new git repository, add a new file and commit the file to the repository:
$ git initInitialized empty Git repository in ...$ echo This is the README file. > README$ git add README$ git commit -m'Initial commit'[master (root-commit) 35ede5c] Initial commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 README
It looks like this:
Now that we have our first commit in the git repository, the history of commits (the “commit log”) can be displayed:
$ git logcommit 35ede5c916f88d8ba5a9dd6afd69fcaf773f70edAuthor: Tim Flagg <email@example.com>Date: Wed Feb 23 16:43:57 2011 -0800 Initial commit
Note the hash of the commit object (35ede5c) is the same in the diagram and in the output from the git log command.
This commit object contains this information:
- The hash of the git tree that represents the whole repository at the time of the commit (the tree hash is not shown in the git log output since it normally doesn’t need to be referenced by a git command).
- The commiter (“Tim Flagg”), date and time of the commit
- The text comment (“Initial commit”)
The next time a commit is done, its new commit object will have a parent commit object, which will be the value of this first commit object (35ede5c).
Commit Diagrams: Only The Commit
Typically, diagrams of commits show only commit object, but not the tree it references. So the above will be abbreviated to the following with C1 being the name of the commit. The current commit history is very boring with just the one commit:
Dig Into a Commit: git show
Another useful command for examining commit history is git show, which shows both the commit and the difference between the commit and any parent commit. In this case, there is no parent commit, since this commit is the first commit made to the repository:
$ git showcommit 35ede5c916f88d8ba5a9dd6afd69fcaf773f70edAuthor: Tim Flagg <firstname.lastname@example.org>Date: Wed Feb 23 16:43:57 2011 -0800 Initial commitdiff --git a/README b/READMEnew file mode 100644index 0000000..bccdfbd--- /dev/null+++ b/README@@ -0,0 +1 @@+This is the README file.
Adding More Commit History
Let’s add more to the repository.
$ echo One more line. >> README $ git commit -a -m "Added a second line."[master 70d1f90] Added a second line. 1 files changed, 1 insertions(+), 0 deletions(-)
This results in 2 commits in the repository:
$ git logcommit 70d1f90caa3343c1ea4a7ba238e4ae4cf9df28a0Author: Tim Flagg <email@example.com>Date: Wed Feb 23 17:00:59 2011 -0800 Added a second line.commit 35ede5c916f88d8ba5a9dd6afd69fcaf773f70edAuthor: Tim Flagg <firstname.lastname@example.org>Date: Wed Feb 23 16:43:57 2011 -0800 Initial commit
The repository’s commit history now looks like this:
Note that C1 is the first commit in the repository so it has no parent. The second commit, C2 has one parent: commit C1. The arrow pointing from C2 to C1 means the C1 commit is the parent of the C2 commit.
Next: HEAD: Where Are We? Where Were We?
Previous: git diff
The Git Commit Object
All Git Object Types: Blob, Tree, Commit And Tag
What’s The Deal With The Git Index?