Driving a project forward requires a myriad of small changes. Version control uses so-called "diffs" to illustrate these changes.
Inspecting Changes with Diffs
Driving a project forward requires a myriad of small changes. Understanding each of these individual changes is the key to understanding how the project evolved. Version control uses so-called "diffs" to illustrate these changes.
Before we take a closer look at such a diff, we'll be looking at the different places in Tower where diffs are displayed:
In the "Working Copy" view, Tower lists all currently changed files. Select a file in the list to see its detailed diff on the right. Using the toggle on the top right, you can even switch between staged and unstaged changes.
When you select the "History" item in the sidebar (or any branch or tag item), the associated commits are displayed in a list. Select any commit to see which files it changed on the right. You can expand each changed file item to see the detailed diff of this modification.
A diff (or, synonymously, a "patch"), is a visualization of the _diff_erences between two versions of a file. Let's take a detailed look at such a diff - and learn how to read it.
As said, a diff compares two versions of a file with each other.
When looking at local changes, the file that you currently have on disk (with your local modifications) is compared with the file as it was last committed.
When looking at committed changes, the file that's contained in this very commit is compared to the version of the same file in the commit before.
A diff doesn't show the complete file from beginning to end: you wouldn't want to see everything in a 10,000 lines file, when only 2 lines have changed. Instead, it only shows those portions that were actually modified. Such a portion is called a "chunk" (or "hunk"). In addition to the actual changed lines, a chunk also contains a bit of "context": some (unchanged, non-colored) lines before and after the modification so you can better understand in what context that change happened. In the example diff above, you can see 3 chunks.
Each of these chunks is prepended by a header. Enclosed in two "@" signs each, Git tells you which lines were affected. In the second chunk of our example diff, the following lines are represented:
From file A (represented by "-" signs), 6 lines are displayed, beginning from line no. 33
From file B (represented by "+" signs), 11 lines are shown, also starting from line no. 33
The text after the closing pair of "@@" aims to clarify the context: Git tries to display a method name or other contextual information of where this chunk was taken from in the file. However, this greatly depends on the programming language and doesn't work in all scenarios.
Each changed line is prepended with either a "+" or a "-" symbol. These symbols help you associate the different lines with the correct version: a line that is prepended with a "-" sign comes from A, while a line with a "+" sign comes from B. Tower helps make this distinction even easier by assigning each version a background color.
In most cases, Git picks A and B in such a way that you can think of A/- as "old" content and B/+ as "new" content.
Let's look at our concrete example:
In chunk #1, a line was modified: the red "-" line was changed to look like the green "+" line below.
Chunk #2 contains five lines prepended with a "+". Since no counterpart in A existed for these lines (no lines with "-"), this means that these lines were added.
Chunk #3 is just the opposite: in A, we have seven lines marked with "-" signs. However, B doesn't have an equivalent (no "+" lines), meaning they were deleted.
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.