Table of contents
Brief Overview
On Day 3, we will cover:
Understanding the importance of Git history.
Using
git log
to view commit history.Customizing
git log
output.Viewing changes with
git diff
.Exploring specific commits with
git show
.
Detailed Explanation with Code
Understanding the Importance of Git History: The Git history is a record of all the commits made to a repository. It helps track changes, understand the evolution of the project, and identify who made specific changes. Reviewing the history is crucial for debugging, collaboration, and maintaining a clean project.
Using
git log
to View Commit History: Thegit log
command shows the commit history of the repository. Each commit includes information like the commit hash, author, date, and commit message.git log
Example:
git log
Output:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t Author: Your Name <youremail@example.com> Date: Mon Jul 26 12:34:56 2023 +0000 Update hello.txt with another new line commit d1e8d3b4c5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t Author: Your Name <youremail@example.com> Date: Sun Jul 25 11:23:45 2023 +0000 Add hello.txt with initial content
Customizing
git log
Output: Thegit log
command can be customized to display information in various formats. Here are some useful options:One-line Summary: Display each commit on a single line.
git log --oneline
Example:
git log --oneline
Output:
a1b2c3d Update hello.txt with another new line d1e8d3b Add hello.txt with initial content
Graphical Representation: Show a graphical representation of the commit history.
git log --graph --oneline
Example:
git log --graph --oneline
Output:
* a1b2c3d Update hello.txt with another new line * d1e8d3b Add hello.txt with initial content
Author and Date: Display the author and date of each commit.
git log --pretty=format:"%h - %an, %ar : %s"
Example:
git log --pretty=format:"%h - %an, %ar : %s"
Output:
a1b2c3d - Your Name, 2 hours ago : Update hello.txt with another new line d1e8d3b - Your Name, 1 day ago : Add hello.txt with initial content
Viewing Changes with
git diff
: Thegit diff
command shows the differences between various commits, working directory, and staging area.Unstaged Changes: Show changes that haven't been staged yet.
git diff
Example:
echo "Another change" >> hello.txt git diff
Output:
diff --git a/hello.txt b/hello.txt index 47eb3d1..1a2b3c4 100644 --- a/hello.txt +++ b/hello.txt @@ -1,2 +1,3 @@ Hello, Git! This is a new line. +Another change
Staged Changes: Show changes that have been staged but not yet committed.
git diff --staged
Example:
git add hello.txt git diff --staged
Output:
diff --git a/hello.txt b/hello.txt index 47eb3d1..1a2b3c4 100644 --- a/hello.txt +++ b/hello.txt @@ -1,2 +1,3 @@ Hello, Git! This is a new line. +Another change
Exploring Specific Commits with
git show
: Thegit show
command displays detailed information about a specific commit, including the commit message, author, date, and the changes introduced by the commit.git show <commit-hash>
Example:
git show a1b2c3d
Output:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t Author: Your Name <youremail@example.com> Date: Mon Jul 26 12:34:56 2023 +0000 Update hello.txt with another new line diff --git a/hello.txt b/hello.txt index 47eb3d1..1a2b3c4 100644 --- a/hello.txt +++ b/hello.txt @@ -1,2 +1,3 @@ Hello, Git! This is a new line. +Another change
Example Workflow
Initialize a New Git Repository:
mkdir my-git-project cd my-git-project git init
Create and Commit a File:
echo "Hello, Git!" > hello.txt git add hello.txt git commit -m "Add hello.txt with initial content"
Modify and Commit the File Again:
echo "This is a new line." >> hello.txt git add hello.txt git commit -m "Update hello.txt with new content"
View the Commit History:
git log
Customize the Commit History View:
git log --oneline git log --graph --oneline git log --pretty=format:"%h - %an, %ar : %s"
View Differences Between Commits:
echo "Another change" >> hello.txt git diff git add hello.txt git diff --staged
Show Detailed Information About a Specific Commit:
git show a1b2c3d
By the end of Day 3, readers should understand how to view and interpret the commit history using various Git commands. This knowledge is essential for tracking changes, understanding the project's evolution, and collaborating effectively.