It certainly looks the most cryptic one, but it’s actually quite easy to understand. Here we have an indication of a new line “added.” See #7.įinally, this indicates the last line I added. In fact, it was changed, but that’s effectively the same. This line indicates that the “more text” line was removed. We’ll skip this one now and get back to it later. The minus sign here means original, or old. We also see the mode (100644), which indicates this is a regular, non-executable, file. This line shows us the short versions of the hash of the original and changed versions of the file. Here, we have the traditional a and b as prefixes. The output starts by declaring this is a diff in the Git format, comparing these two files. ![]() Let’s now walk you through that output line by line: Now, run git diff without any arguments and you’ll get a result like the following: Open the file.txt file using your favorite text editor.įor inspiration, this is what my file looks like after the changes: Start by committing the current change with git commit -am “Add new line”. But first, we’ll need a slightly more complex change. As you’ll see, it’s easier than it looks. Git config -global diff.noprefix true What Does the Diff Output Mean?Īs promised, we’ll now cover the git diff output in more detail. Git diff -src-prefix=first -dst-prefix=secondįinally, you can get rid of prefixes for good by changing your global configuration: You can also define custom prefixes for the source, destination or both with the -src-prefix and -dst-prefix options: If you’re one of them, it’s easy to turn off prefixes by using the -no-prefix option: The a and b in the line are what we call “prefixes.” They indicate the source and origin for the comparison. The first line of the output from the previous example was diff -git a/file.txt b/file.txt. Now, let’s start going deeper by answering common questions people have about it. Going Deeper With Git Diff: 3 “What” Questions The actual information that was added was the line “more text”. The “b” version got something added to it (as evidenced by the plus signs). Git is comparing two versions of the same file (a/file.txt and b/file.txt). The output of git diff can be pretty overwhelming at first. The output might have different colors, depending on your configurations. Just run git diff and you’ll see a result like the following: You’re now ready to perform your first comparison. The next step is appending a new line to the end of the file: We’ll start by creating a new repository, adding a single file and committing it: How Do We Use Git Diff? A Minimum ExampleĪfter covering the “what” and “why” of git diff, let’s proceed to the “how” by showing a minimal example of this command in action. You can see the changes made since the last commit, to review what will go into the next commit. It’s also possible to see the evolution of a single file through time, so you can, for instance, understand how a recent refactoring affected it. You can compare two branches to see the result of a future merge. It might help you track down how a bug was introduced. Why Do We Use Git Diff?īeing able to make comparisons in your project is extremely useful: We’ll talk about this format later for now, let’s focus on the reasons why this command is useful. The output from git diff comes in a standardized format that makes it easier to understand what changed but also serves the purpose of allowing the changes to be applied later. More specifically, it’s Git’s implementation of the famous diff utility. In a nutshell, git diff is the comparison command in Git. Let’s start our git diff guide by walking you through the fundamental questions. The tutorial will assume you’re comfortable working with the command line and know your way around the basic Git commands. ![]() You’ll learn about its main use cases and the most common options, always with practical examples. In this post, we’ll give you a complete yet focused guide for this Git command. In Git, you use the command git diff to compare commits, branches, individual files and more. ![]() Version control tools are great at many things.
0 Comments
Leave a Reply. |