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.
git diff --staged > my-file.patch
cp my-file my-file.save
git stash save my-file
git apply my-file.patch
cp my-file my-file.to-commit
mv my-file.save my-file
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.
In Git, we can use git show commit_id --name-only to list all the committed files that are going to push to the remote repository. P.S The git status didn’t show the committed files.
There are different ways to find the commit history for a specific file. We can use gitk or git to get the history of a specific file. For graphical view of the log, we can use gitk: It will open one GUI with the history for the file which file is provided in filepath. Note that, you need to install gitk to use this command.
Let's consider two ways of viewing all the changed files in a commit. Using git diff-tree is considered as a preferred way of listing files in a commit as it is a plumbing command. It is used to compare the content and mode of blobs found via two tree objects.
You can use git show to view a file’s content at a specific commit in git: https://www.systutorials.com/docs/linux/man/1-git-show/ $ git show REVISION:/path/to/file You can also save a copy by $ git show REVISION:/path/to/file >file.copy
You can take advantage of the :[<n>:]<path>
construct to access to corresponding staged blob, and just do
git show :my-file
As described here :
:[<n>:]<path>, e.g. :0:README, :README
A colon, optionally followed by a stage number (0 to 3) and a colon, followed by a path, names a blob object in the index at the given path. A missing stage number (and the colon that follows it) names a stage 0 entry. During a merge, stage 1 is the common ancestor, stage 2 is the target branch’s version (typically the current branch), and stage 3 is the version from the branch which is being merged.
So git show :0:path/to/file
or the shorter git show :path/to/file
outputs the full staged version of the file.
For individual files, you can use git checkout-index
:
# will overwrite 'that/file.txt' in place with the indexed version :
git checkout-index -- that/file.txt
# will create '/tmp/that/file.txt' :
git checkout-index --prefix=/tmp/ -- that/file.txt
Or you can mention explicitly the target --work-tree
and --git-dir
(options to git
itself, not to its subcommand), and use git checkout
:
git --git-dir=.git/ --work-tree=/tmp/myindex/ checkout -- .
Also worth mentioning :
git stash -k
will store your unstaged changes in a stash, preserve the index as it is and restore the content on disk to the indexed contentgit stash apply
or git stash pop
to get your unstaged changes backgit checkout -- some/path
will get the version from the index on disk (it will overwrite your local changes, use git stash -k
or some other way to checkout your files if that's a problem)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With