Test with Partially Merged Branch
In the previous steps, we saw how git branch --merged
and git log main ^branch
work with a fully merged branch. Now, let's see what happens with a branch that is partially merged, meaning some, but not all, of its commits have been integrated into another branch.
First, make sure you are in the my-time-machine
directory:
cd ~/project/my-time-machine
Let's create a new branch called partial-feature
and switch to it:
git branch partial-feature
git checkout partial-feature
Now, we'll make two commits on this new branch:
echo "Adding part 1 of the feature" >> partial.txt
git add partial.txt
git commit -m "Add part 1"
echo "Adding part 2 of the feature" >> partial.txt
git add partial.txt
git commit -m "Add part 2"
You should see output similar to this after the commits:
[partial-feature a1b2c3d] Add part 1
1 file changed, 1 insertion(+)
create mode 100644 partial.txt
[partial-feature e1f2g3h] Add part 2
1 file changed, 1 insertion(+)
Now, let's switch back to the master
branch and merge only the first commit from partial-feature
. We can do this using git cherry-pick
. First, we need the commit hash of the "Add part 1" commit. You can find this by running git log partial-feature
and copying the hash of the first commit.
git checkout master
## Replace <commit_hash> with the actual hash of the "Add part 1" commit
git cherry-pick <commit_hash>
You should see output indicating the cherry-pick was successful:
[master i1j2k3l] Add part 1
Date: Mon Aug 7 10:05:00 2023 +0000
1 file changed, 1 insertion(+)
create mode 100644 partial.txt
Now, let's use git branch --merged master
again:
git branch --merged master
You should see master
and feature-branch
listed, but not partial-feature
. This is because partial-feature
still has the "Add part 2" commit which has not been merged into master
. git branch --merged
only lists branches where all commits are present in the target branch.
Finally, let's use git log master ^partial-feature
:
git log master ^partial-feature
This command will show you the commits that are in master
but not in partial-feature
. It should show the initial commit(s) from master
, the "Add new feature" commit (which is now in master), and the "Add part 1" commit (which we cherry-picked). It should not show the "Add part 2" commit, as that commit is only on partial-feature
.
This demonstrates how git branch --merged
and git log main ^branch
can help you understand the merge status of your branches, even when only some commits have been integrated.