Test with Unmerged Branches
In the previous steps, we saw how git branch --merged
shows branches that have been fully integrated. Now, let's see what happens when we have a branch that has not been merged into the current branch.
Make sure you are in the ~/project/my-time-machine
directory and on the master
branch. You can verify this with git status
.
Let's create a new branch called feature/add-farewell
and switch to it:
git branch feature/add-farewell
git checkout feature/add-farewell
You should see output indicating you switched branches:
Switched to branch 'feature/add-farewell'
Now, let's create a new file and make a commit on this branch:
echo "Farewell from the farewell branch!" > farewell.txt
git add farewell.txt
git commit -m "Add farewell file"
You should see output similar to this after the commit:
[feature/add-farewell a1b2c3d] Add farewell file
1 file changed, 1 insertion(+)
create mode 100644 farewell.txt
Now, let's switch back to the master
branch:
git checkout master
You should see output indicating you switched branches:
Switched to branch 'master'
We are now on the master
branch, and the feature/add-farewell
branch contains a commit that is not present in master
.
Let's use git branch --merged
again:
git branch --merged
You should see output similar to this:
* master
feature/add-greeting
Notice that feature/add-farewell
is not listed in the output. This is because the commit we made on feature/add-farewell
has not been merged into the master
branch.
Now, let's use the git branch --no-merged
command. This command shows branches that have not been merged into the current branch.
git branch --no-merged
You should see output similar to this:
feature/add-farewell
This output correctly shows feature/add-farewell
because it contains commits that are not in the master
branch.
The git branch --merged
and git branch --no-merged
commands are powerful tools for managing your branches. They help you keep track of which branches are finished and can be deleted, and which branches are still under development and need to be merged.