I have modified a file (or some files) in my directory, and I've used git add
to stage some of the changes lines from the file, but not all the changed line.
I can use git diff --staged my-file
to see the diff of what's changed. git diff --staged my-file
ignores lines which were changed but not staged. Here is an example of the output of git diff --staged my-file
diff --git a/ens/cours/ens_cours_JN.csv b/ens/cours/ens_cours_JN.csv
index dcea574..ff33469 100644
--- a/ens/cours/ens_cours_JN.csv
+++ b/ens/cours/ens_cours_JN.csv
@@ -24,6 +24,7 @@ SCALIN_E;EPITA;préparation pédagogique;JN;ING1;2020-05-13;;False;True;PT4H;;
SCALIN_E;EPITA;préparation pédagogique;JN;ING1;2020-05-20;;False;True;PT4H;;
SCALIN_E;EPITA;préparation pédagogique;JN;ING1;2020-05-27;;False;True;PT4H;;
SCALIN_E;EPITA;préparation pédagogique;JN;ING1;2020-06-03;;False;True;PT4H;;
+SCALIN_E;EPITA;préparation pédagogique;JN;ING1;2020-06-03;;False;True;PT4H;;commit this line
THLR;EPITA;préparation pédagogique;JN;ING1;2020-07-20;;False;True;PT8H;;Recording TDs
THLR;EPITA;préparation pédagogique;JN;ING1;2020-07-21;;False;True;PT8H;;Recording TDs
THLR;EPITA;préparation pédagogique;JN;ING1;2020-07-22;;False;True;PT8H;;Recording TDs
Question: How can I generate the text of the file which would be committed? I'd like a check-in hook to eventually process that file before allowing the commit.
I suspect there is some simple incantation using git apply
. However, a simple use of git apply
produces the following diagnostic messages.
jnewton@Marcello cours % git diff --staged > ens_cours_JN.csv.patch
git diff --staged > ens_cours_JN.csv.patch
jnewton@Marcello cours % git apply ens_cours_JN.csv.patch
git apply ens_cours_JN.csv.patch
error: patch failed: ens/cours/ens_cours_JN.csv:24
error: ens/cours/ens_cours_JN.csv: patch does not apply
I have a solution that seems far too complicated.
- generate the .patch file with
git diff --staged > my-file.patch
- save the original with
cp my-file my-file.save
- stash the changes with
git stash save my-file
- apply the patch with
git apply my-file.patch
- saved the desired result with
cp my-file my-file.to-commit
- restore the file to the pre-add state with
mv my-file.save my-file
- restore the fiel to the post-add state with
git stash apply
Now, my file.to-commit
is a copy of the fill which would be committed.
Is this really the correct way to do this? It seems like I'm doing too much work.