As you’re progressing with development on a topic branch, you eventually reach a point where you need to merge your work back into the main development line. While graphical tools like gitk or GitX offer intuitive ways to identify unmerged commits, let’s explore how to achieve the same from the command line. Consider the following scenario:
If you prefer the command line, you have several options available. One effective way to visualize this is by using git log, which offers a range of useful options:
git log --pretty=oneline --graph --all
You can also use the range syntax in git log to identify the commits that haven’t been merged:
git checkout master
git log ..42-adding-replies
git log master..42-adding-replies
Another option is to use git cherry, which precisely identifies commits that aren’t in the specified branch. For instance, if you’re on the master branch, you can run:
git cherry -v master 42-adding-replies
This command tells you which commits are present in the 42-adding-replies branch but not in master. The -v option prints the commit message along with the SHA hash, making it easier to identify each commit. Additionally, you can provide an optional third argument to specify a starting point or limit for the comparison.
If you’re not on the upstream branch and want to check what commits haven’t been merged into it, the process is simpler:
git checkout 42-adding-replies
git cherry master
This command compares the commits in the current branch with those in master and lists the ones that haven’t been merged yet.
An interesting aspect of git cherry is that it compares changesets rather than commit SHA hashes. This ensures accuracy, especially if you’ve modified the commit by adding a signoff or altering its parents in any way. You can rely on this command to accurately identify commits that haven’t been merged into your upstream branch. And remember, don’t confuse git cherry with cherry-pick, as they serve different purposes.