Working in a multi-branch system with Git can sometimes be challenging when you need to understand exactly what has happened on a particular branch. This is especially crucial during code reviews, where you want to see all the changes made since the branch was created from the
main branch (or any other sub-branch). Using
git diff main won’t give you the desired results, as it includes changes from the original branch as well.
To accomplish this, we need to combine a couple of Git commands, much like a pipeline in your command line. First, we must find the reference point where the branch was created using
git merge-base HEAD main. Then, we use this reference point with
git diff. You can achieve this by using command substitution with backticks or
git diff `git merge-base HEAD main`
You can add this command into your bash or zsh scripts, or you can even add it to your .gitconfig file, depending on your preference.
One important thing to note is that if you merge your original branch into the new one, this command loses its effectiveness. Merging is not recommended in this scenario. Instead, consider using
git rebase for a cleaner history and better clarity.