Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to see the file which would be committed

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.

  1. generate the .patch file with git diff --staged > my-file.patch
  2. save the original with cp my-file my-file.save
  3. stash the changes with git stash save my-file
  4. apply the patch with git apply my-file.patch
  5. saved the desired result with cp my-file my-file.to-commit
  6. restore the file to the pre-add state with mv my-file.save my-file
  7. 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.

like image 704
Jim Newton Avatar asked Jul 02 '21 07:07

Jim Newton


People also ask

How to show all the committed files in Git?

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.

How to find the commit history for a specific file?

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.

How to view all the changed files in a commit?

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.

How do I view the content of a file in Git?

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


2 Answers

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.

like image 103
Romain Valeri Avatar answered Oct 19 '22 07:10

Romain Valeri


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 content
    you can then use git stash apply or git stash pop to get your unstaged changes back
  • git 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)
like image 2
LeGEC Avatar answered Oct 19 '22 05:10

LeGEC