Tags
Today I learned how to diff between two branches.
git diff feature..main
Sometimes we get a little git add . && git commit -m "WIP"
happy and mistakenly commit something that we just can't figure out. This is a good way to figure out what the heck has changed on the current branch compared to any other branch.
Example
Let's create a new directory, initialize git and toss some content into a readme.
mkdir git-diff git init echo "hello there" > readme.md git add . && git commit -m "hello there" cat readme.md
After all of that, we have a git repository on our local machine with a single file readme.md
that contains the following.
hello there
Create a branch and โ edit
Let's checkout a new branch called Waylon and change the word there
to Waylon
in our readme.md
file, then diff it.
git checkout -b Waylon echo "hello Waylon" > readme.md git add . && git commit -m "hello Waylon" git diff
- hello there + hello waylon
At this point we have one commit. Things are really straightforward, and our diff will be the same between the last commit and the main branch since. Let's make another commit by adding the date.
echo "hello waylon\n\n$(date)" > readme.md cat readme.md git diff
hello Waylon + + Fri 13 Mar 2020 04:23:21 PM DST
๐ At this point, our diff doesn't tell us the whole story between our current state and main, only between our current state and our last commit. Let's commit our changes and compare our branch to main.
git add . && git commit -m "add date" git diff main..waylon
- hello there + hello Waylon + + Fri 13 Mar 2020 03:43:21 PM DST
Git is powerful
I learn small tricks like this often with git. Many times I forget about it and have to come back to re-learn. Sharing my thoughts gives me a better chance of remembering.