![]() Addition lines are preceded by +, deletion lines by -, and modification lines are sequences of deletions and additions (Source: Brief) So the patch includes deleting the last line, and adding a new line that equals to that line, with the addition of a !. Interestingly, notice that Git views a modified line as a sequence of two changes - erasing a line and adding a new line instead. The deleted line is preceded by a - sign. You can see that the added line ( // new test) is preceded by a + sign. So the output of git diff shows quite a lot of things.įor now, focus on the part starting with This is a simple line. (I will explain the -no-index switch of this command later.) The output of git diff (Source: Brief) Git diff -–no-index file.txt new_file.txt Now we can run git diff to compute the difference between the files like so: Save this file with a new name, say new_file.txt. Add an ! to the end of the last line, so you get this result: After modifying file.txt, we get different 6 lines (Source: Brief) Remove the second line, and insert a new line as the fourth line. You are encouraged to run the commands yourself while reading this post.Ĭonsider this very short text file, called file.txt on my machine, which consists of 6 lines: file.txt consists of 6 lines (Source: Brief) This is important – git diff computes the difference between two strings, which most of the time happen to consist of code, but not necessarily. Git diff takes two inputs, which can be commits or files (Source: Brief) Inputs can be commits, but also files, and even files that have never been introduced to the repository. Git diff is a command that takes two inputs, and computes the difference between them. ![]() The first released version shipped with the 5th Edition of Unix in 1974. diff was developed in the early 1970s on the Unix operating system. Git’s diff is based on the diff utility on UNIX systems. So, what do I mean when I say diff? Let’s start with some history. Rather, it is more helpful to look at how different a commit is from its parent commit, that is, the diff between these commits. Yet, it is really hard to make sense of individual commits by looking at the entire working tree. Specifically, we discussed that a commit is a snapshot of the working tree at a certain point in time, in addition to some meta-data. In a previous post, you learned about Git’s objects. In this post, you will learn what Git diffs and patches are, their structure, and how to apply patches. Many of the interesting processes in Git like merging, rebasing, or even committing are based on diffs and patches.ĭevelopers work with diffs all the time, whether using Git directly or relying on the IDE's diff view.
0 Comments
Leave a Reply. |