DEV Community

Waylon Walker
Waylon Walker

Posted on • Originally published at waylonwalker.com on

Today I learned `git diff master..feature`

Today I learned how to diff between two branches.

git diff master..feature
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Create a branch and ✍ edit

git branch icon

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
Enter fullscreen mode Exit fullscreen mode
- hello there
+ hello waylon
Enter fullscreen mode Exit fullscreen mode

At this point we have one commit. Things are really straightforward, and our diff will be the same between the last commit and the master branch since. Let's make another commit by adding the date.

echo "hello waylon\n\n$(date)" > readme.md
cat readme.md
git diff
Enter fullscreen mode Exit fullscreen mode
hello Waylon
+
+ Fri 13 Mar 2020 04:23:21 PM DST
Enter fullscreen mode Exit fullscreen mode

👆 At this point, our diff doesn't tell us the whole story between our current state and master, only between our current state and our last commit. Let's commit our changes and compare our branch to master.

git add . && git commit -m "add date"
git diff master..waylon
Enter fullscreen mode Exit fullscreen mode
- hello there
+ hello Waylon
+
+ Fri 13 Mar 2020 03:43:21 PM DST
Enter fullscreen mode Exit fullscreen mode

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.

Newsletter

I recently started a newsletter, you can keep up with all of the things I am working on with a monthly-ish newsletter. Sign up at waylonwalker.com/newsletter

Top comments (0)