Use git log --ancestry-path
In this step, we will explore how to use the git log --ancestry-path
command. This command is useful for viewing the commit history along a specific path between two commits. It helps you understand the lineage of changes.
First, let's create a simple Git repository and make a few commits to set up a scenario for using --ancestry-path
.
Navigate to your project directory:
cd ~/project
Create a new directory for this lab and initialize a Git repository:
mkdir ancestry-lab
cd ancestry-lab
git init
You should see output indicating that an empty Git repository has been initialized:
Initialized empty Git repository in /home/labex/project/ancestry-lab/.git/
Now, let's create a file and make the first commit:
echo "Initial content" > file1.txt
git add file1.txt
git commit -m "Initial commit"
You will see output confirming the commit:
[master (root-commit) <commit-hash>] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 file1.txt
Next, let's make another commit:
echo "Adding more content" >> file1.txt
git add file1.txt
git commit -m "Add more content"
You will see output for the second commit:
[master <commit-hash>] Add more content
1 file changed, 1 insertion(+)
Now, let's create a new branch and make a commit on that branch:
git branch feature
git checkout feature
echo "Feature work" > file2.txt
git add file2.txt
git commit -m "Add feature file"
You will see output for creating the branch, switching to it, and the new commit:
Switched to a new branch 'feature'
[feature <commit-hash>] Add feature file
1 file changed, 1 insertion(+)
create mode 100644 file2.txt
Let's go back to the master branch and make another commit:
git checkout master
echo "More master work" >> file1.txt
git add file1.txt
git commit -m "More master content"
You will see output for switching branches and the new commit:
Switched to branch 'master'
[master <commit-hash>] More master content
1 file changed, 1 insertion(+)
Now we have a commit history with a branch. Let's use git log
to see the full history:
git log --all --decorate --oneline
You will see a log similar to this (commit hashes and order may vary):
<commit-hash> (HEAD -> master) More master content
<commit-hash> Add more content
<commit-hash> (feature) Add feature file
<commit-hash> Initial commit
Now, let's use git log --ancestry-path
. This command requires two commit references. It will show the commits that are ancestors of the second commit and descendants of the first commit.
Let's find the commit hash for "Initial commit" and "More master content". You can get these from the git log --all --decorate --oneline
output. Replace <initial-commit-hash>
and <master-commit-hash>
with the actual hashes from your output.
git log --ancestry-path <initial-commit-hash> <master-commit-hash> --oneline
This command will show the commits on the path from the initial commit to the latest commit on the master branch. You should see the "Initial commit", "Add more content", and "More master content" commits.
The --ancestry-path
option is useful for understanding the direct line of development between two points in your history, ignoring commits from other branches that might have been merged in later.