Test Non-Ancestor Commits
In the previous steps, we used git merge-base --is-ancestor
to confirm that earlier commits were ancestors of later commits on the same branch. Now, let's explore what happens when we test commits that are not ancestors of each other.
Navigate to your repository directory:
cd ~/project/git-ancestor-lab
We currently have a single branch (master
) with three commits. To test non-ancestor relationships, we need to create a new branch and make a commit on that branch. This will create a divergent history.
First, let's create a new branch called feature
:
git branch feature
This command creates a new branch pointer called feature
that points to the same commit as master
(our latest commit, <commit-hash-3>
).
Now, let's switch to the feature
branch:
git checkout feature
You should see output indicating you've switched branches:
Switched to branch 'feature'
We are now on the feature
branch. Let's make a new commit on this branch. Create a new file:
echo "Feature content" > file2.txt
git add file2.txt
git commit -m "Add file2 on feature branch"
You will see output confirming the commit on the feature
branch:
[feature <commit-hash-4>] Add file2 on feature branch
1 file changed, 1 insertion(+)
create mode 100644 file2.txt
Now, let's look at the history using git log --oneline --all --graph
. The --all
flag shows commits from all branches, and --graph
draws a text-based representation of the commit history.
git log --oneline --all --graph
The output will show a branching history. It might look something like this (commit hashes will vary):
* <commit-hash-4> (HEAD -> feature) Add file2 on feature branch
* <commit-hash-3> (master) Add final content to file1
* <commit-hash-2> Add more content to file1
* <commit-hash-1> Add file1 with initial content
In this graph, <commit-hash-4>
is the latest commit on the feature
branch, and <commit-hash-3>
is the latest commit on the master
branch. These two commits are not ancestors of each other. They share a common ancestor, which is <commit-hash-3>
(the commit where the feature
branch was created).
Let's use git merge-base --is-ancestor
to test the relationship between <commit-hash-4>
and <commit-hash-3>
. Replace the placeholders with your actual commit hashes.
git merge-base --is-ancestor <commit-hash-4> <commit-hash-3>
echo $?
This command checks if <commit-hash-4>
is an ancestor of <commit-hash-3>
. Based on our graph, it is not. Therefore, the command should exit with a status of 1
.
Now, let's test the other way around: is <commit-hash-3>
an ancestor of <commit-hash-4>
?
git merge-base --is-ancestor <commit-hash-3> <commit-hash-4>
echo $?
This command checks if <commit-hash-3>
is an ancestor of <commit-hash-4>
. Looking at the graph, <commit-hash-4>
's parent is <commit-hash-3>
. So, <commit-hash-3>
is an ancestor of <commit-hash-4>
. The command should exit with a status of 0
.
This demonstrates how git merge-base --is-ancestor
can be used to programmatically check the relationship between any two commits in your repository's history, even across different branches.