0
0
Loading version...
🔄 Update App
🔍 Check for Updates
Test Notification
🔔 Enable Notifications
📰 Fetch NHK News
🚀 Fetch TechCrunch News
🧪 Experiment
📰 Article Management
📚 Reading List
🎤 Speaking List
📊 Statistics
💻 Software Statistics
Push Admin
Edit Reading
Back to List
Basic Information
Title
Please enter a title.
URL
Please enter a valid URL.
Date
カテゴリID
画像ファイル名
統計情報
単語数:
1239語
読了回数:
0回
作成日:
2024/10/23 10:45
更新日:
2025/12/08 09:36
本文
本文
How to Use the diff3 Command on Linux Dave McKay \n \n ""> Hannah Stryker / How-To Geek diff3 detects differences between three text files to help identify edits, file versions, and merge changes. Diffs are displayed in hunks with line numbers and file indicators to easily identify differences. diff3 can merge the contents of three files with conflicts highlighted for manual resolution. The Linux diff3 command detects differences between three text files. This helps you identify versions, understand their differences, and merge the changes. diff3's great for source code, scripts, or any plain text file. What Is a Diff? A diff is the result of running a differencing tool on a set of files. The resulting output contains a list of the differences inside each of the files. The differences are identified by line number, and which file the difference is in. As youd probably expect, diff3 creates diffs from three files. Software version control (SVC) packages such as Git do diffs as part and parcel of what they are, and they do it exceptionally well. If youre using Git or another SVC for diffing files, you should keep on doing so. Its not just developers who can make use of diffs. If youve got any text-based material with different drafts stored in different files, youre likely to find diffs helpful. The diff3 command makes the difficult problem of diffing three files manageable. How diff3 Is Different diff3 takes three filenames as parameters. By convention, theyre referred to as mine, older, and yours, but they can be named anything. The mine, older, and yours model assumes an older ancestor file and two modified derivatives. Heres a simple example. This is my-file.txt. first line second line edited third line This is old-file.txt. first line second line third line This is your-file.txt. first line second line changed third line We can perform a three-way diff by listing the files in order on the command line. diff3my-file.txtold-file.txtyour-file.txt diff3 reports that each file has a different third line. The diff3 Output Format Differences are displayed in hunks. A hunk starts with ==== and an optional number, indicating which file the differences are from. The number is the position of the file on the command line. No number means all three files differ. Heres an example. Files older.txt and yours.txt contain these lines. Line 1 Line 2 Line 3 File mine.txt contains a modified first line. My file Line 1 Line 2 Line 3 Heres the diff3 command. diff3mine.txtolder.txtyours.txt The ====1 delimiter means file 1, or mine.txt. The 1: means file one. The 1c means line one requires changing if it is to match the same line in the older file. The line is displayed. The corresponding lines from files two and three are shown. Because they contain the same text as one another, it is listed once, but identified twice. The stacked 2:1c and 3:1c indicate the line appears in both files, with the line shown below them. Lets make mine.txt and yours.txt the same, with this text. Line 1 Line 2 Line 3 The older.txt file has a modified second line. Line 1 Older file Line 2 Line 3 We haven't changed the filenames, so we can run the same command once more. diff3mine.txtolder.txtyours.txt The 2 at the end of the delimiter ====2 shows the difference in this hunk is in file two. The corresponding line from file one and three is displayed, and the line with the differences from file two is displayed. Lets modify file two once more, and add a line that isnt in the other files. Line 1 Older file Line 2 Line 3 Line 4 We can use our same command. diff3mine.txtolder.txtyours.txt This time, we have two hunks. Both delimiters refer to differences in file two. The first hunk means line two in files one and three needs to be changed to match the same line in file two. Or, line two in file two needs to be changed to match the other two files. The second hunk introduces a new piece of information. The older.txt file has a fourth line. The 1:3a and 3:3a tell us that in files one and three, a new line has to be appended after line three, to match file two. The line is shown below them. The other thing diff3 can display is sequences of lines containing differences. Lets make mine.txt and older.txt the same, with this content. Line 1 Line 2 Line 3 Well change two lines in yours.txt Line 1 Yours Line 2 Yours Line 3 Well run the same command. diff3mine.txtolder.txtyours.txt This time, the line indicators dont use a single line number. They have two line numbers separated by a comma ',' to represent a range of lines. For example, 1:2,3c tells us that in file 1, lines two to three have differences. Merging With diff3 Instead of getting diff3 to list the differences, we can ask diff3 to merge the contents and differences from the files into one single file. What its actually doing is rolling the differences between file three and file two, into file number one. If youre lucky, your files will merge with no conflicts. Sometimes there are changes in the same place in the files, causing conflicts. Conflicts are highlighted so you can choose which sets of lines you wish to keep. The output is written to the terminal window, but you can easily redirect this into a file, then edit the file to review it. Heres a simple case. Here's the contents of mine.txt. We're going to run diff3 against these files. Yipee! This is whats in older.txt. We're going to run diff3 against these files. The yours.txt file contains this. We're going to run the diff3 command against these files. Well use our familiar command to do a diff, then well run it again with the -m (merge) option to perform the merge. diff3mine.txtolder.txtyours.txt diff3-mmine.txtolder.txtyours.txt This blends the differences between older.txt and yours.txt into mine.txt, creating a new fourth version. To capture this output, add a redirect to the command. diff3-mmine.txtolder.txtyours.txt > fourth.txt Sadly, it's rare your merges are that smooth. Heres a more complicated example. The same lines are changed in several files. Heres our new mine.txt chaffinch swallow pigeon sparrow nuthatch This is our new older.txt chaffinch swallow pigeon dove nuthatch Finally, our new yours.txt. chaffinch cuckoo rook dove nuthatch woodpecker goldfinch This is the results of a diff. diff3mine.txtolder.txtyours.txt There are changes in all three files, and extra lines in one file. Well do a merge and redirect the output to a new file. diff3-mmine.txtolder.txtyours.txt > new-file.txt Using your favorite editor, open the file. gedit new-file.txt The section between the two delimiters <<<<<<< and >>>>>>> is a conflict. The conflicting lines from each file are shown in turn. The lines from file one are between <<<<<<< and |||||||, the lines from file two are between ||||||| and =======, and the lines from file three are between ======= and >>>>>>>. On a real-world file youre likely to have several areas of conflict to review and edit, to make them contain the text from the section you want to keep. Differences and Conflicts It's easy to forget what changes you've made to different versions of a file. The diff3 command makes it easy to find those differences. There's more human interaction needed for merging than for diffing, but at least diff3 makes it difficult to miss a conflict.
本文を入力してください。
メモ
メモ・感想
キャンセル
更新
Debug Info:
Saved State:
-
Redirected Flag:
-
Current URL:
-
Refresh
Close
Debug
Send Report
Send Report
Draw Arrow
Clear
Message:
Cancel
Send