-1

Hi Could anyone help on creating a file with only the changed content(lines) and not any other extra metadata like +'s,-'s or filenames from last two git commits locally

git diff 091sd752e7 28cdfbad3
diff --git a/README.md b/README.md
index 86c6331..73a7c15 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
-# agent-conf-data
\ No newline at end of file
+# agent-conf-data
+check

so now I want get a file named X.diff with only the changes like

$ cat X.diff
agent-conf-data
check
Raghavendra
  • 521
  • 5
  • 11
  • 2
    Why is the file named `X.diff`? What about the missing newline? What does your effort so far to do this yourself look like, and what has gone wrong with that effort? – torek Aug 03 '21 at 06:26
  • https://stackoverflow.com/search?q=%5Bgit%5D+list+changed+files+between+two+commits – phd Aug 03 '21 at 12:51
  • `git diff --name-only HEAD~2 > X.diff` – phd Aug 03 '21 at 12:52

1 Answers1

1

Something like this should work for you

git diff HEAD~2 | grep -E '^[+-][^-+]' > X.diff

how this works:

  • get the diff of the last two commits HEAD~2
  • find all the lines that start with + or -, but don't have another - or + following the first
    (NOTE it is valid for a file to do this so it may not work for all cases, for example if it had a diff in it itself! .. if you find this is the case, you should craft your own regex)
  • redirect the output > to a file named X.diff

If you meant to strip the # from the first line and it's not really an error, you could chain another piped command before writing to remove 'em or do any other work

ti7
  • 16,375
  • 6
  • 40
  • 68