How to download the diffs present in the staging a

2020-07-23 01:33发布

问题:

Is there a way to download the diffs of the files present in staging area in git other than git stash. I would probably want a way to download the diffs as a tar ball?

回答1:

Since git archive works only with a treeish, you would need to:

  • make a commit out of your current index
  • export those files as an archive, as mentioned in "Export only modified and added files with folder structure in Git"
  • git reset (to restore the index)

That is:

git add .
git commit -m "tmp commit for export"
git archive -o patch.zip @ $(git diff --name-only @~..@)
git reset @~

Note: if you have spaces in the pathnames of those files, you might need instead:

git diff -z --name-only @~ @ | xargs -0 git archive -o patch.zip @


回答2:

Assuming that:

  • Your work is already placed in staging area with git add <filename>
  • You don't want to create temporary commits just for the sake of saving the work
  • You want one diff/patch per versioned file

for f in $(git diff --cached --name-only); do \
   p="$f.patch"; git diff --cached -- "$f" > "$p" && zip p.zip "$p" && rm "$p"; \
done
  adding: file-1.c.patch (deflated 36%)
  adding: file-2.c.patch (deflated 44%)

Viewing the archive contents:

unzip -l p.zip 
Archive:  p.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      259  2016-08-07 19:24   file-1.c.patch
      347  2016-08-07 19:24   file-2.c.patch
---------                     -------
      606                     2 files


标签: git